admin管理员组文章数量:1405630
When I run my jasmine test with Jest I get an error:
G:\git\diamant\SpaUI\node_modules\linqts\dist\src\index.js:10
export { default as List } from './list';
^^^^^^
SyntaxError: Unexpected token export
> 1 | import { List } from 'linqts';
| ^
2 | import { ReportMessageData } from './../models/report.model';
3 | import { TranslateService } from '@ngx-translate/core';
4 | import { Injectable } from '@angular/core';
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (src/app/feature-modules/report/services/report-message-flatten.service.ts:1:1)
I understand that I have to tell Jest to transform the code to plain Javascript, but I don't know how to do it.
My jest.config.js
looks as follows:
var preset = require("jest-preset-angular/jest-preset");
module.exports = {
...preset,
preset: "jest-preset-angular",
transformIgnorePatterns: ["<rootDir>/node_modules/(?!linqts)"],
testMatch: ["**/*.test.ts"],
globals: {
...preset.globals,
"ts-jest": {
...preset.globals["ts-jest"],
tsConfig: "src/tsconfig.test.json",
isolatedModules: true
}
},
moduleNameMapper: {
'^@diamant/feature-modules(.*)$': '<rootDir>/src/app/feature-modules/$1',
}
};
When I run my jasmine test with Jest I get an error:
G:\git\diamant\SpaUI\node_modules\linqts\dist\src\index.js:10
export { default as List } from './list';
^^^^^^
SyntaxError: Unexpected token export
> 1 | import { List } from 'linqts';
| ^
2 | import { ReportMessageData } from './../models/report.model';
3 | import { TranslateService } from '@ngx-translate/core';
4 | import { Injectable } from '@angular/core';
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (src/app/feature-modules/report/services/report-message-flatten.service.ts:1:1)
I understand that I have to tell Jest to transform the code to plain Javascript, but I don't know how to do it.
My jest.config.js
looks as follows:
var preset = require("jest-preset-angular/jest-preset");
module.exports = {
...preset,
preset: "jest-preset-angular",
transformIgnorePatterns: ["<rootDir>/node_modules/(?!linqts)"],
testMatch: ["**/*.test.ts"],
globals: {
...preset.globals,
"ts-jest": {
...preset.globals["ts-jest"],
tsConfig: "src/tsconfig.test.json",
isolatedModules: true
}
},
moduleNameMapper: {
'^@diamant/feature-modules(.*)$': '<rootDir>/src/app/feature-modules/$1',
}
};
Share
Improve this question
edited Nov 25, 2020 at 14:15
Mohammad Fared
6581 gold badge8 silver badges22 bronze badges
asked Nov 25, 2020 at 7:05
Philip FrerkPhilip Frerk
911 gold badge1 silver badge12 bronze badges
1
- Is there any solution available for this issue, i have the similar problem @Philip – Ininiv Commented Mar 15, 2022 at 14:31
1 Answer
Reset to default 3Jest doesn't support ES6
module and hence throwing this error when you directly run the test with Jest. if you want to run like that then you have to add babel.
In newer version of jest babel-jest is now automatically loaded by Jest and fully integrated
Hope this answer your question.
Adding babel in jest.
Installation
babel-jest
is now automatically loaded by Jest and fully integrated. This step is only required if you are using babel-jest to transform TypeScript files.
npm install --save-dev babel-jest
Usage
In your package.json file make the following changes:
{
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
}
}
}
Create .babelrc configuration file
Create a babel.config.json
config in your project root and enable some presets
.
To start, you can use the env preset, which enables transforms for ES2015+
npm install @babel/preset-env --save-dev
In order to enable the preset you have to define it in your babel.config.json
file, like this:
{
"presets": ["@babel/preset-env"]
}
Check for more details on Babel official site
本文标签: javascriptJest SyntaxError Unexpected token exportStack Overflow
版权声明:本文标题:javascript - Jest: SyntaxError: Unexpected token export - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744336730a2601232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论