show correct error in jest unit test angular

Viewed 2508

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.

2 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

This error with flabby description only happens when it is executed with nx test (in my case) and it executes the test in sequence and does not show the most detailed error (because there is).

In my case what I did was specifically run the jest to the specific file with the specified config file like this:

npx jest app.component.spec.ts --config=apps/my-app/jest.config.js

So the problem was more descriptive and I handled to solve it.

NOTE: Remember to install jest -global first.

Related