Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular 13: SyntaxError: Cannot use import statement outside a module #1346

Closed
Mawi137 opened this issue Nov 8, 2021 · 4 comments
Closed

Comments

@Mawi137
Copy link

Mawi137 commented Nov 8, 2021

After updating to @ngx-translate/[email protected], 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.

@ocombe
Copy link
Member

ocombe commented Nov 8, 2021

You will have to update to the new version of jest angular preset (currently it's 11.0.0.rc2) as angular libs are now ESM only

@Mawi137
Copy link
Author

Mawi137 commented Nov 8, 2021

I'm using "@angular-builders/jest": "^13.0.0-beta.1" which uses jest-angular-preset, They use 11.0.0.rc1 at the moment, but 11.0.0.rc2 has the same issue. Repo to reproduce: https://github.com/Mawi137/jest-angular-13-issue

@Mawi137
Copy link
Author

Mawi137 commented Nov 8, 2021

Apparently this module needs to be excluded in jest using transformIgnorePatterns as explained here: thymikee/jest-preset-angular#1149 (comment).

@Mawi137 Mawi137 closed this as completed Nov 8, 2021
@ocombe
Copy link
Member

ocombe commented Nov 8, 2021

Great find, thanks! I'll try to add a note about that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants