如果我使用import/exportES6,那么我的所有测试都会失败并显示错误:
意外的保留字
我将我的测试对象转换为使用旧学校IIFY语法,突然我的测试通过了.或者,采用更简单的测试用例:
var Validation = require('../src/components/validation/validation');//PASS
//import * as Validation from '../src/components/validation/validation'//FAIL
Run Code Online (Sandbox Code Playgroud)
同样的错误.显然这里导入/导出存在问题.使用ES5语法重写我的代码只是为了让我的测试框架感到满意是不切实际的.
我有巴贝尔开玩笑.我尝试了github问题的各种建议.不行,到目前为止.
"scripts": {
"start": "webpack-dev-server",
"test": "jest"
},
"jest": {
"testPathDirs": [
"__tests__"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"testFileExtensions": ["es6", "js"],
"moduleFileExtensions": ["js", "json", "es6"]
},
Run Code Online (Sandbox Code Playgroud)
{
"presets": ["es2015", "react"],
"plugins": ["transform-decorators-legacy"]
}
Run Code Online (Sandbox Code Playgroud)
有没有解决这个问题?
我在jest:24.9.0没有任何配置的情况下使用,从 create-react-app 全局安装。在这些文件中,我使用了 es6 模块。使用时没有错误"test": "react-scripts test"
但是,当我开始使用jestwith 时,"test": "jest --config jest.config.js",我看到以下错误。
FAIL src/configuration/notifications.test.js
? Test suite failed to run
/var/www/management/node/src/configuration/notifications.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import notifications from './notifications';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Run Code Online (Sandbox Code Playgroud) 在使用 Babel、Typescript、React 和 Jest 测试某些组件时,我遇到了 Jest 问题。
在测试 React/Typescript 组件时,我得到了SyntaxError: Cannot use import statement outside a module.
我尝试过的事情:
这里是jest.config.js:
const ignores = ['/node_modules/'];
module.exports = {
preset: 'ts-jest',
roots: ['<rootDir>'],
modulePaths: [
"<rootDir>/src"
],
moduleDirectories: [
"node_modules",
],
transformIgnorePatterns: [...ignores],
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
'^.+\\.(gif|svg|ico)$': '<rootDir>/svgTransform.js',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.js?$',
moduleFileExtensions: ['tsx', 'js', 'ts'],
moduleNameMapper: { …Run Code Online (Sandbox Code Playgroud) jestjs ×3
babel-jest ×1
babeljs ×1
ecmascript-6 ×1
javascript ×1
node.js ×1
reactjs ×1
ts-jest ×1
unit-testing ×1