Javascript Jest遇到了意外的标记
不确定它为什么在这条线上抱怨:Javascript Jest遇到了意外的标记,javascript,reactjs,jestjs,Javascript,Reactjs,Jestjs,不确定它为什么在这条线上抱怨: const wrapper=shallow() 整个测试: import React from 'react'; import { shallow } from 'enzyme'; import toJson from 'enzyme-to-json'; // Local components import BitcoinWidget from './bitcoinWidget'; const props = { logo: 'foobar', co
const wrapper=shallow()代码>
整个测试:
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);
});
});
从“React”导入React;
从“酶”导入{shall};
从“酶到json”导入toJson;
//局部分量
从“./BitcoinWidget”导入BitcoinWidget;
常量道具={
徽标:“foobar”,
硬币:{
价格:0
},
刷新:jest.fn()
}
描述('渲染时',()=>{
常量包装器=浅();
它('应该呈现与快照匹配的组件',()=>{
consttree=toJson(包装器);
expect(tree.toMatchSnapshot();
expect(包装器).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;
从“React”导入React;
const BitcoinWidget=({logo,coin:{price},refresh})=>{
返回(
铸币库
${price}
刷新
);
}
导出默认比特币控件;
还有我的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.1.0”,
“私人”:没错,
“依赖项”:{
“axios”:“^0.18.0”,
“反应”:“^16.4.2”,
“react dom”:“^16.4.2”,
“react redux”:“^5.0.7”,
“反应脚本”:“1.1.5”,
“redux”:“^4.0.0”,
“redux thunk”:“^2.3.0”
},
“脚本”:{
“开始”:“反应脚本开始”,
“构建”:“反应脚本构建”,
“弹出”:“反应脚本弹出”,
“测试”:“纱线运行测试笑话:更新--verbose--maxWorkers=2”,
“测试jest:update”:“jest src--updateSnapshot”,
“测试笑话”:“笑话src”
},
“现在”:{
“名称”:“比特币”,
“发动机”:{
“节点”:“8.11.3”
},
“别名”:“leongaban.com”
},
“笑话”:{
“详细”:正确,
“moduleNameMapper”:{
“\ \(jpg | jpeg | png | gif | eot | otf | webp | svg | ttf | woff | woff2 | mp4 | webm | wav | mp3 | m4a | aac | oga)$”:“/client/assetsstransformer.js
},
“moduleFileExtensions”:[
“js”,
“jsx”
],
“模块目录”:[
“节点_模块”
]
},
“依赖性”:{
“酶”:“^3.4.4”,
“酶到json”:“^3.3.4”,
“笑话”:“^23.5.0”
}
}
将其添加到您的包.json
jest配置中
"transform": {
"\\.js$": "<rootDir>/node_modules/babel-jest"
},
“转换”:{
“\\.js$”:“/node\u modules/babel jest”
},
如果问题仍然存在,请告诉我。对于使用create react app的任何人,在使用create react app时,只能在package.json中更改某些jest配置
我在Jest获取内部库时遇到问题,Jest将在我从该库导入的任何位置显示“意外令牌”错误
要解决此问题,您可以将测试脚本更改为以下内容:
“test:“react scripts test--transformIgnorePatterns'node_modules/(?!()/)”,
在使用Webpack创建的react应用程序中设置Jest时,我也遇到了相同的错误。我不得不添加@babel/preset env
,它被修复了。我也写了一篇博客文章
npm i-D@babel/preset env
然后将其添加到.babelrc
文件的“预设”中。例如
{
"presets": ["@babel/react", "@babel/env"]
}
对于任何一个在这个问题上挣扎的人来说,上述答案都不适用于他/她
经过长时间的搜索,我找到了这个解决方案
编辑您的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$",
],
}
将要忽略的包放入[]
中,并通过|
将它们分开
//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]
我也缺少了babel-plugin-transform-es2015-destructuring
babel-plugin-transform-object-rest-spread
酶适配器-react-15
eslint-config airbnb
,太棒了。这是在第一次配置jest时发生的。我也经历过同样的情况。很高兴你的问题解决了。我也面临着同样的问题,@LeonGaban你提到了图书馆在那里失踪。。。你能告诉我是什么吗?@Anupammarya我在上面的评论中列出了它们。老实说,我不知道这是导致这个问题的原因