我正在尝试使用Jest和Enzyme测试我的应用程序。每当我运行“纱线测试”时,我都会不断收到以下错误:
Jest遇到意外令牌
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: /path/untitled/om/dist/__test__/setup/enzyme.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { configure } from 'enzyme'; ^^^^^^ SyntaxError: Unexpected token import
我没有单独的.babelrc,但我相信webpack中的内容就足够了。我也尝试过用Google搜索它,但没有找到
webpack.config.js
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: "./src/index.html",
filename: "index.html",
inject: "body"
});
module.exports = {
devtool: "eval",
entry: ["@babel/polyfill", "./src/index.js"],
output: {
path: path.resolve("dist"),
filename: "bundle.js",
publicPath: "/"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/react"],
plugins: ["@babel/plugin-proposal-class-properties", "transform-export-extensions"]
}
}
},
{
test: /\.svg$/,
loader: 'svg-inline-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ["eslint-loader"]
},
{
test: /^.*\.(css|scss)$/,
use: [
'style-loader',
'css-loader?importLoaders=1&modules&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
'sass-loader'
],
}
]
},
plugins: [HtmlWebpackPluginConfig]
};
package.json
{
"name": "frontend",
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/__test__/setup/enzyme.js",
"testPathIgnorePatterns": [
"<rootDir>/node_modules"
],
"transform": {
"^.+\\.js$": "babel-jest"
},
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"testMatch": [
"<rootDir>/src/**/*.test.js"
],
"transformIgnorePatterns": [
"!node_modules/react-runtime"
]
},
"version": "1.0.0",
"main": "index.js",
"scripts": {
"format:all": "prettier --write \"src/**/*.js\"",
"start": "webpack-dev-server --mode=development",
"eslint": "eslint --ext .js ./src",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.0.0-rc.1",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.0.0-rc.1",
"axios": "^0.18.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.0-beta.4",
"babel-polyfill": "^6.26.0",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"enzyme-to-json": "^3.3.4",
"eslint": "^5.7.0",
"eslint-loader": "^2.1.1",
"eslint-plugin-react": "^7.11.1",
"html-webpack-plugin": "^3.2.0",
"jest": "^23.6.0",
"lodash.get": "^4.4.2",
"node-sass": "^4.9.4",
"postcss-loader": "^3.0.0",
"prettier": "^1.14.3",
"react-icons": "^3.2.2",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "3.1.4"
},
"dependencies": {
"@babel/polyfill": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@types/react": "^16.4.18",
"babel-preset-react": "^6.24.1",
"css-loader": "^1.0.0",
"prop-types": "^15.6.2",
"react": "^16.4.2",
"react-dom": "^16.5.2",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-form": "^7.4.2",
"redux-saga": "^0.16.2",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1"
}
}
我的酶设置来自Enzyme docs
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
答案 0 :(得分:0)
尝试一下:
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
Enzyme.configure({ adapter: new Adapter() });