jest-preset-angular: Yarn Workspace Monorepo + Library Built with Angular 12 + Jest Throws an Error
đ Bug Report
So, this is an interesting situation. Iâm responsible for a component library within our company, and we recently updated for Angular 12. Part of Angularâs guidance for library authors, is to compile with Partial Ivy starting in Angular 12. An app thatâs using our library in an Angular project thatâs nested within a monorepo, using Yarn Workspaces, is failing to run tests on components that use stuff from our library, with the following error:
Error: thrown: "Failed to load header.component.html"
That html is a file nested (and built) within our library code. It isnât even a template thatâs in the app project. Just to test this out, I rebuilt the library with Ivy completely disabled, published it, installed it in the app and re-tested, and the tests run fine. So it seems the Jest config is having trouble working with Partial Ivy libraries, when node_modules is hoisted to the top of the repo. I found this issue in another libraryâs repo, seeming to indicate the same problem after they build with Partial Ivy: https://github.com/tiberiuzuld/angular-gridster2/issues/740
Another, possibly related issue, was resolved in the angular repo a year and a half ago. Could point towards a possible solution https://github.com/angular/angular/issues/35747
To Reproduce
Steps to reproduce the behavior: Setup monorepo with Yarn workspaces. Create new Angular app as a project within the monorepo with Jest configured Install that angular-gridster2 library Add one of their components to the app.component.html, and import the module into the spec Run the test
Expected behavior
Should run without error
Link to repo (highly encouraged)
My repo is internal, but if youâre unable to reproduce with the steps above, I can try and set something up.
Error log:
Error: thrown: "Failed to load header.component.html"
at context.<computed> (C:\Users\<user>\src\Atom\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4309:39)
at C:\Users\<user>\src\Atom\packages\GuideApp\src\app\app.component.spec.ts:55:3
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (C:\Users\<user>\src\Atom\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:407:30)
at Zone.Object.<anonymous>.Zone.run (C:\Users\<user>\src\Atom\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:167:47)
at C:\Users\<user>\src\Atom\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4227:33
at _dispatchDescribe (C:\Users\<user>\src\Atom\node_modules\jest-circus\build\index.js:97:26)
at describe (C:\Users\<user>\src\Atom\node_modules\jest-circus\build\index.js:60:5)
at context.<computed> (C:\Users\<user>\src\Atom\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4270:39)
at Object.<anonymous> (C:\Users\<user>\src\Atom\packages\GuideApp\src\app\app.component.spec.ts:26:1)
at Runtime._execModule (C:\Users\<user>\src\Atom\node_modules\jest-runtime\build\index.js:1394:24)
at Runtime._loadModule (C:\Users\<user>\src\Atom\node_modules\jest-runtime\build\index.js:996:12)
at Runtime.requireModule (C:\Users\<user>\src\Atom\node_modules\jest-runtime\build\index.js:828:12)
at jestAdapter (C:\Users\<user>\src\Atom\node_modules\jest-circus\build\legacy-code-todo-rewrite\jestAdapter.js:79:13)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at runTestInternal (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\jest-runner\build\runTest.js:389:16)
at runTest (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\jest-runner\build\runTest.js:481:34)
at TestRunner.runTests (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\jest-runner\build\index.js:111:12)
at TestScheduler.scheduleTests (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\@jest\core\build\TestScheduler.js:333:13)
at runJest (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\@jest\core\build\runJest.js:387:19)
at _run10000 (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\@jest\core\build\cli\index.js:408:7)
at runCLI (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\@jest\core\build\cli\index.js:261:3)
at Object.run (C:\Users\<user>\src\Atom\packages\GuideApp\node_modules\jest-cli\build\cli\index.js:163:37)
envinfo
System:
OS: Windows 10
Npm packages:
jest: 27.0.6
jest-preset-angular: 9.0.4
typescript: 4.3.5
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 19 (10 by maintainers)
Commits related to this issue
- add explicit "ngcc" call in postinstall to fix template error see https://github.com/thymikee/jest-preset-angular/issues/980 for further information — committed to loetscher/ngx-spinner-ng12-jest by bison-rmc 2 years ago
- add explicit "ngcc" call in postinstall to fix template error see https://github.com/thymikee/jest-preset-angular/issues/980 for further information — committed to loetscher/ngx-spinner-ng12-jest by bison-rmc 2 years ago
@literalpie , it is a must to run
ngccbefore running Jest, otherwise Jest wonât get the correctumdmodules for Angular package format. In your repo, you can just putrequire('jest-preset-angular/ngcc-jest-processor');in yourjest.config.jsand the test will pass.@michaelfaith in your case, you would need to run
ngccat root of the project because all ofnode_modulespackages are hoisted at the top in the Yarn workspace. The scriptrequire('jest-preset-angular/ngcc-jest-processor');doesnât help in this case because it doesnât handle the case of Yarn workspace hoisting.Normally, when you run
ngccor using the scriptrequire('jest-preset-angular/ngcc-jest-processor');, you should seethe 1st time you run it. If you donât see anything like that, you need to check your
node_modulesto see if it contains__ngcc_entry_points__.jsonwhich is a file created afterngccruns.I hope this helps you guys solving the issue. Pls understand that
ngccis a must to work with Jest, the same applies to Karma + Jasmine. When Angular CLI runs Karma + Jasmine, 1st it also needs to runngccto produce the correctesm2015bundle to start testing. For Jest, we needumd(in normal mode) orumd+esm2015in ESM mode.I can work with that. Thanks for helping to zero in on the issue. Adding âpostinstallâ: ângccâ to the top level package.json (not the lower one), it all works as expected. Appreciate the help.
We need
ngccbecause it does also process all packages under@angular. If those packages are not processed byngcc, Jest wonât run correctly. If I donât runngccexplicitly from terminal, therequire('jest-preset-angular/ngcc-jest-processor');doesnât run at all which explains why tests failed.When Angular executes tests, it will access the codes which are in packages under
@angular. As long as Angular still hasngcc, we still have to run it.What I did in your repo is:
yarn installngccfrom root of the repoyarn reproI see the test passed after those 3 steps. Iâm not sure what else I should do?
Hi! Iâm glad this issue is already being tracked because Iâm also running into it. However, I donât think yarn monorepos have anything to do with the issue. I was able to reproduce it using a basic Angular project and angular-gridster2 here.
Hope this helps!
edit: you can see the debug log output here