1test("mockFn.mockReset", () => {
2 const mockFn = jest.fn().mockImplementation(() => 43);
3 const MockClass = jest.fn();
4
5 new MockClass();
6 expect(mockFn()).toBe(43);
7
8 expect(mockFn.mock.calls).toHaveLength(1);
9 expect(MockClass.mock.instances).toHaveLength(1);
10
11 mockFn.mockReset();
12 MockClass.mockReset();
13
14 new MockClass();
15 expect(mockFn()).toBeUndefined();
16
17 expect(mockFn.mock.calls).toHaveLength(1);
18 expect(MockClass.mock.instances).toHaveLength(1);
19});
1import Foo from './Foo';
2import Bar from './Bar';
3
4jest.mock('./Bar');
5
6describe('Foo', () => {
7 it('should return correct foo', () => {
8 // As Bar is already mocked,
9 // we just need to cast it to jest.Mock (for TypeScript) and mock whatever you want
10 (Bar.prototype.runBar as jest.Mock).mockReturnValue('Mocked bar');
11 const foo = new Foo();
12 expect(foo.runFoo()).toBe('real foo : Mocked bar');
13 });
14});
15
16
17
1test("mockName", () => {
2 const mockFn = jest.fn().mockName("mockedFunction");
3 mockFn(); // comment me
4 expect(mockFn).toHaveBeenCalled();
5});
1test("mock.calls", () => {
2 const mockFn = jest.fn();
3 mockFn(1, 2);
4
5 expect(mockFn.mock.calls).toEqual([[1, 2]]);
6});