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

`regeneratorRuntime` is not defined when running Jest test

In case you are using a setupTests.js file you can import regenerator-runtime from there: // setupTests.js import ‘regenerator-runtime/runtime’ import Enzyme from ‘enzyme’ import EnzymeAdapter from ‘enzyme-adapter-react-16’ Enzyme.configure({ adapter: new EnzymeAdapter() })   Then you can import setupTests.js to every test file or better yet, in your package.json just add setupFilesAfterEnv to the jest config: // … Read more

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

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 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)