不确定为什么在此行抱怨:
const wrapper = shallow(<BitcoinWidget {...props} />);
整个测试:
import React from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';
// Local components
import BitcoinWidget from './bitcoinWidget';
const props = {
logo: 'foobar',
coin: {
price: 0
},
refresh: jest.fn()
}
describe('when rendering', () => {
const wrapper = shallow(<BitcoinWidget {...props} />);
it('should render a component matching the snapshot', () => {
const tree = toJson(wrapper);
expect(tree).toMatchSnapshot();
expect(wrapper).toHaveLength(1);
});
});
组件
import React from 'react';
const BitcoinWidget = ({ logo, coin : { price }, refresh }) => {
return (
<div className="bitcoin-wrapper shadow">
<header>
<img src={logo} alt="Bitcoin Logo"/>
</header>
<div className="price">
Coinbase
${price}
</div>
<button className="btn striped-shadow white" onClick={refresh}>
<span>Refresh</span>
</button>
</div>
);
}
export default BitcoinWidget;
还有我的package.json
{
"name": "bitcoin",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-redux": "^5.0.7",
"react-scripts": "1.1.5",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "yarn run test-jest:update --verbose --maxWorkers=2",
"test-jest:update": "jest src --updateSnapshot",
"test-jest": "jest src"
},
"now": {
"name": "bitcoin",
"engines": {
"node": "8.11.3"
},
"alias": "leongaban.com"
},
"jest": {
"verbose": true,
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/client/assetsTransformer.js"
},
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
]
},
"devDependencies": {
"enzyme": "^3.4.4",
"enzyme-to-json": "^3.3.4",
"jest": "^23.5.0"
}
}
答案 0 :(得分:4)
将此添加到您的package.json
玩笑配置中。
"transform": {
"\\.js$": "<rootDir>/node_modules/babel-jest"
},
让我知道问题是否仍然存在。
答案 1 :(得分:1)
在使用Webpack创建的React应用程序中设置Jest时,我也遇到了相同的错误。我必须添加@babel/preset-env
,并且它已修复。我也写了一篇关于博客的文章。
npm i -D @babel/preset-env
然后将其添加到.babelrc
文件的“预设”中。例如
{
"presets": ["@babel/react", "@babel/env"]
}
答案 2 :(得分:0)
对于使用create-react-app的任何人,在使用create-react-app时,package.json中只能更改某些笑话配置。
我在Jest提取内部库方面遇到问题,无论我从该库中导入什么内容,Jest都会显示“意外令牌”错误。
要解决此问题,您可以将测试脚本更改为以下内容:
"test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!(<your-package-goes-here>)/)'",
答案 3 :(得分:0)
对于任何在这个问题上挣扎并且上述答案都不适合他/她的人。
找了好久,终于找到了这个方案
编辑您的 jest.config.js 以添加 transformIgnorePatterns
//jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testMatch: ["**/__tests__/**/*.ts?(x)", "**/?(*.)+(test).ts?(x)"],
transform: {
"^.+\\.(js|ts)$": "ts-jest",
},
transformIgnorePatterns: [
"/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.js$",
"/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.ts$",
"/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.tsx$",
],
}
将要忽略的包放在[]
中,并用|
就我而言[@autofiy/autofiyable|@autofiy/property]