问题
I'm attempting to use jest (v19.0.2) w/ my react native (v0.38.0) project however when I run the jest command I'm receiving the following error:
● Test suite failed to run
/Users/kyledecot/code/root-react-native/node_modules/react-native/jest/setup.js:40
)
^
SyntaxError: Unexpected token )
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:106:13)
Here's the file it's complaining about:
30 .mock('ReactNativeDefaultInjection')
31 .mock('Image', () => mockComponent('Image'))
32 .mock('Text', () => mockComponent('Text'))
33 .mock('TextInput', () => mockComponent('TextInput'))
34 .mock('Modal', () => mockComponent('Modal'))
35 .mock('View', () => mockComponent('View'))
36 .mock('ScrollView', () => mockComponent('ScrollView'))
37 .mock(
38 'ActivityIndicator',
39 () => mockComponent('ActivityIndicator'),
40 )
41 .mock('ListView', () => {
42 const RealListView = require.requireActual('ListView');
43 const ListView = mockComponent('ListView');
44 ListView.prototype.render = RealListView.prototype.render;
45 return ListView;
46 })
47 .mock('ListViewDataSource', () => {
48 const DataSource = require.requireActual('ListViewDataSource');
49 DataSource.prototype.toJSON = function() {
50 function ListViewDataSource(dataBlob) {
Has anyone else ran into this bug or know how I would go about fixing it?
回答1:
I'm answering this nearly a year later so I can't verify if this fix would work on those versions but I ran into the same issue with React Native 53, Jest 21 and TypeScript 2.7
My solution has 3 parts:
- your
package.json, add the following to your jest config:
```
{
jest: {
"moduleFileExtensions": ["ts", "tsx", "js", "json"],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.(ts|tsx)$": "<rootDir>/preprocessor.js"
},
}
}
```
- in your root folder (see path above), create a
preprocessor.jsfile with the following contents (note the use of your existingtsconfig.jsonfile):
```
const tsc = require('react-native-typescript-transformer');
const tsConfig = require('./tsconfig.json');
module.exports = {
process(src, filename) {
if (filename.match(/\.png/)) {
return '';
}
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
return tsc.transform(src, filename, tsConfig.compilerOptions);
}
return src;
},
};
```
- use
npm install --S react-native-typescript-transformerto install the transformer library
来源:https://stackoverflow.com/questions/43529402/syntax-error-unexpected-token-w-jest-react-native