1.1.0 • Published 1 year ago

jest-magic-mock v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

jest-magic-mock

EXPERIMENT: Do not actually use this, I think?

This is a JavaScript implementation of a Magic Mock, inspired by Python's unittest.mock.MagicMock. It is implemented using Proxy.

API

import magicMock from 'jest-magic-mock';

test('has all of the properties', () => {
  const mock = magicMock();

  expect(mock.foo.bar.baz[2]).toBeDefined();
});

test('can mock individual properties', () => {
  const mock = magicMock();

  mock.foo.mockReturnValue(3);
  mock.bar.baz.mockImplementation(x => x * x);

  expect(mock.foo()).toEqual(3);
  expect(mock.bar.baz(3)).toEqual(9);
  expect(mock()).toEqual(undefined);
});

See the tests for more examples.