Jest won’t transform the module – SyntaxError: Cannot use import statement outside a module

Even though I have tried them separately, I haven’t tried them together (transform and transformIgnorePatterns). So this jest configuration solved my issue: “jest”: { “preset”: “ts-jest”, “testEnvironment”: “node”, “transform”: { “node_modules/variables/.+\\.(j|t)sx?$”: “ts-jest” }, “transformIgnorePatterns”: [ “node_modules/(?!variables/.*)” ] }, My mistakes were: Not using transform and transformIgnorePatterns together. And defining babel-jest as the transformer instead of … 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” ],