1.3.2 • Published 7 months ago

bunboozle v1.3.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
7 months ago

Conventional Commits Semantic Release

Bunboozle - the simplified Bun mock

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

You can revert mocks by using unboozle() from bunboozle, for example in your afterEach hook.

Features

  • Mock functions and variables in any module (files, classes, objects)
  • Automatically attach a spy that is returned
  • Full Typescript support, get type hints for any module property

Simple function mock

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

it('should mock a single implementation', () => {
  const spy = boozle(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');
});

Simple variable mock

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

it('should mock a variable', () => {
  boozle(mockModule, 'myVariable', 42);

  expect(mockModule.myVariable).toBe(42);
});

Skip the explicit function call if not interested in parameters

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

it('should mock a function', () => {
  boozle(mockModule, 'myFunction', 'Mocked string');

  expect(mockModule.myFunction).toBe('Mocked string');
});

Mock functions 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}`;

  boozle(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');
});

Roadmap

  • Mock functions with mockImplementation.
  • Mock functions with mockImplementationOnce for sequential mocking.
  • Automatically restore original implementation after each test.
  • Mock variables with a single value.
  • Mock variables with mockImplementationOnce for sequential values.
  • Built-in support for spying on function calls without mocking.
1.3.2

7 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.3

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago