Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve Angular unit test error: "An error was thrown in afterAll\n[object ErrorEvent] thrown"

When I run ng test command in my angular project has error, it gives an error like that

10% building modules 1/1 modules 0 active04 12 2018 11:29:43.408:WARN [karma]: No captured browser, open http://localhost:9876/
04 12 2018 11:29:43.414:INFO [karma]: Karma v2.0.0 server started at http://0.0.0.0:9876/
04 12 2018 11:29:43.414:INFO [launcher]: Launching browser Chrome with unlimited concurrency
04 12 2018 11:29:43.418:INFO [launcher]: Starting browser Chrome
04 12 2018 11:29:53.540:WARN [karma]: No captured browser, open http://localhost:9876/    
04 12 2018 11:29:53.777:INFO [Chrome 70.0.3538 (Mac OS X 10.14.1)]: Connected on socket SKF3rI13kIK0WCqqAAAA with id 32081204
Chrome 70.0.3538 (Mac OS X 10.14.1): Executed 95 of 191 SUCCESS (0 secs / 0.131 secs)
Chrome 70.0.3538 (Mac OS X 10.14.1) ERROR
  {
    "message": "An error was thrown in afterAll\n[object ErrorEvent] thrown",
    "str": "An error was thrown in afterAll\n[object ErrorEvent] thrown"
  }
Chrome 70.0.3538 (Mac OS X 10.14.1): Executed 96 of 191 ERROR (0 secs / 0.826 secs)
Chrome 70.0.3538 (Mac OS X 10.14.1) ERROR
  {
    "message": "An error was thrown in afterAll\n[object ErrorEvent] thrown",
    "str": "An error was thrown in afterAll\n[object ErrorEvent] thrown"
Chrome 70.0.3538 (Mac OS X 10.14.1): Executed 96 of 191 ERROR (1.386 secs / 0.826 secs)

That is the karma result, every test was passed. Karma test result, click

My package.json content

"devDependencies": {
    "@angular/cli": "~1.7.0",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  },

Thank you.

like image 521
Samet ÇELİKBIÇAK Avatar asked Dec 04 '18 08:12

Samet ÇELİKBIÇAK


People also ask

What is the function of fixture detectChanges () in Angular?

Fixtures have access to a debugElement , which will give you access to the internals of the component fixture. Change detection isn't done automatically, so you'll call detectChanges on a fixture to tell Angular to run change detection.

Which of the following can be used to run unit tests in Angular?

Jasmine is the default test framework used with Angular.

What is mocking in unit testing Angular?

Introduction. Mocking is a great idea for testing Angular apps because it makes maintenance easier and helps reduce future bugs. There are a few complex tools, such as XUnit, for mocking an Angular CLI project. You can execute the mocking methods described in this guide only if you use vanilla Jasmine + Angular Testbed ...

How do you mock a component in Angular unit testing?

A mock component in Angular tests can be created by MockComponent function. The mock component respects the interface of its original component, but all its methods are dummies. To create a mock component, simply pass its class into MockComponent function.


Video Answer


2 Answers

I found the solution, in my case with

  afterAll(() => {
    TestBed.resetTestingModule();
  });

method all errors disappear, there is sample code below

describe('Component', () => {

  let component: Component;

  beforeEach((() => {

    TestBed.configureTestingModule({
      declarations: [Component]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    const fixture = TestBed.createComponent(Component);
    component = fixture.componentInstance;
  });

  it('should have a defined component', () => {
    expect(component).toBeDefined();
  });

  afterAll(() => {
    TestBed.resetTestingModule();
  });

});
like image 64
Samet ÇELİKBIÇAK Avatar answered Oct 19 '22 09:10

Samet ÇELİKBIÇAK


afterEach(() => {
    TestBed.resetTestingModule();
});

This cleared the error for me.

like image 7
sravan ponugoti Avatar answered Oct 19 '22 10:10

sravan ponugoti