run ngcc when using ng test to run test with ivy
See original GitHub issueDescribe the solution you’d like
When running ng test
with
"test": {
"builder": "@angular-builders/jest:run",
"options": {
}
inside angular.json
, it should run ngcc
beforehand as angular-cli
already does (see: https://github.com/angular/angular-cli/pull/15044#issue-296489062). Otherwise, tests are not running with ivy
, which should be the default for angular 9.
As this might break existing tests, an options
flag like "enableIvy": false
could still be added in order to run them the old way. Or just use the configuration that’s already existing in tsconfig.spec.ts
which usually points to tsconfig.ts
that has "enableIvy": false
already defined.
Describe alternatives you’ve considered
The only way I could make it work is by defining a scripts
inside package.json
.
{
"scripts": {
"test": "ngcc && ng test"
}
}
and then execute it with npm run test
.
It would still be great to just run it with ng test
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Here are my findings:
ngcc
transforms libraries, typicallynode_modules
(js files and dts), to make them Ivy compatible. It is necessary, especially if you want to have meaningful error messages when your test fails.node_modules
it’s enough to runngcc
only once, for example inpostinstall
hook to ensure compatibility.ngcc
on modules other thannode_modules
- when you have a monorepo with libraries and you reference the production bundles of these libraries (dist
folders) from your app’s tsconfig.build
,serve
,karma
) usengtools/webpack
and as part of Webpack compilation they runngcc_processor
on each non-relative module, which essentially handles the case of monorepo with libraries. It also eliminates the need to runngcc
for the wholenode_modules
and only runs it for the files that are part of the compilation.Conclusion: Running
ngcc
as apostinstall
hook might be a viable workaround for 95% of projects, so we have to include it in the documentation and the migration guide.However, the only solution that will handle all the use cases properly is adding a custom Jest transformer that will apply
ngcc_processor
fromngtools/webpack
to all the non-relative modules similarly to Angular CLI.I seriously doubt though that this transformer should be implemented here and not in
jest-preset-angular
.Unfortunately, currently this is the only way. A few things you can do:
node_modules
on CI workersngcc
run and run it on them specifically (instead of the wholenode_modules
folder).