jest mock instance
describe("mockImplementation", () => {
test("function", () => {
const mockFn1 = jest.fn().mockImplementation(() => 42);
const mockFn2 = jest.fn(() => 42);
expect(mockFn1()).toBe(42);
expect(mockFn2()).toBe(42);
});
3.67
6
test("mock.instances", () => {
const mockFn = jest.fn();
const a = new mockFn();
const b = new mockFn();
mockFn.mock.instances[0] === a;
mockFn.mock.instances[1] === b;
});
Thank you!
6
0
3.75
8
//class.js
class MyClass {
methodOne() {
return 1;
}
methodTwo() {
return 2;
}
}
module.exports = MyClass;
// class.test.js
test('spy using class method', () => {
const result = new MyClass()
const spy = jest.spyOn(result, 'methodOne')
result.methodOne()
// check class method is call or not
expect(spy).toHaveBeenCalled()
// expect old value
expect(result.methodOne()).toBe(1)
// expect new value
spy.mockReturnValueOnce(12)
expect(result.methodOne()).toBe(12)
})
Thank you!
8
0
Are there any code examples left?
New code examples in category Javascript