更新我的RN版本后,测试文件不再通过。以下是一个失败的测试和相应的错误:
import React from 'react'
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer'
import RelatedCallsListItem from
'../app/components/Phoning/relatedCallsListItem'
import {shallow} from 'enzyme'
const openItem = {
dueDate: '07/03/18',
subType: 'Follow-up',
subject: 'Test subject',
comment: 'Test comment',
dueTime: '',
date: '',
result: '',
isOpen: 'yes'
}
const completedItem = {
dueDate: '07/03/18',
subType: 'Follow-up',
subject: 'Test subject',
comment: 'Test comment',
dueTime: '',
date: '',
result: 'AppointmentMade',
isOpen: 'no'
}
it('renders properly', () => {
const item = openItem
expect(renderer.create(
<RelatedCallsListItem item={item}/>
)).toMatchSnapshot()
})
it('notices the call is still open and there is a subType', () => {
const item = openItem
const test = shallow(
<RelatedCallsListItem item={item}/>
)
console.log('test: ', test.debug())
expect(test.find('Text[testID="subType"]').prop('children')).toEqual('FOLLOW-UP')
})
it('notices the call is completed and there is a result', () => {
const item = completedItem
const test = shallow(
<RelatedCallsListItem item={item}/>
)
console.log('test: ', test.debug())
expect(test.find('Text[testID="result"]').prop('children')).toEqual('APPT MADE')
})
这是我尝试运行它时遇到的错误:
● Test suite failed to run
Jest encountered an unexpected token
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://facebook.github.io/jest/docs/en/configuration.html
Details:
SyntaxError: /Users/fri0729/Desktop/PostingPlus/posting-plus/PostingPlus/node_modules/react-native/Libraries/StyleSheet/StyleSheet.js: Unexpected token (18:12)
16 | const flatten = require('flattenStyle');
17 |
> 18 | import type {
| ^
19 | ____Styles_Internal,
20 | ____DangerouslyImpreciseStyle_Internal,
21 | ____DangerouslyImpreciseStyleProp_Internal,
at Parser.raise (node_modules/@babel/core/node_modules/babylon/lib/index.js:776:15)
at Parser.unexpected (node_modules/@babel/core/node_modules/babylon/lib/index.js:2079:16)
at Parser.expectContextual (node_modules/@babel/core/node_modules/babylon/lib/index.js:2047:41)
at Parser.parseImport (node_modules/@babel/core/node_modules/babylon/lib/index.js:5205:12)
at Parser.parseStatementContent (node_modules/@babel/core/node_modules/babylon/lib/index.js:4043:27)
at Parser.parseStatement (node_modules/@babel/core/node_modules/babylon/lib/index.js:3962:17)
at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/core/node_modules/babylon/lib/index.js:4513:23)
at Parser.parseBlockBody (node_modules/@babel/core/node_modules/babylon/lib/index.js:4500:10)
at Parser.parseTopLevel (node_modules/@babel/core/node_modules/babylon/lib/index.js:3938:10)
at Parser.parse (node_modules/@babel/core/node_modules/babylon/lib/index.js:5304:17)
我已经看到一些在我的package.json文件中添加忽略模式的解决方案,但是这些修复程序并未成功。测试运行良好,直到我更新了RN版本以及相应的jest和babel软件包。
答案 0 :(得分:0)
对于那些为此问题苦苦挣扎的人,此链接提供了部分修复:
React Native - Jest: Broken since update to 0.56. How to fix it?
从那里开始,您可能必须更新某些测试,因为更新Babel会更改其更新中的语法。