Jest configuration issue with React/TypeScript

Viewed 509

Trying to configure jest env on my project, was getting a below error when I run simple test case:

Test suit failed to run
  TypeError: Jest: a transform must export a 'process' function
    at ScriptTransformer._getTransformer(../../node_modules/jest-runtime/node_modules/@jest/transform/build/ScriptTransformer.js:360.13)
1 Answers

I came across this issue specifically for css/scss modules.

solved it by installing jest-css-modules-transform npm package.

module.exports = {
  testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
  setupFilesAfterEnv: ["<rootDir>/setupTests.js"],
  transform: {
    "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
    "\\.(css|less|scss|sass)$":
      "<rootDir>/node_modules/jest-css-modules-transform",
  },
};

But when i used below config (transform method created using styleMock.js) i was getting the above error.

module.exports = {
  testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
  setupFilesAfterEnv: ["<rootDir>/setupTests.js"],
  transform: {
    "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
    "\\.(css|less|scss|sass)$": "<rootDir>/test/styleMock.js",
  },
};

Hope this help someone in the same area.

Related