首页 > 解决方案 > 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));
  }));

标签: angularjestjszone

解决方案


推荐阅读