angular - Angular 中的笑话问题 - UnhandledPromiseRejectionWarning zone.js
问题描述
目前正在尝试在 Angular 中执行 Jest。从zoneDelegate
. 似乎无法解决。试图更新zone.js
到版本11
,但这似乎会产生新问题。
我尝试添加async
到 spec.ts 中,但这似乎并不能解决问题。
低于我目前的spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ForOthersDashboardComponent } from './for-others-dashboard.component';
describe('ForOthersDashboardComponent', () => {
let component: ForOthersDashboardComponent;
let fixture: ComponentFixture<ForOthersDashboardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ForOthersDashboardComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ForOthersDashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
和我的jest.config
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'jest-preset-angular',
roots: ['<rootDir>/src/'],
testMatch: ['**/+(*.)+(spec).+(ts)'],
setupFilesAfterEnv: ['<rootDir>/src/test.ts'],
collectCoverage: true,
coverageReporters: ['html'],
coverageDirectory: 'coverage/my-app',
globals: {
'ts-jest': {
isolatedModules: true,
astTransformers: {
before: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
},
},
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths || {}, {
prefix: '<rootDir>/'
})
};
编辑
该错误似乎只发生在导入我需要的模块的规范文件中。例如下面的规范文件。它正在尝试加载node_modules
无法找到的东西。我通过设置模块路径尝试了其他解决方案,但这似乎并没有解决这个问题。
import { RequiredModule } from 'required-module-lib';
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RequiredModule
],
declarations: [
DashboardComponent
]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
.catch(err => console.error(err));
}));
解决方案
推荐阅读
- c# - 在数据访问层中使用 DI 的更好方法
- node.js - 如何修复角度表单控件中的“找不到名称为 __v 的表单控件”错误?
- c# - Xamarin C#在不打开相机的情况下拍照
- python - matplotlib 错误:x 和 y 的大小必须相同
- css - 如何在表格中定义最小列宽
- javascript - React - 如何实现页面范围的操作?
- css - Docker 主题 Jupyter
- python - MIDI 文件中的“持续时间”和“偏移量”有什么区别?
- java - 如何在 VIsualVM 上可视化暴露给 JMX 服务器的 Flink 的 Meter 和 Counter 指标?
- spss - 根据所有变量的“值标签”更改值