Jest: Timer and Promise don’t work well. (setTimeout and async function)

Yes, you’re on the right track. What happens await simpleTimer(callback) will wait for the Promise returned by simpleTimer() to resolve so callback() gets called the first time and setTimeout() also gets called. jest.useFakeTimers() replaced setTimeout() with a mock so the mock records that it was called with [ () => { simpleTimer(callback) }, 1000 ]. … Read more

How do I deal with localStorage in jest tests?

Great solution from @chiedo However, we use ES2015 syntax and I felt it was a little cleaner to write it this way. class LocalStorageMock { constructor() { this.store = {}; } clear() { this.store = {}; } getItem(key) { return this.store[key] || null; } setItem(key, value) { this.store[key] = String(value); } removeItem(key) { delete this.store[key]; … Read more

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

How to resolve “Cannot use import statement outside a module” in jest

Also using Babel, Typescript and Jest. Had the same failure, driving me crazy for hours. Ended up creating a new babel.config.js file specifically for the tests. Had a large .babelrc that wasn’t getting picked up by jest no matter what i did to it. Main app still uses the .babelrc as this overrides babel.config.js files. … Read more

How to make Jest wait for all asynchronous code to finish execution before expecting an assertion

Updated for Jest 27+ For jest 27+, you can also use process.nextTick: await new Promise(process.nextTick); (Thanks to Adrian Godong in the comments) Original Answer Here’s a snippet that waits until pending Promises are resolved: const flushPromises = () => new Promise(setImmediate); Note that setImmediate is a non-standard feature (and is not expected to become standard). … Read more

How can I mock the JavaScript window object using Jest?

The following method worked for me. This approach allowed me to test some code that should work both in the browser and in Node.js, as it allowed me to set window to undefined. This was with Jest 24.8 (I believe): let windowSpy; beforeEach(() => { windowSpy = jest.spyOn(window, “window”, “get”); }); afterEach(() => { windowSpy.mockRestore(); … Read more

Jest mock inner function

If an ES6 module directly exports two functions (not within a class, object, etc., just directly exports the functions like in the question) and one directly calls the other, then that call cannot be mocked. In this case, funcB cannot be mocked within funcA the way the code is currently written. A mock replaces the … Read more

Does Jest support ES6 import/export?

From my answer to another question, this can be simpler: The only requirement is to configure your test environment to Babel, and add the ECMAScript 6 transform plugin: Step 1: Add your test environment to .babelrc in the root of your project: { “env”: { “test”: { “plugins”: [“@babel/plugin-transform-modules-commonjs”] } } } Step 2: Install the … Read more

How do I test axios in Jest?

Without using any other libraries: import * as axios from “axios”; // Mock out all top level functions, such as get, put, delete and post: jest.mock(“axios”); // … test(“good response”, () => { axios.get.mockImplementation(() => Promise.resolve({ data: {…} })); // … }); test(“bad response”, () => { axios.get.mockImplementation(() => Promise.reject({ … })); // … }); … Read more