1// lodash.js
2const _ = require('lodash')
3module.exports = () => _.repeat('abc', 3)
4
5// lodash.test.js
6jest.mock('lodash')
7const lodashMock = require('lodash')
8const { repeat } = jest.requireActual('lodash')
9const lodashRepeat = require('../lodash')
10
11describe('manual mock module', () => {
12
13 beforeEach(() => {
14 // setup mock
15 lodashMock.repeat.mockReturnValue(repeat('xxx', 3))
16 })
17
18 it('lodash repeat value', () => {
19
20 // expect lodashMock
21 expect(jest.isMockFunction(lodashMock)).toBeTruthy()
22
23 // expect lodash.js
24 expect(lodashRepeat()).toBeDefined()
25 expect(lodashRepeat()).toBe("xxxxxxxxx")
26 expect(lodashRepeat().length).toBe(9)
27 })
28})
29
1describe("mockImplementation", () => {
2 test("function", () => {
3 const mockFn1 = jest.fn().mockImplementation(() => 42);
4 const mockFn2 = jest.fn(() => 42);
5
6 expect(mockFn1()).toBe(42);
7 expect(mockFn2()).toBe(42);
8 });
1import randomColor from "randomcolor";
2
3
4jest.mock("randomColor", () => {
5 return {
6 randomColor: () => {
7 return mockColor('#123456');
8 }
9 }
10});
11let mockColor = jest.fn();