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

SyntaxError: Cannot use import statement outside a module #914

Open
ghost opened this issue Nov 15, 2021 · 7 comments
Open

SyntaxError: Cannot use import statement outside a module #914

ghost opened this issue Nov 15, 2021 · 7 comments

Comments

@ghost
Copy link

ghost commented Nov 15, 2021

Jest doesnt work anymore with ngx-toaster after updating to Version 14.x.x, caused by using "mjs".

There is currently no angular jest preset, that works with ngx-toaster *.mjs

Jest Error:

● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

C:\Users\dmitry.prudnikov\IdeaProjects\biskin-kit\node_modules\ngx-toastr\fesm2020\ngx-toastr.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

   5 | import { BsUserManagementDeleteModalComponent } from './delete-modal/user-management-delete-modal.component';
   6 | import { BsUserManagementPasswordResetModalComponent } from './password-reset-modal/user-management-password-reset-modal.component';
>  7 | import { ToastrService } from 'ngx-toastr';
     | ^
   8 | import { BS_USER_SERVICE_TOKEN } from '../../services/user-management-tokens';` 

Jest Config:

"use strict";
module.exports = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js','mjs', 'html'],
coverageReporters: ['html'],
transform: {
'^.+\.(ts|js|mjs|html)$': 'ts-jest',
},
testEnvironment: 'jsdom',
};

module.exports = {
name: 'user-management',
preset: '../../../jest.preset.js',
setupFilesAfterEnv: ['/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '/tsconfig.spec.json',
stringifyContentPathRegex: '\.(html|svg)$',
diagnostics: { warnOnly: true },
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
isolatedModules: true,
},
},
coverageDirectory: '../../../coverage/libs/biskin-kit/user-management',
transform: {
'^.+\.(ts|js|mjs|html)$': 'jest-preset-angular',
'^.+\.m?js$': 'ts-jest',
},
transformIgnorePatterns: ['/node_modules/(?!@ngx-toastr)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'mjs', 'jsx', 'json', 'html'],
modulePathIgnorePatterns: ['/dist/'],
moduleNameMapper: {
'.+\.(mdx?)$': 'identity-obj-proxy',
},
clearMocks: true,
};

@MaximeKoitsalu
Copy link

I am having exactly the same issue

@dimitriosPenta
Copy link

dimitriosPenta commented Dec 7, 2021

So, I solved it by adding the following to my jest.config.js (I am using jest-preset-angular):

moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  transform: {
    '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
  },

@MaximeKoitsalu
Copy link

Thanks, I tried your config, but it didn't change anything for me :(

@aburii
Copy link

aburii commented Dec 8, 2021

Same here, tried the config

So, I solved it by adding the following to my jest.config.js (I am using jest-preset-angular):

moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  transform: {
    '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
  },

and it changed nothing for me too.

Does someone has any other solutions?

@MKlimentowicz
Copy link

The error disappears when I downgrade the dependency to 14.1.3

package.json:

"ngx-toastr": "14.1.3"

@aburii
Copy link

aburii commented Dec 21, 2021

#914 (comment)

Yep, that is the only solution for now

@nschimek
Copy link

The error disappears when I downgrade the dependency to 14.1.3

package.json:

"ngx-toastr": "14.1.3"

Thank you, this solved the issue for me for now.

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

5 participants