jest-preset-angular: [Bug]: SyntaxError: Cannot use import statement outside a module
Version
11.0.0-rc.2
Steps to reproduce
Steps to reproduce:
- Clone repo: https://github.com/Mawi137/jest-angular-13-issue
- npm i
- npm test
Expected behavior
No error
Actual behavior
After updating to @ngx-translate/core@14.0.0, I keep getting this error in my jest tests (application itself works fine):
C:\dev\prj\Angular13Test\node_modules\@ngx-translate\core\fesm2015\ngx-translate-core.mjs:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as i0 from '@angular/core';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import { Component } from '@angular/core';
> 2 | import {TranslateService} from '@ngx-translate/core';
| ^
3 |
4 | @Component({
5 | selector: 'app-root',
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/app/app.component.ts:2:1)
Any idea what the issue might be?
I also created a simple new Angular project, added ngx-translate + jest and added the TranslateService in the constructor of the AppComponent. It has the same issue.
Additional context
No response
Environment
System:
OS: macOS 11.6
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Binaries:
Node: 14.17.0 - /usr/local/bin/node
Yarn: 1.19.1 - /usr/local/bin/yarn
npm: 7.15.1 - /usr/local/bin/npm
npmPackages:
jest: ^27.3.1 => 27.3.1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (1 by maintainers)
FYI: The following pattern will not ignore (transform with jest-preset-angular) any
.mjsfiles innode_moduleswhich would better describe the growing set of packages with the new APF format in Angular 13.Only that works for me (ng13 + nx)
So you have to add
mjstotransformarray and ignore either entirenode_modulesor just specific libsSo apparently my problem happened because of this
upgraded to angular 14, and i have this problem: `Details:
`
I think that pattern seems to be generic enough to add to the preset?
You need to add
@ngx-translate/coretotransformIgnorePatternslike thisAngular 13 libraries are shipped under ESM format I believe, which now only contains
.mjsfiles. Jest doesn’t understand.mjsfiles inCommonJSmode therefore you need to inform Jest to transform it.@klevismehmeti how exactly did you manage to fix your issue with
node_modules/@angular/core/fesm2020/testing.mjs:7? I am currently facing the exact same issue every time I add transform + transformIgnorePattern in jest config, even something as simple astransform: { '^.+\\.(ts|js|mjs|html)$': 'jest-preset-angular' }, transformIgnorePatterns: [ '<rootDir>/node_modules/' ],angular 14 ns 8.4.0
If anyone is still facing this error.
I’m using Jest 28 and Angular 13 with jest-preset-angular 12 and the solution was having
transformIgnorePatternsin a single line if you are using a single module in it, for ex.,node_modules.jest.config.js:
and then hopefully, all the .mjs files inside
node_modulesin CommonJS mode will be taken into account by Jest.@TCModus the workaround will be applicable to
jest-preset-angularatnexttag, which is 11.0.0-rc.4. Since you are using Nx, you should wait until Nx releases a new version which supports Angular 13 fully.I don’t know if you’re still struggling with this but the same thing was happening for me on Angular 15.0.4. I had to specify each module as they failed the test, like so:
@achilehero This is an issue with nx. Please follow the following issue: https://github.com/nrwl/nx/issues/7844