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.

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

how to reset module imported between tests

You have to re-import or re-require your module. Check the doc or this issue for more information: https://github.com/facebook/jest/issues/3236 https://facebook.github.io/jest/docs/en/jest-object.html#jestresetmodules describe(‘MyModule’, () => { beforeEach(() => { jest.resetModules() }); describe(‘init’, () => { const myModule = require(‘./MyModule’); it(‘not throws exception when called’, () => { expect(() => myModule.init()).not.toThrow(); }); }) describe(‘do’, () => { const myModule … Read more

Service mocked with Jest causes “The module factory of jest.mock() is not allowed to reference any out-of-scope variables” error

You need to store your mocked component in a variable with a name prefixed by “mock”. This solution is based on the Note at the end of the error message I was getting. Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable … Read more

Simulate a button click in Jest

#1 Using Jest This is how I use the Jest mock callback function to test the click event: import React from ‘react’; import { shallow } from ‘enzyme’; import Button from ‘./Button’; describe(‘Test Button component’, () => { it(‘Test click event’, () => { const mockCallBack = jest.fn(); const button = shallow((<Button onClick={mockCallBack}>Ok!</Button>)); button.find(‘button’).simulate(‘click’); expect(mockCallBack.mock.calls.length).toEqual(1); … 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

Mocking `document` in jest

Similar to what others have said, but instead of trying to mock the DOM yourself, just use JSDOM: // __mocks__/client.js import { JSDOM } from “jsdom” const dom = new JSDOM() global.document = dom.window.document global.window = dom.window Then in your jest config: “setupFiles”: [ “./__mocks__/client.js” ],

tech