Angular-cli: For polyfill es7/reflect, it should be mentioned that tests are not run in AOT mode

Created on 5 Sep 2018  Â·  15Comments  Â·  Source: angular/angular-cli

_From @fredgate on January 25, 2018 11:7_

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Area

- [ ] devkit
- [x] schematics

Versions

angular CLI 1.6.4

Repro steps

Use AOT to serve and build the application.

In the polyfills.ts file, comment the import 'core-js/es7/reflect'; line as it is mentioned that :

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
// import 'core-js/es7/reflect';

Then execute unit tests. Tests where a provider (the system under test, or any other class) has a injected dependency will fail with a message indicating that it can resolve all parameter for the class. It is not because its dependencies are not registered in the testing module, but because it can not read metadata of the class and so determine the dependant classes.

The log given by the failure

Error: Can't resolve all parameters for MyService: (?). in http://localhost:9876/_karma_webpack_/vendor.bundle.js (line 20595)
syntaxError@http://localhost:9876/_karma_webpack_/vendor.bundle.js:20595:34
../../../compiler/esm5/compiler.js/CompileMetadataResolver.prototype._getDependenciesMetadata@http://localhost:9876/_karma_webpack_/vendor.bundle.js:35802:35

Desired functionality

The polyfills.ts should mention for the _es7/reflect_ polyfill that unit tests are not run in AOT mode.

_Copied from original issue: angular/devkit#408_

schematicangular docs

Most helpful comment

@ravioshankar It doesn't work with core-js 3. Revert to 2.6.5 fixed the same error for me..

npm install [email protected] --save

All 15 comments

_From @fredgate on January 25, 2018 11:9_

People are having problems like : angular/angular#21440

_From @fredgate on January 25, 2018 11:10_

I can propose a pull request.

In my case when the CLI removed this polyfill production builds using the ReflectiveInjector got broken.
It may be worth mentioning it somewhere.
Thanks,
Leo.

Any more info on that? I am currently having a problem as described above Can't resolve all parameters for BsModalService: (?, ?) however commenting the es7/reflect in or out doesn't seem to matter. I am using Jest 23.6, Angular 6.1.0

@moniuch in my scenario I'm using the ReflectiveInjector and custom decorators which requires the Reflect API. My prod builds got broken after upgrading to Angular 7

I managed to fix it by adding the polyfill back(that was removed by the CLI when I did the update)

Note that the CLI entirely removes the line (instead of commenting it)

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es7/reflect'; 

As a side note, a similar line remains commented, which confused me at first:

/** IE10 and IE11 requires the following for the Reflect API. */
// import 'core-js/es6/reflect';

(Note the es6 vs es7)
adding/removing the es6 line had no effect for me, apparently I need the es7 one.

@leonardochaia Thank you very much for your answer, appreciated. I am still on Angular 6, my project was built by cli 6. I had been made aware of the polyfill thing, and so I tested all 3 permutations of es6 and es7 in an out. Nothing seems to have any impact. The error is still produced regardless of the setting.

Can this situation mean that my Jest setup is wrong, or is it rather independent of the test runner?

@leonardochaia This is because of https://github.com/angular/angular-cli/commit/30f8352ca2b3cccd72f5fcff8e09a543f9c70a8d. If you need this polyfill for other reasons than JIT, than adding it back manually is the right course of action.

@devoto13 As I mentioned, there is no effect of having that line in or out
screenshot 2018-11-06 at 10 20 39

@moniuch It's hard to say what may be wrong here. I would suggest to create a minimal reproduction and either you'll figure out the missing part in the process or it will be much easier for people to help you.

Getting similar issue on ubuntu
CLI version is
"@angular/cli": "~7.3.8",
"@angular-devkit/build-angular": "~0.13.8",

ERROR in ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/jit-polyfills.js
Module not found: Error: Can't resolve 'core-js/es7/reflect' in '/home/openworkshop/git/prithvi/node_modules/@angular-devkit/build-angular/src/angular-cli-files/models'
ERROR in ./src/polyfills.ts
Module not found: Error: Can't resolve 'core-js/es7/reflect' in '/home/openworkshop/git/prithvi/src'
ℹ 「wdm」: Failed to compile.

@ravioshankar which version of core-js do you have specified in your package.json?

@ravioshankar It doesn't work with core-js 3. Revert to 2.6.5 fixed the same error for me..

npm install [email protected] --save

@penfold (i know, you are not a maintainer)

this package depends on [email protected]. Why is it not possible for projects depending on this package to use newer versions of core-js, too? Over at https://github.com/testdeck/testdeck-angular-seed we would like to use the latest core-js, similar to all of the other dependencies.

Angular CLI 8.0+ automatically handles JIT related polyfills; manually including them is no longer necessary.

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings