Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show correct error in jest unit test angular

I am writing unit test in NX angular workspace. sometimes it is giving error like this :

(node:15320) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'element' -> object with constructor 'Object'
    |     property 'componentProvider' -> object with constructor 'Object'
    --- property 'parent' closes the circle
    at stringify (<anonymous>)
    at writeChannelMessage (internal/child_process/serialization.js:117:20)
    at process.target._send (internal/child_process.js:779:17)
    at process.target.send (internal/child_process.js:677:19)
    at reportSuccess (C:\Users\INFINTY\angular\nfx__1-sep\node_modules\jest-runner\node_modules\jest-worker\build\workers\processChild.js:67:11)
(node:15320) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15320) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It is not showing the exact error. I know there was a command in jasmine/karma to force test to show correct error, but i forgot it. can anyone please help me, how can i get exact error in jest/cypress.

like image 933
raju Avatar asked Sep 14 '20 10:09

raju


1 Answers

In my app, it was a matter of importing the HttpClientModule into the test file (HttpClient was being used in that component):

import { HttpClientModule } from '@angular/common/http';

describe('AppComponent', () => {
    beforeEach(async () => {
        await TestBed.configureTestingModule({
            imports: [HttpClientModule, RouterTestingModule],
            declarations: [AppComponent],
        }).compileComponents();
    });
});

I'm using:

  • angular 10.1.4
  • jest 26.6.0
  • jest-preset-angular 8.3.1
like image 121
Avi Kaminetzky Avatar answered Sep 29 '22 04:09

Avi Kaminetzky