Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azz/jest-magic-mock
https://github.com/azz/jest-magic-mock
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/azz/jest-magic-mock
- Owner: azz
- Created: 2019-06-18T14:43:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T00:46:45.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T03:17:25.267Z (3 months ago)
- Language: JavaScript
- Size: 975 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `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`](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock). It is implemented using [`Proxy`](https://mdn.io/proxy).
## API
```js
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](./src/__tests__/index.test.js) for more examples.