Syntax Error: Unexpected Token ) w/ Jest & React Native

非 Y 不嫁゛ 提交于 2019-12-24 02:25:39

问题


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:

  1. 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"
        },
    }
}

```

  1. in your root folder (see path above), create a preprocessor.js file with the following contents (note the use of your existing tsconfig.json file):

```

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;
    },
};

```

  1. use npm install --S react-native-typescript-transformer to install the transformer library


来源:https://stackoverflow.com/questions/43529402/syntax-error-unexpected-token-w-jest-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!