vimic
TypeScript icon, indicating that this package has built-in type declarations

1.2.4 • Public • Published

Conventional Commits Semantic Release

Vimic - the simplified Vitest mimic

Mock the module and function, set the mocked implementation and spy on the result all in one quick call.

The mock will automatically revert to the original functionality afterEach test.

Simple mock

import * as mockModule from 'some-module.js';

it('should mock a single implementation', () => {
  const spy = vimic(mockModule, 'myFunction', (arg: string) => `Mocked with: ${arg}`);

  expect(mockModule.myFunction('Test1')).toBe('Mocked with: Test1');
  expect(spy).toHaveBeenCalledWith('Test1');
  expect(mockModule.myFunction('Test2')).toBe('Mocked with: Test2');
  expect(spy).toHaveBeenCalledWith('Test2');
});

Mock with mockImplementationOnce

import * as mockModule from 'some-module.js';

it('should mock multiple implementations in sequence', () => {
  const mock1 = (arg: string) => `First call: ${arg}`;
  const mock2 = (arg: string) => `Second call: ${arg}`;
  const mock3 = (arg: string) => `Third call: ${arg}`;

  vimic(mockModule, 'myFunction', mock1, mock2, mock3);

  expect(mockModule.myFunction('Test1')).toBe('First call: Test1');
  expect(mockModule.myFunction('Test2')).toBe('Second call: Test2');
  expect(mockModule.myFunction('Test3')).toBe('Third call: Test3');
  expect(mockModule.myFunction('Test4')).toBe('Original implementation: Test4');
});

Package Sidebar

Install

npm i vimic

Weekly Downloads

20

Version

1.2.4

License

Apache-2.0

Unpacked Size

18.6 kB

Total Files

11

Last publish

Collaborators

  • mcmomo