Cannot run tests when having `import.meta.url`
See original GitHub issueDescribe the Bug
Jest cannot compile when there is import.meta.url
in the code, even in console.log
.
The use of import.meta.url
is obligatory in Angular 12 for new Worker(new URL('./app.worker', import.meta.url), ...)
https://angular.io/guide/updating-to-version-12#breaking-changes-in-angular-version-12 Angular 12 updates Webpack from version 4 to version 5.
- Webpack 5 now includes web worker support. However, the structure of the URL within the worker constructor must be in a specific format that differs from the current requirement. To update web worker usage, where
./app.worker
is the actual worker name, changenew Worker('./app.worker', ...)
tonew Worker(new URL('./app.worker', import.meta.url), ...)
. See PR #20466.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
title = 'my-app';
ngOnChanges(): void {
console.log(import.meta.url);
}
}
$ ng test
FAIL src/app/app.component.spec.ts
● Test suite failed to run
src/app/app.component.ts:17:17 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'esnext', or 'system'.
17 console.log(import.meta.url);
~~~~~~~~~~~
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.464 s
Ran all test suites.
error Command failed with exit code 1.
tsconfig.json: generated by Angular 12
{
...
"compilerOptions": {
...
"module": "es2020",
},
...
}
Minimal Reproduction
Create a new app ng new my-app
;
Follow the guide https://www.npmjs.com/package/@angular-builders/jest to add @angular-builders/jest
;
Add import.meta.url
in the code (wherever);
Run the tests.
Expected Behavior
Run the tests successfully.
Screenshots
Environment
Libs
- @angular/core version: “~12.0.1”
- @angular-devkit/build-angular version: “~12.0.1”
- @angular-builders/jest version: “^12.1.0”
For Tooling issues:
- Node version: v14.16.1
- Platform: Windows 10
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:13 (5 by maintainers)
Top Results From Across the Web
How to Use `import.meta` When Testing With Jest
The solution: Modularize the ESM code and mock it. · Export the ESM specific issue, e.g. import.meta functionality, into it's own, "utils" file/ ......
Read more >Cannot run tests when having `import.meta.url` - Bountysource
Jest cannot compile when there is import.meta.url in the code, even in console.log . The use of import.meta.url is obligatory in Angular 12 ......
Read more >import.meta - JavaScript - MDN Web Docs
meta meta -property exposes context-specific metadata to a JavaScript module. It contains information about the module, such as the module's URL.
Read more >import.meta - JavaScript - UDN Web Docs: MDN Backup
The import.meta object exposes context-specific metadata to a JavaScript module. It contains information about the module, like the module's URL.
Read more >ECMAScript Modules - Jest
Execute node with --experimental-vm-modules , e.g. node ... you need to import it from the @jest/globals module or use import.meta .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello @acalvo. Here is the fix to your minimal reproduction that will allow you to use
import.meta.url
:It might be worth encapsulating it inside the builder (certainly the preset part) but it shouldn’t block you.
Thank you for the reply - Do I need to add @angular-builders/jest dependency also in my project or it is not required. I added and checked also followed the diff changes still getting the same issue.