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

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 15 (6 by maintainers)

Most upvoted comments

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

npm install core-js@2.6.5 --save

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.

@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.

@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?