我正在尝试运行我的 Jest 测试。我在 export default configureStore
行收到错误 SyntaxError: Unexpected token export
,特别是在“export”一词上。对我来说,这表明 redux-mock-store
没有被 Babel 转译,那么我如何用 Jest 强制执行此操作呢?我正在使用jest-webpack .
ContractsActions.test.js
import * as contractsActions from './contractsActions';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares);
describe('async actions', () => {
const store = mockStore({})
return store.dispatch(contractsActions.fetchAllContracts()).then(() => {
//Do something
})
});
package.json
...
"scripts": {
"test": "jest-webpack",
...
"jest": {
"transformIgnorePatterns": [
"!node_modules/"
]
}
...
webpack.config.js
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:
{
presets:['es2015', 'react', 'stage-2']
}
}
]
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
react: path.resolve('./node_modules/react'),
}
},
最佳答案
我最终通过创建一个单独的 .babelrc 文件来解决这个问题,而不是尝试在 package.json 中设置 babel 配置设置。我确信我尝试过的其他步骤可能有所帮助,但这似乎是修复它的方法。
.babelrc
{
"presets": [["es2015", {"modules": false}]],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
关于javascript - 开 Jest 测试 'unexpected token export',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49060283/