Jest: ReferenceError: global is not defined
Jest: ReferenceError: global is not defined
所以我在 Jest 上使用“react-testing-library”编写单元测试,但我遇到了这个错误:
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)
在我将 "jest-environment-jsdom": "^27.0.6"
添加为开发依赖项后,该错误消失了。
我在使用带有 jest v27.2.3 和 tsjest v27.0.5 的 angular v13 时遇到了这个问题
我的问题是 jest.config.ts 文件
的配置
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'
]
};
或库
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'
]
};
所以我在 Jest 上使用“react-testing-library”编写单元测试,但我遇到了这个错误:
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)
在我将 "jest-environment-jsdom": "^27.0.6"
添加为开发依赖项后,该错误消失了。
我在使用带有 jest v27.2.3 和 tsjest v27.0.5 的 angular v13 时遇到了这个问题 我的问题是 jest.config.ts 文件
的配置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'
]
};
或库
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'
]
};