Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@node-loaders/jest-mock

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@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.

  • 1.1.0
  • next
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
198
decreased by-41.07%
Maintainers
1
Weekly downloads
 
Created
Source

@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

FAQs

Package last updated on 16 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc