4

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>/'
  })
};

screenshot

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));
  }));
Pimmesz
  • 335
  • 2
  • 8
  • 29
  • Check all your imported components and services into test. Some `JSON.stringify` causes this error. Probably due to wrong mocking process or whatever connected to providing wrong value – Sergey Aug 26 '20 at 08:12
  • Consider posting error message instead of an image, this will make the question searchable at least. This happens only with a specific module, doesn't it? The module should be specified then because it's very relevant. See https://stackoverflow.com/help/mcve . *I've tried other solutions by setting the modules path but that does not seem to resolve this issue* - what exactly? – Estus Flask Aug 27 '20 at 14:49

0 Answers0