Closed imolorhe closed 2 years ago
Ngcc is intended to run at root of a project only. If you are using Jest projects
option, you need to run ngcc over there
If I understand it correctly, I need to run it with the tsconfig.spec.json which would compile for the tests, which is different from the ngcc run in my postbuild script. I don't use Jest projects
option either but that doesn't seem to be related, as the issue is about compiling the node modules dependencies before running the tests
ngcc
, you can disable it, we have provided the possibility via documentationngcc
, you can do 2 things:
setup-jest.ts
at root jest configngcc
command from your package.jsonngcc
is optional thing, it shouldn't block you from anything. The intention of ngcc-jest-processor
is to make it in 1 file which is easy to use and it's entirely optional.
Maybe I'm not explaining it properly. I need ngcc to run, and it has always run as a postbuild script and when running my angular tests thanks to jest-preset-angular. However, I recently decided to use yarn workspaces for my repo which I wasn't using previously. This change made some node modules to get hoisted to the root of the monorepo.
One of the things that jest-preset-angular brings is the convenience to configure all the things needed to properly run angular tests with jest, which I believe is why it also offers to run ngcc for your tests by default. However this was lost when I used yarn workspaces since it only runs ngcc in one of the node modules.
(I should note that I have no issues with running or building the angular project itself, and it is just the tests that have the issue.)
Again I don't use jest projects so I don't have a jest config at the root of the monorepo. However if I had one, the global-setup
from jest-preset-angular would still not work since it tries to resolve the node modules based on the location of the ngcc script, which means in both the root of the repo and the package, it would resolve to the same node modules directory still.
Let me know if this makes it clearer.
I get it now, one thing I want to clear up is: by default this preset doesn’t provide ngcc
to run but you have to opt in, please check our preset https://github.com/thymikee/jest-preset-angular/blob/main/src/presets/index.ts
Our ngcc
script is to stimulate what angular cli does, but if it doesn’t fit your need, you shouldn’t use this preset’s ngcc script but should use the one from Angular itself. I think that would help you to move forward. There is an ngcc
bin file which is shipped with angular cli.
Please also take a look at our example https://github.com/thymikee/jest-preset-angular/tree/main/examples/example-app-yarn-workspace which might help.
Regarding to fix the ngcc script, feel free to submit your PR :)
Version
12.2.0
Steps to reproduce
Setup a monorepo with an angular project with several dependencies, some dependencies are hoisted to the root of the monorepo and others are not.
When you run jest with this preset, you will be getting errors like:
Unexpected value 'TranslateModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation
TypeError: Cannot read properties of undefined (reading 'Tag')
Type AgGridModule does not have 'ɵmod' property
Some tests will pass and others will fail.
As it turns out, jest-preset-angular uses this code to call ngcc to compile the dependencies, but it only accounts for the node_modules directory where the ngcc was found in https://github.com/thymikee/jest-preset-angular/blob/main/src/utils/ngcc-jest-processor.ts#L17-L19
This has the unintended effect that depending on how the hoisting happened (moving the ngcc to the root node modules or the angular project node modules), some dependencies in the other node_modules directory would not get compiled.
Took a bit of digging through the code to identify this as the source of the problem.
I fixed it in my case by copying ngcc-jest-processor and modifying it to run the compiler on both node_modules directories.
It would be great if this issue could be solved directly in this project as well.
Expected behavior
I expect all the tests to pass
Actual behavior
Some tests pass, and others fail
Additional context
For my use case, I fixed it by copying the ngcc-jest-processor into my project and modifying it to call ngcc twice (one per node modules directory) and that resolved the issue for me.
Environment