2

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

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

环境.ts

export const environment = {
    production: false
};

环境.spec.ts

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

环境.prod.ts

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.ts

/**
 * 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 环境中运行而不会出现错误。

4

1 回答 1

2

我正在解决这个问题。对我来说,在 jest.config.js 中,将 testEnvironment 设置为 jsdom 而不是 node 可以解决问题。

于 2019-08-28T05:58:45.590 回答