Jest: ReferenceError: global is not defined
Asked Answered
Z

3

23

So I am writing unit test using "react-testing-library" on Jest and I have this error:

 Test suite failed to run

    ReferenceError: global is not defined

      at Object.<anonymous> (node_modules/@jest/core/node_modules/graceful-fs/graceful-fs.js:92:1)
      at Object.<anonymous> (node_modules/@jest/core/node_modules/expect/build/toThrowMatchers.js:10:24)
      at Object.<anonymous> (node_modules/@jest/core/node_modules/expect/build/index.js:35:48)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:387:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)
Zebu answered 7/7, 2021 at 0:5 Comment(0)
A
34

After I added "jest-environment-jsdom": "^27.0.6" as a dev dependency, that error went away.

Alec answered 12/7, 2021 at 19:37 Comment(5)
this error removed the global issues, but gave me a new one in cannot use import statement outside a module off jest runtime.Spent
for me, i didn't know i was using the old jest-env*dom package. removing it from the package.json helped me. Thanks.Soupandfish
I've just added "resolutions": { "jest-environment-jsdom": "^27" } to the package.json and it solved the issueTriplane
I'm new to React/React testing. what down the "resolutions" : { thing do?Soler
I'm newish to React and React testing How does one revert? Change the version number in package.json and do a yarn install?Soler
R
1

for me downgrading jest/typejest version from 27 to 26 fixed this. found the fix from this github issues. https://github.com/facebook/jest/issues/10957

Refugiorefulgence answered 23/2, 2023 at 18:34 Comment(1)
I'm newish to React and React testing. I've been taking an OLD Udemy course. I started getting this error after updating all of the packages. What is the exact version number of jest/typejest you are using? How does one revert? Change the version number in package.json and do a yarn install?Soler
A
0

I had this problem using angular v13 with jest v27.2.3 and tsjest v27.0.5 and my problem was with the configuration of jest.config.ts files and what solves it for me was this configuration:

module.exports = {
  displayName: 'myApp',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$'
    }
  },
  coverageDirectory: '../../coverage/apps/myapp',
  transform: {
    '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular'
  },
  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!lodash-es/.*)',
    '<rootDir>/node_modules/(?!ng2-charts/.*)',
    '^.+\\.js$'
  ],
  moduleNameMapper: {
    '^lodash-es$': 'lodash'
  },
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment'
  ]
};

or to libs

module.exports = {
  displayName: 'myLib',
  preset: '../../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$'
    }
  },
  coverageDirectory: '../../../coverage/libs/...',
  transform: {
    '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular'
  },
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment'
  ]
};
Arundel answered 21/3, 2022 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.