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” ],

How to resolve “Cannot use import statement outside a module” from Jest when running tests?

I was having the same failure (also using Babel, Typescript and Jest), it was driving me crazy for hours! Ended up creating a new babel.config.js file specifically for the tests. I had a large .babelrc that wasn’t getting picked up by jest no matter what I did to it. The main app still uses the … 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

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