@node-loaders/jest-mock
Integrates [@node-loaders/mock](https://github.com/node-loaders/loaders/tree/main/workspaces/mock#node-loadersmock) with jest-mock using a jest like api.
Last updated a year ago by mshima .
Original npm · Tarball · package.json
$ npm install @node-loaders/jest-mock 
SYNC missed versions from official npm registry.

@node-loaders/jest-mock

Integrates @node-loaders/mock with jest-mock using a jest like api.

Usage

For configuration tools, refer to usage

@node-loaders/jest-mock re-exports @node-loaders/mock default loader, for non default loader use @node-loaders/mock directly.

Implementing tests

Import a module with mocked dependencies:

import { mock, checkMocks, restoreMocks } from '@node-loaders/jest-mock';

const mockedPath = (await mock) < typeof import('path') > 'path';
const mockedModule = await import('./module.js');

describe(() => {
  after(() => {
    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.
    restoreMocks(); // Restore jest-mocks to initial state.
  });

  it(async () => {
    mockedModule.run();
    expect(mockedPath.join).toBeCalled();
  });
});

Due to technical limitation, mock + import only works for second level imported modules, which is equivalent to importMock with { [maxDepth]: 1 } options.

Advanced options

For advanced options use importMock instead of import. For more information on about importMock refer to mock.

import { mock, importMock, checkMocks, fn, restoreMocks, maxDepth, ignoreUnused, fullMock } from '@node-loaders/jest-mock';

const mockedPath = (await mock) < typeof import('path') > 'path';
const mockedFunction = fn.mock();
const mockedModule = await importMock('./module.js', {
  [maxDepth]: number, // -1 by default
  [ignoreUnused]: boolean, // false by default
  '../a-mocked-module.js': {
    [fullMock]: boolean, // false by default
    func: mockedFunction,
  },
});

describe(() => {
  after(() => {
    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.
    restoreMocks(); // Restore jest-mocks to initial state.
  });

  it(async () => {
    mockedModule.run();
    expect(mockedPath.join).toBeCalled();
    expect(mockedFunction).toBeCalled();
  });
});

License

MIT

Current Tags

  • 1.1.0                                ...           latest (a year ago)
  • 1.1.0                                ...           next (a year ago)

9 Versions

  • 1.1.0                                ...           a year ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
Maintainers (1)
Downloads
Total 7
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE