Currently trying to execute Jest in Angular. Getting an unhandledpromise error from zoneDelegate
. Cannot seem to resolve. Tried to update zone.js
to version 11
but that seems to generate new issues.
I've tried adding async
to the spec.ts but that does not seem to resolve the issue.
Below my current 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();
});
});
And my 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>/'
})
};
EDIT
The error only seems to occur in spec files which import a module I need. So for example the spec file below. It is trying to load something out of node_modules
which cannot be found. I've tried other solutions by setting the modules path but that does not seem to resolve this issue.
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));
}));