'React' refers to a UMD global, but the current file is a module error with js-jest

Viewed 11

I'm using ts-jest. I recently removed a bunch of import React from "react" statements from my code. My jest tests started failing with this error:

'React' refers to a UMD global, but the current file is a module

I found a lot of answers to similar questions but nothing that solved the issue for me. So, posting to answer.

This is my jest.config.js file:

module.exports = {
  preset: 'ts-jest',
  rootDir: 'tests',
  globalSetup: '<rootDir>/jest.setup.js',
  globalTeardown: '<rootDir>/jest.teardown.js',
  globals: {
    'ts-jest': {
      tsconfig: {
        jsx: 'react',
      },
    },
  },
};
1 Answers

Changing the jsx: 'react' line to jsx: 'react-jsx' solved it for me.

module.exports = {
  preset: 'ts-jest',
  rootDir: 'tests',
  globalSetup: '<rootDir>/jest.setup.js',
  globalTeardown: '<rootDir>/jest.teardown.js',
  globals: {
    'ts-jest': {
      tsconfig: {
        jsx: 'react-jsx',
      },
    },
  },
};
Related