Closed maxime1992 closed 5 years ago
Hi @maxime1992, after adding material did you try adding @material
to the transformIgnorePatterns - https://github.com/thymikee/jest-preset-angular#adjust-your-transformignorepatterns-whitelist. I'll try to reproduce myself soon.
Arf no. I did not. (Sorry, new to all that conf). Will do.
This issue also happens on an Angular 6 project just created by Angular CLI:
FAIL src/app/app.component.spec.ts
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
C:\Temp\angular-jest\src\setup-jest.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import 'jest-preset-angular';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
I think the schematic should change this two elements:
compilerOptions.module
in tsconfig.json
is es2015
but Jest needs commonjs
: set commonjs
in tsconfig.spec.json
transformIgnorePatterns
in jest.config.js
:
transformIgnorePatterns: [
"node_modules/(?!(jest-test))"
]
To Reproduce Steps to reproduce the behavior:
ng g @briebug/jest-schematic:add
)Desktop
OS: Windows
Node version: 8.12.0
NPM or Yarn version : npm 6.4.1
App type: Angular CLI
Angular version: 6.1.10
Proposed solution by @sylvainleduby does the trick. Thanks
Thanks for the help debugging @sylvainleduby and @Foxandxss. Making the module change does appear to fix the issue with ng7 apps. I've got a branch with that change that I'll work on getting out soon.
For the transformIgnorePatterns
property, I think I'll add the prop by default with no arguments in the hopes that it helps users, but I think it will be hard to know what is a good set of default packages that need to be transformed.
In the meantime the solution for this issue is to set the tsconfig.spec.json.compilerOptions.module
= commonjs
Hey guys, I'm also currently working on an Angular 7 project, but still got pretty much the same error even though I've included the library I depend on (lodash-es
) to my transformIgnorePatterns
. Not sure what I'm missing here. 😕
jest.config.js
module.exports = {
preset: 'jest-preset-angular',
roots: ['src'],
setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
moduleNameMapper: {
'@app/(.*)': '<rootDir>/src/app/$1',
'@assets/(.*)': '<rootDir>/src/assets/$1',
'@core/(.*)': '<rootDir>/src/app/core/$1',
'@env': '<rootDir>/src/environments/environment',
'@src/(.*)': '<rootDir>/src/src/$1',
'@state/(.*)': '<rootDir>/src/app/state/$1'
},
transformIgnorePatterns: ['node_modules/(?!(jest-test|lodash-es))']
};
Error
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
/Users/glenn/cumulocity/asset-management-app/node_modules/lodash-es/lodash.js:10
export { default as add } from './add.js';
^^^^^^
SyntaxError: Unexpected token export
1 | import { Injectable } from '@angular/core';
> 2 | import { identity } from 'lodash-es';
| ^
3 | import { Superhero } from './superhero.model';
4 |
5 | /**
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:440:17)
at Object.<anonymous> (src/app/superhero/superhero.service.ts:2:1)
Turns out I have to leverage the TS compiler to allow compiling JS files, too. Ironically, it's actually also been there in the jest-preset-angular
docs all this time (https://github.com/thymikee/jest-preset-angular#allow-js-files-in-your-ts-compileroptions). :sweat_smile:
Maybe it's worth it to turn on the option, i.e. "allowJs": true
, by default in the schematics?
Describe the bug With the latest from Angular (cli, core, etc) in v7 it seems to be broken.
The error is:
To Reproduce Steps to reproduce the behavior:
Desktop (please complete the following information):
PS:
this might be related to https://github.com/thymikee/jest-preset-angular#unexpected-token-importexportother but tried and couldn't get it working...