How to change the behaviour of a mocked import?

You can mock with a spy and import the mocked module. In your test you set how the mock should behave using mockImplementation: jest.mock(‘the-package-to-mock’, () => ({ methodToMock: jest.fn() })); import { methodToMock } from ‘the-package-to-mock’ it(‘test1’, () => { methodToMock.mockImplementation(() => ‘someValue’) }) it(‘test2’, () => { methodToMock.mockImplementation(() => ‘anotherValue’) })

Recommended approach for route-based tests within routes of react-router

If you think about the responsibility of the AccessDenied component, it isn’t really to send the user home. That’s the overall behaviour you want, but the component’s role in that is simply to send the user to “/”. At the component unit level, therefore, the test could look something like this: import React, { FC … Read more

Mocking methods on a Vue instance during TDD

Solution 1: jest.spyOn(Component.methods, ‘METHOD_NAME’) You could use jest.spyOn to mock the component method before mounting: import MyComponent from ‘@/components/MyComponent.vue’ describe(‘MyComponent’, () => { it(‘click does something’, async () => { const mockMethod = jest.spyOn(MyComponent.methods, ‘doSomething’) await shallowMount(MyComponent).find(‘button’).trigger(‘click’) expect(mockMethod).toHaveBeenCalled() }) }) Solution 2: Move methods into separate file that could be mocked The official recommendation is … Read more

How to mock a constructor like new Date()

Since jest 26, you can use the ‘modern’ fakeTimers implementation (see article here) wich supports the method jest.setSystemTime. beforeAll(() => { jest.useFakeTimers(‘modern’); jest.setSystemTime(new Date(2020, 3, 1)); }); afterAll(() => { jest.useRealTimers(); }); Note that ‘modern’ will be the default implementation from jest version 27. See documentation for setSystemTime here.

How can I use Jest to spy on a method call?

The key is using jests spyOn method on the object’s prototype. It should be like this: const spy = jest.spyOn(Component.prototype, ‘methodName’); const wrapper = mount(<Component {…props} />); wrapper.instance().methodName(); expect(spy).toHaveBeenCalled(); As found here e.g.: Test if function is called react and enzyme Please note it is also best practice to clear the spied function after each … Read more

Message “Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout”

The timeout you specify here needs to be shorter than the default timeout. The default timeout is 5000 and the framework by default is jasmine in case of jest. You can specify the timeout inside the test by adding jest.setTimeout(30000); But this would be specific to the test. Or you can set up the configuration … Read more

How to change mock implementation on a per single test basis?

Use mockFn.mockImplementation(fn). import { funcToMock } from ‘./somewhere’; jest.mock(‘./somewhere’); beforeEach(() => { funcToMock.mockImplementation(() => { /* default implementation */ }); // (funcToMock as jest.Mock)… in TS }); test(‘case that needs a different implementation of funcToMock’, () => { funcToMock.mockImplementation(() => { /* implementation specific to this test */ }); // (funcToMock as jest.Mock)… in TS … Read more

Testing with React’s Jest and Enzyme when simulated clicks call a function that calls a promise

Updated answer: using async / await leads to cleaner code. Old code below. I’ve successfully solved this problem by combining the following elements: Mock out the promise and make it resolve immediately Make the test asynchronous by marking the test function async After simulating the click, wait until the next macrotask to give the promise … Read more