7

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.

Dobby Dimov
  • 944
  • 8
  • 10
raju
  • 6,448
  • 24
  • 80
  • 163
  • getting the same error, no luck so far finding a solution. – George Knap Sep 20 '20 at 07:54
  • @george,. This error is basically some other error in spec.ts. commeent all code in spec file, then uncomment line by line. – raju Sep 20 '20 at 09:12
  • correct. In my case this error is cased by importing `EffectsModule.forRoot()` from ngrx in my test module – George Knap Sep 21 '20 at 10:18
  • I've seen many people reporting this error, and each attributing it to one thing or another, from my observations it actually seems to be some race condition in the Jest reporter that causes an intermittent error – simbro Oct 14 '20 at 15:59
  • I have added this issue to the NX issues list: https://github.com/nrwl/nx/issues/3973 - as this started happening with the latest update to @nrwl/workspace (Related to JEST 26) – UberGeoff Oct 28 '20 at 07:22
  • Look this answer for tips on how to show correct messages: https://stackoverflow.com/questions/54874414/yarn-test-hangs-when-one-more-tests-file-is-added/68643113#68643113 – daymannovaes Aug 03 '21 at 21:51

2 Answers2

3

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
Avi Kaminetzky
  • 1,489
  • 2
  • 19
  • 42
  • 1
    Thanks! That's a really sneaky one... My component isn't using HttpClient but it's using ngrx-translate and the anglar router. I guess one of these requires the HttpClient. In any case, it fixed it. – abracadabrax Nov 11 '20 at 15:24
  • No luck for me unfortunately :( – James Barrett Nov 30 '20 at 09:49
  • 1
    Look this answer for tips on how to show correct messages: https://stackoverflow.com/questions/54874414/yarn-test-hangs-when-one-more-tests-file-is-added/68643113#68643113 – daymannovaes Aug 03 '21 at 21:51
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.

1antares1
  • 1,146
  • 2
  • 11
  • 19