Jest-preset-angular: Tests are hanging when you do not provide a service mock or stub for a component

Created on 3 Oct 2020  路  7Comments  路  Source: thymikee/jest-preset-angular

馃悰 Bug Report

Hello, I believe I might have an issue that is a side effect of setting "emitDecoratorMetadata": true. #288

I created a simple component that injects two services - to test exactly Can't resolve all parameters issue. One of them is an api service that will call on HttpClient. As soon as I add this setting to the tsconfig.spec.json the tests will enter into a hanging status.

I first noticed it when moving from Jest 26.0.1 and Jest-Preset-Angular 8.2.0. I am pretty sure this is related to an upgrade because once I rolled back to this exact version alongside its dependencies, and this problem no longer exists.

This repository was created today with the latest versions of Jest and Jest-Preset-Angular using this article as a guide, though tsconfig.spec.json has been updated to reflect Angular guideline to extend a base tsconfig and jest.config.js has been updated because setupJest.ts has been renamed to setup-jest.ts.

To Reproduce

Download code from repository and run npm install and ng test.

The elapsed time on the error log will keep going indefinitely.

Once you remove the comment from line 13 on test.component.spec.ts, tests are executed.

Expected behavior

Test would fail because the stub/mock was not provided or there is no relevant method on the mock used by the component.

Link to repo (highly encouraged)

https://github.com/gustavobmichel/angular-jest-hanging-tests

Error log:

PS C:\Repositories\angular-jest-hanging-tests> ng test
ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
 PASS  src/app/app.component.spec.ts      
(node:20168) 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:\Repositories\angular-jest-hanging-tests\node_modules\jest-worker\build\workers\processChild.js:67:11)
(node:20168) 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:20168) [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.

 RUNS  src/app/test/test.component.spec.ts

Test Suites: 1 passed, 1 of 2 total       
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        100 s

envinfo

System:
    OS: Windows 10

Npm packages:
    jest: ^26.4.2
    jest-preset-angular: ^8.3.1
    typescript: ~4.0.2
Bug Report

All 7 comments

Your issue does solve when I use the --runInBand/-i option, but I have no clue how this might be related to our preset.

That said I do not believe it is related to emitDecoratorMetadata.

@wtho thank you for coming back to me so quickly. Sorry I forgot to mention that with the flag runInBand it works. I have tried so many things, and forgot to mention it, though I think this is not the solution because for me the biggest point of migrating to jest is the speed that it provides, allowing tests to run in parallel.

I mentioned the emitDecoratorData because as soon as you remove that, the tests stop after the failure, though with a different issue. I thought the emitDecoratorData was more of a workaround based on that thread I mentioned.

I really don't know what is causing it except by knowing that with a specific version of Jest it used to work.

I am gonna try and use an older version of jest to try and reproduce this error.

If you think this is not related to the preset, I am happy to close this issue.

Thanks in advance.

@wtho I have downgraded the version of Jest from 26.4.2 to 25.5.4 and the problem no longer happens. I'll close the issue as this does not seem to be related to the preset. Thank you for your help.

FYI similar issue was reported in jest repo https://github.com/facebook/jest/issues/10577

Hi @ahnpnl, yes I looked at it this morning. The person seemed to be able to trace it back to 26.3.0. I was able to trace it back to working on 26.0.1 and not working from 26.1.0 onwards. The only reason why I know that is because the package-lock.json was stamped with 26.0.1, and when deleting package-lock.json, the version 26.0.1 was on the dependant packages.

It is very difficult to understand the package-lock.json fully, but I saw this on @jest/core on my old package lock.

"requires": {
    "@jest/console": "^26.0.1",
    "@jest/reporters": "^26.0.1",
    "@jest/test-result": "^26.0.1",
    "@jest/transform": "^26.0.1",
    "@jest/types": "^26.0.1",
    "ansi-escapes": "^4.2.1",
    "chalk": "^4.0.0",
    "exit": "^0.1.2",
    "graceful-fs": "^4.2.4",
    "jest-changed-files": "^26.0.1",
    "jest-config": "^26.0.1",
    "jest-haste-map": "^26.0.1",
    "jest-message-util": "^26.0.1",
    "jest-regex-util": "^26.0.0",
    "jest-resolve": "^26.0.1",
    "jest-resolve-dependencies": "^26.0.1",
    "jest-runner": "^26.0.1",
    "jest-runtime": "^26.0.1",
    "jest-snapshot": "^26.0.1",
    "jest-util": "^26.0.1",
    "jest-validate": "^26.0.1",
    "jest-watcher": "^26.0.1",
    "micromatch": "^4.0.2",
    "p-each-series": "^2.1.0",
    "rimraf": "^3.0.0",
    "slash": "^3.0.0",
    "strip-ansi": "^6.0.0"
  },

If you assign jest to ^26.0.1 on my repository, you end up with:

  "requires": {
    "@jest/console": "^26.3.0",
    "@jest/reporters": "^26.4.1",
    "@jest/test-result": "^26.3.0",
    "@jest/transform": "^26.3.0",
    "@jest/types": "^26.3.0",
    "@types/node": "*",
    "ansi-escapes": "^4.2.1",
    "chalk": "^4.0.0",
    "exit": "^0.1.2",
    "graceful-fs": "^4.2.4",
    "jest-changed-files": "^26.3.0",
    "jest-config": "^26.4.2",
    "jest-haste-map": "^26.3.0",
    "jest-message-util": "^26.3.0",
    "jest-regex-util": "^26.0.0",
    "jest-resolve": "^26.4.0",
    "jest-resolve-dependencies": "^26.4.2",
    "jest-runner": "^26.4.2",
    "jest-runtime": "^26.4.2",
    "jest-snapshot": "^26.4.2",
    "jest-util": "^26.3.0",
    "jest-validate": "^26.4.2",
    "jest-watcher": "^26.3.0",
    "micromatch": "^4.0.2",
    "p-each-series": "^2.1.0",
    "rimraf": "^3.0.0",
    "slash": "^3.0.0",
    "strip-ansi": "^6.0.0"
  },

I'll mention my issue there but it might be a different problem.

I have created another branch https://github.com/gustavobmichel/angular-jest-hanging-tests/tree/jest-v25 showing that it used to work on Jest 25.

If you have any other suggestions, please let me know. For the meantime, I am gonna raise an issue with Jest and have to keep using Jest 25.

Thanks for your help.

Ok, thanks for confirming its source is not in our project.
Hope it gets solved soon!

I'm having the same issue. Hopefully they'll fix this soon!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chris5287 picture chris5287  路  7Comments

thymikee picture thymikee  路  6Comments

SrHuevo picture SrHuevo  路  6Comments

splincode picture splincode  路  4Comments

thekip picture thekip  路  6Comments