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

Latest version **1.1.0** (published 2023-05-16) · 0 weekly downloads

## Install

```sh
npm install @node-loaders/jest-mock
pnpm add @node-loaders/jest-mock
yarn add @node-loaders/jest-mock
bun add @node-loaders/jest-mock
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-05-16 |
| First published | 2023-01-03 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mshima |

## Links

- npm: https://www.npmjs.com/package/@node-loaders/jest-mock
- npm.io page: https://npm.io/package/@node-loaders/jest-mock

## Dependencies (1)

- [jest-mock](https://npm.io/package/jest-mock.md) ^29.3.1

## Recent versions

- 1.1.0 (latest) — 2023-05-16
- 1.0.2 — 2023-05-11
- 1.0.1 — 2023-05-11
- 1.0.0 — 2023-05-11
- 0.2.3 — 2023-02-22
- 0.2.1 — 2023-01-10
- 0.1.3 — 2023-01-03
- 0.1.2 — 2023-01-03
- 0.1.1 — 2023-01-03

## README

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

## Usage

For configuration tools, refer to [usage](https://github.com/node-loaders/loaders#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:

```js
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](https://github.com/node-loaders/loaders/tree/main/workspaces/mock#esm).

```js
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

---
_Source: https://npm.io/package/@node-loaders/jest-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
