angular - 如何解决 Angular 7 上的 jest 问题?
问题描述
当我在我的角度 7 上运行测试时,我收到了这个错误
console.error node_modules/jest-environment-jsdom-thirteen/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Error: connect ECONNREFUSED 127.0.0.1:80
at Object.dispatchError (D:\Subin\PROJECTS\angular-project\node_modules\jest-environment-jsdom-thirteen\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:60:19)
at Request.client.on.err (D:\Subin\PROJECTS\angular-project\node_modules\jest-environment-jsdom-thirteen\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:674:20)
at emitOne (events.js:121:20)
at Request.emit (events.js:211:7)
at Request.onRequestError (D:\Subin\PROJECTS\angular-project\node_modules\request\request.js:881:8)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7) undefined
有没有什么办法解决这一问题?
我的 setupJest.ts
import 'jest-preset-angular';
Object.defineProperty(window, 'CSS', {value: null});
Object.defineProperty(document, 'doctype', {
value: '<!DOCTYPE html>'
});
Object.defineProperty(window, 'getComputedStyle', {
value: () => {
return {
display: 'none',
appearance: ['-webkit-appearance']
};
}
});
/**
* ISSUE: https://github.com/angular/material2/issues/7101
* Workaround for JSDOM missing transform property
*/
Object.defineProperty(document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true,
};
},
});
包.json
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "jest",
"lint": "ng lint",
"e2e": "ng e2e"
},
"jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": [
"<rootDir>/setupJest.ts"
],
"globals": {
"ts-jest": {
"diagnostics": false
}
}
},
我从https://angular.io/guide/testing读到这个
当您运行 CLI ng test 命令时,这不是问题,因为它会在运行测试之前编译应用程序。
但是,如果您在非 CLI 环境中运行测试,则此组件的测试可能会失败
我认为这与我在非 CLI 环境中运行测试有关。
我试过的:
- 尝试使用 @angular-builders/jest 在 CLI 环境中运行测试
- 试图覆盖我的应用程序组件上的外部文件
我的应用组件规范
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
test('should create the app', async(() => {
expect(component).toBeTruthy();
}));
});
解决方案
推荐阅读
- python - 在 matplotlib 中的 3D 图的背板上绘制直方图
- c++ - 为什么我只从可变参数模板中提取一个值?
- sql - 访问不匹配或类似的查询,其中一列不包含或不像另一列
- javascript - 如何将数据库中的数据提取到 javascript 文件中并将其存储在数组中?
- entity-relationship - 哪种实体关系方法看起来正确或更好?
- elasticsearch - 获取浏览量、唯一用户的统计信息,没有任何进一步的信息,ELK 就足够了吗?
- css - 如何让css只影响一个按钮,而不是全部?
- git - 推送到不同的远程存储库
- django - 如何获得超级详细的 Django 调试信息?
- arduino - 似乎无法让这个“计数”起作用。每次我按下按钮时,无论代码如何,LED 和灯都会打开