Jest 测试在 bash 中运行,但生成 ReferenceError:XMLHttpRequest 未在 Visua



我正在VSCode中尝试一个非常简单的测试,我试图确保我的环境设置为调试测试。 但是,我确实收到与我的测试无关的错误。

我只是尝试在VSCode中使用Angular 6运行Jest并调试我的应用程序和测试。

环境.ts

export const environment = {
production: false
};

环境规范

import { environment } from './environment';
describe('Production environment', () => {
it('should have the environment not set for production', () => {
expect(environment.production).toBeFalsy();
});
});

环境产品

export const environment = {
production: true
};

environment.prod.spec.ts

import { environment } from './environment.prod';
describe('Production environment', () => {
it('should have the environment set for production', () => {
expect(environment.production).toBeTruthy();
});
});

我收到错误:

FAIL  src/environments/environment.prod.spec.ts  ● Test suite failed to run
ReferenceError: XMLHttpRequest is not defined
at patchXHR (node_modules/zone.js/dist/zone.js:2926:39)
at node_modules/zone.js/dist/zone.js:2919:5
at Function.Object.<anonymous>.Zone.__load_patch (node_modules/zone.js/dist/zone.js:84:33)
at node_modules/zone.js/dist/zone.js:2917:6
at Object.<anonymous>.FUNCTION (node_modules/zone.js/dist/zone.js:9:65)
at Object.<anonymous> (node_modules/zone.js/dist/zone.js:12:2)
at Object.<anonymous> (node_modules/jest-preset-angular/setupJest.js:5:1)

设置-开玩笑

/**
* Jest setup file for automated testing
*/
import 'jest-preset-angular';
import './jestGlobalMocks';

.vscode/launch.json

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"${relativeFile}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

这似乎只是通过导入让 Jest 运行所需的基本代码来实现的。 但是,这确实在 bash 环境中运行而不会出错。

我正在对此进行故障排除。 对我来说,在jest.config.js中,将testEnvironment设置为jsdom而不是node可以解决问题。

最新更新