SyntaxError: Cannot use import statement outside a module
See original GitHub issueJest 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: [‘<rootDir>/src/test-setup.ts’], globals: { ‘ts-jest’: { tsconfig: ‘<rootDir>/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: [‘<rootDir>/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: [‘<rootDir>/dist/’], moduleNameMapper: { ‘.+\.(mdx?)$’: ‘identity-obj-proxy’, }, clearMocks: true, };
Issue Analytics
- State:
- Created 2 years ago
- Reactions:14
- Comments:7
Top GitHub Comments
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.