jest-sandbox
Version:
Simple sinon like sandbox for jest
37 lines (32 loc) • 761 B
JavaScript
/* */
/* eslint-env jest */
/* global JestMockFn */
class JestSandbox {
constructor() {
this._mocks = [];
}
_each(method , ...args ) {
this._mocks.forEach(mock => mock[method](...args));
}
fn(...args ) {
const mock = jest.fn(...args);
this._mocks.push(mock);
return mock;
}
spyOn(...args ) {
const mock = jest.spyOn(...args);
this._mocks.push(mock);
return mock;
}
clear() {
this._each('mockClear');
}
reset() {
this._each('mockReset');
}
restore() {
this._each('mockRestore');
}
}
const createSandbox = () => new JestSandbox();
module.exports = createSandbox;