reactjsjestjstsdx

Jest tests stopped working after upgrading to React 18 (TSDX project)


I'm working on a React component library that was bootstrapped initially with TSDX (v0.14.1). After upgrading React from 17 to 18 and Storybook from 6.5.16 to 7.6.20, all my Jest tests stopped working and threw an error saying: TypeError: Jest: a transform must export a process function

My jest.config.js looks like this:

/** @type {import('jest').Config} */
const config = {
  setupFilesAfterEnv: ['jest-expect-message'],
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/fileMock.js',
    '\\.(css|less|scss|sass)$': 'identity-obj-proxy',
  },
};

module.exports = config;

I'm using:

I tried to add the dev dependency ts-jest, but it didn't resolve the issue.

Has anyone faced this issue? Is there a known compatibility issue with Jest 27 and React 18 in a TSDX setup?


Solution

    1. Remove jest from package.json

      TSDX includes an internal Jest setup.

    2. Add testEnvironment: 'jsdom' to your jest.config.js:

      /** @type {import('jest').Config} */
      const config = {
        testEnvironment: 'jsdom',
        setupFilesAfterEnv: ['jest-expect-message'],
        moduleNameMapper: {
          '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
          '<rootDir>/__mocks__/fileMock.js',
          '\\.(css|less|scss|sass)$': 'identity-obj-proxy',
        },
      };
      
      module.exports = config;