Detalhes do pacote

mock-import

coderaiser938MIT4.2.1

Mocking of Node.js EcmaScript modules

mock, import

readme (leia-me)

Mock Import License NPM version Build Status Coverage Status

Mocking of Node.js EcmaScript Modules, similar to mock-require.

Recording 2022-06-16 at 19 41 04

Install

npm i mock-import -D

Run

Loaders used to get things working, so you need to run tests with:

node --import mock-import/register test.js

mock-import uses transformSource hook, which replaces on the fly all imports with constants declaration:

const {readFile} = global.__mockImportCache.get('fs/promises');

mockImport adds new entry into Map, stopAll clears all mocks and reImport imports file again with new mocks applied.

Supported Declarations

/* ✅ */
import fs from 'node:fs/promises';
/* ✅ */
import {readFile} from 'node:fs/promises';
/* ✅ */
import * as fs1 from 'node:fs/promises';

/* ✅ */
const {writeFile} = await import('fs/promses');

/* ✅ */
export * as fs2 from 'fs/promises';

/* ✅ */
export {readFile as readFile1} from 'fs/promises';

Unsupported Declarations

/* ❌ */
export * from 'fs/promises';

// doesn't have syntax equivalent

How mock-import works?

As was said before, loaders used to get things working. This is experimental technology, but most likely it wan't change. If it will mock-import will be adapted according to node.js API.

  • loader hook intercepts into import process and get pathname of imported file;

  • if pathname in reImports it is processed with 🐊Putout code transformer, changes all import calls to access to __mockImportsCache which is a Map filled with data set by mockImport call. And appends sourcemap at the end, so node can generate correct code coverage.

-import glob from 'glob';
+const glob = global.__mockImportCache.get('./glob.js');
  • if traceCache contains pathname it calls are traced with estrace;

Code like this

const f = () => {};

will be changed to

const f = () => {
    try {
        __estrace.enter('<anonymous:1>', 'trace.js:1', arguments);
    } finally {
        __estrace.exit('<anonymous:1>', 'trace.js:1');
    }
};

Straight after loading and passed to traceImport stack will be filled with data this way:

__estrace.enter = (name, url, args) => stack.push([name, url, Array.from(args)]);

And when the work is done stack will contain all function calls.

  • traceCache contains some paths current file will be checked for traced imports and change them to form ${path}?count=${count} to re-import them;

Environment variables

mock-import supports a couple env variables that extend functionality:

  • MOCK_IMPORT_NESTED - transform each import statement so mock of module work in nested imports as well (slowdown tests a bit)

API

mockImport(name, mock)

  • name: string - module name;
  • mock: object - mock data;

Mock import of a module.

stopAll()

Stop all mocks.

reImport(name)

  • name: string - name of a module

Fresh import of a module.

traceImport(name, {stack})

  • name: string name of a module
  • stack: [fn, url, args];

Add tracing of a module.

reTrace(name)

  • name: string - name of traced module

Apply tracing.

enableNestedImports()

Enable nested imports, can slowdown tests;

disableNestedImports()

Disable nested imports, use when you do not need nested imports support;

Example

Let's suppose you have cat.js:

import {readFile} from 'node:fs/promises';

export default async function cat() {
    const readme = await readFile('./README.md', 'utf8');
    return readme;
}

You can test it with 📼Supertape:

import {createMockImport} from 'mock-import';
import {test, stub} from 'supertape';

const {
    mockImport,
    reImport,
    stopAll,
} = createMockImport(import.meta.url);

test('cat: should call readFile', async (t) => {
    const readFile = stub();

    mockImport('fs/promises', {
        readFile,
    });

    const cat = await reImport('./cat.js');
    await cat();

    stopAll();

    t.calledWith(readFile, ['./README.md', 'utf8']);
    t.end();
});

Now let's trace it:

import {createMockImport} from 'mock-import';
import {test, stub} from 'supertape';

const {
    mockImport,
    reImport,
    stopAll,
} = createMockImport(import.meta.url);

test('cat: should call readFile', async (t) => {
    const stack = [];

    traceImport('fs/promises', {
        stack,
    });

    const cat = await reImport('./cat.js');
    await cat();

    stopAll();

    const expected = [
        ['parse', 'parser.js:3', [
            'const a = 5',
        ]],
        ['tokenize', 'tokenizer.js:1', [
            'parser call',
            'const a = 5',
        ]],
    ];

    t.deepEqual(stack, expected);
    t.end();
});

License

MIT

changelog (log de mudanças)

2025.01.29, v4.2.1

feature:

  • e6200c5 mock-import: putout v38.0.0
  • fc1459d mock-import: eslint-plugin-putout v24.0.0

2024.07.19, v4.2.0

feature:

  • f460b5e mock-import: @putout/test v11.0.0
  • f8afcfb mock-import: c8 v10.1.2
  • 39422d2 mock-import: putout v36.0.2

2024.06.07, v4.1.1

feature:

  • 3736be7 mock-import: isBuiltin

2024.06.07, v4.1.0

feature:

  • 70077fc mock-import: eslint v9.4.0
  • a315ba3 mock-import: @putout/test v10.0.0
  • e8646db mock-import: check-dts v0.8.0
  • 87999ac mock-import: supertape v10.0.0

2024.01.19, v4.0.3

feature:

  • 2223db9 mock-import: c8 v9.1.0
  • 658520c mock-import: putout v35.0.0

2023.12.13, v4.0.2

feature:

  • 9fe9567 rm useless zenload
  • 88f679a mock-import: eslint-plugin-putout v22.1.0
  • ded2310 mock-import: @putout/test v8.0.0
  • a9dc25e mock-import: escover v4.0.1
  • f4b23ec mock-import: supertape v9.0.0
  • 18cb4db mock-import: estrace v5.0.0

2023.12.10, v4.0.1

feature:

  • 46d2b95 mock-import: estrace v4.2.3

2023.12.10, v4.0.0

feature:

  • be2b2ba mock-import: convert to node v18 loaders
  • 101289f mock-import: madrun v10.0.0
  • f134af2 mock-import: putout v34.0.0
  • 0ddf013 drop support of node < 18

2023.11.30, v3.4.1

feature:

  • 8dec7bf mock-import: putout v33.0.0
  • d5a8087 mock-import: eslint-plugin-putout v21.0.1

2023.09.14, v3.4.0

feature:

  • 3139269 mock-import: remove-useless apply-shorthand-properties
  • 62116fb package: @putout/plugin-apply-shorthand-properties v4.0.0
  • 2fe696b package: putout v32.0.0

2023.08.07, v3.3.2

fix:

  • 579104e mock-import: drop useless @babel/core

feature:

  • 41fa600 package: @putout/test v7.0.1

2023.08.04, v3.3.1

feature:

  • b6d7e01 package: eslint-plugin-putout v19.0.0
  • 15812ab package: putout v31.0.0

2023.07.31, v3.3.0

fix:

  • 7b69e65 types

2023.07.31, v3.2.0

fix:

  • 408588c types
  • bf15c00 convert-to-absolute
  • 2a48c69 mock-import: stopAll
  • 83c0c36 loader: add loader field
  • bee71ce mock-import: add ";"

feature:

  • 1d13b26 package: putout v30.0.2
  • ef7ed21 package: eslint-plugin-putout v18.0.0
  • 91b3088 package: c8 v8.0.0
  • 49e8a7b mock-import: add support of node v20 (nodejs/node#48240)

2023.07.06, v3.1.2

fix:

  • 2a48c69 mock-import: stopAll
  • 83c0c36 loader: add loader field

feature:

  • 1d13b26 package: putout v30.0.2
  • ef7ed21 package: eslint-plugin-putout v18.0.0
  • 91b3088 package: c8 v8.0.0

2023.05.31, v3.1.1

fix:

  • bee71ce mock-import: add ";"

feature:

  • 49e8a7b mock-import: add support of node v20 (nodejs/node#48240)

2023.05.31, v3.1.0

feature:

  • 8f2c09e mock-import: add support of node v20 (nodejs/node#48240)
  • fca11e9 package: typescript v5.0.4
  • 08c7ab1 package: eslint-plugin-putout v17.6.0
  • da99ca1 package: @putout/test v6.4.0
  • b90bb14 package: eslint-plugin-n v16.0.0
  • d264734 package: escover v3.1.2

2023.03.06, v3.0.6

feature:

  • package: putout v29.0.0

2023.03.04, v3.0.5

feature:

  • package: putout v28.18.0
  • package: check-dts v0.7.0

2022.10.20, v3.0.4

feature:

  • package: putout v28.0.0
  • package: supertape v8.0.1

2022.07.20, v3.0.3

feature:

  • (package) eslint-plugin-n v15.2.4
  • (package) eslint-plugin-putout v16.0.0
  • (package) putout v27.0.1

2022.05.09, v3.0.2

feature:

  • (package) eslint-plugin-putout v15.1.1
  • (package) @cloudcmd/stub v4.0.1
  • (package) putout v26.0.0

2022.03.07, v3.0.1

feature:

  • (package) eslint-plugin-putout v14.4.0
  • (package) estrace v4.0.0
  • (package) escover v2.0.2
  • (package) zenload v2.0.0

2022.02.17, v3.0.0

feature:

  • (package) @putout/test v5.0.0
  • (package) madrun v9.0.0
  • (mock-import) drop support of node < 16
  • (package) putout v25.0.0
  • (package) supertape v7.0.0
  • (mock-import) get back sourcemap support

2022.01.16, v2.12.0

feature:

  • (mock-import) drop sourcemap support

2022.01.13, v2.11.1

feature:

  • (package) eslint-plugin-putout v13.0.1
  • (package) putout v24.0.2

2022.01.08, v2.11.0

feature:

  • (mock-import) is-built-in: add support of fs/promises

2022.01.08, v2.10.0

feature:

  • (mock-import) add 🎩ESCover
  • (mock-import) built-in: add process

2022.01.06, v2.9.0

feature:

  • (mock-import) context -> defaultLoader

2022.01.05, v2.8.0

feature:

  • (mock-import) add ability to read source from context

2022.01.01, v2.7.8

fix:

  • (mock-import) convert-imports: sourceMapName

2021.12.31, v2.7.7

fix:

  • (mock-import) sourceFilename -> sourceFileName

2021.12.31, v2.7.6

fix:

  • (mock-import) convert-imports: revert improve sourcemaps support: always full coverage

2021.12.31, v2.7.5

fix:

  • (mock-import) full coverage support 🎉

2021.12.31, v2.7.4

fix:

  • (mock-import) sourceFileName

2021.12.31, v2.7.3

fix:

  • (mock-import) sourceFileName: url -> url.pathname

2021.12.31, v2.7.2

fix:

  • (mock-import) convert-imports: improve sourcemaps support

2021.12.30, v2.7.1

fix:

  • (mock-import) convert-imports: set default sourcemap name

2021.12.30, v2.7.0

feature:

  • (mock-import) add support of sourcemaps (bcoe/c8#325)

2021.12.21, v2.6.0

feature:

  • (package) putout v23.0.0
  • (package) @putout/test v4.1.0

2021.12.04, v2.5.0

feature:

  • (mock-import) add enableNestedImports, disableNestedImports

2021.12.04, v2.4.0

feature:

  • (mock-import) add ability to apply nested mock import when MOCK_IMPORT_NESTED env var used

2021.12.03, v2.3.0

feature:

  • (mock-import) add ability to handle node built-ins

2021.12.03, v2.2.2

feature:

  • (mock-import) drop convert to re-import: twice slower, cannot distinguish node native modules - crash while resolve

2021.12.03, v2.2.1

fix:

  • (convert-imports) convert-imports-to-reimports: add support of NamespaceSpecifier

2021.12.03, v2.2.0

feature:

  • (mock-import) add ability to mock nested imports
  • (convert-imports) add plugin-convert-import-to-re-import
  • (convert-imports) mv out replaceImport
  • (package) eslint-plugin-putout v12.2.0

2021.11.10, v2.1.3

feature:

  • (package) putout v22.0.0

2021.10.31, v2.1.2

feature:

  • (package) check-dts v0.6.3
  • (package) eslint-plugin-putout v11.0.0
  • (package) putout v21.0.0

2021.10.21, v2.1.1

fix:

  • (mock-import) add async

2021.10.21, v2.1.0

feature:

2021.09.14, v2.0.0

feature:

  • (mock-import) ?count -> ?mock-import-count=
  • (package) eslint-plugin-putout v10.0.1
  • (package) putout v20.0.1

2021.09.11, v1.12.3

feature:

  • (package) estrace v3.0.1

2021.09.09, v1.12.2

fix:

  • (mock-import) mockImport: rm updating reImports cache

2021.09.08, v1.12.1

fix:

  • (mock-import) types: traceImport, reTrace
  • (mock-import) types: reImport

2021.09.08, v1.12.0

feature:

  • (package) eslint v8.0.0-beta.1
  • (mock-imports) add types

2021.08.28, v1.11.0

feature:

  • (mock-import) reuse is-in-cache in both convert-imports and convert-traced-imports

2021.08.28, v1.10.0

feature:

  • (mock-import) convert-traced-imports: traceCache + resolve -> isTraced

2021.08.27, v1.9.0

feature:

  • (mock-import) add isMocked
  • (mock-import) add support of dynamic imports

2021.08.26, v1.8.0

feature:

  • (mock-import) add support of named and default import

2021.08.25, v1.7.1

fix:

  • (mock-import) reImport: get back, simport do require thing as well

2021.08.25, v1.7.0

feature:

  • (mock-import) reImport: add ability to be used as reImportDefault as well

2021.08.24, v1.6.0

feature:

  • (mock-import) improve support of trace

2021.08.23, v1.5.0

feature:

  • (package) putout v19.0.0
  • (mock-import) add ability to trace function calls

2021.08.21, v1.4.1

fix:

  • (package) rm homepage

2021.08.21, v1.4.0

feature:

  • (mock-import) add list of supported declarations
  • (mock-import) add support of export from

2021.08.20, v1.3.0

feature:

  • (package) eslint-plugin-putout v9.2.1
  • (mock-import) add support of ImportNamespaceSpecifier
  • (package) supertape v6.0.3

2021.08.17, v1.2.0

feature:

  • (mock-import) add ability to mock imports that cannot be resolved
  • (package) eslint v8.0.0-beta.0

2021.06.16, v1.1.0

feature:

  • (package) eslint-plugin-putout v8.0.1
  • (mock-import) convert-imports: merge putout calls: double -> single

2021.05.28, v1.0.8

feature:

  • (package) putout v18.0.0

2021.05.03, v1.0.7

feature:

  • (package) putout v17.0.0

2021.04.04, v1.0.6 feature:

  • (package) supertape v5.1.0
  • (package) putout v16.2.0
  • (package) eslint-plugin-putout v7.3.1

2021.02.11, v1.0.5

feature:

  • (package) putout v15.0.0

2021.02.10, v1.0.4

feature:

  • (package) putout v14.3.0
  • (package) supertape v4.8.0

2020.12.28, v1.0.3

feature:

  • (package) putout v13.0.0

2020.12.07, v1.0.2

feature:

  • (package) putout v12.0.0
  • (package) @putout/test v3.1.0
  • (package) supertape v3.0.2

2020.12.01, v1.0.1

fix:

  • (mock-import) transform