语法错误:意外令牌),带有Jest&反应本机 [英] Syntax Error: Unexpected Token ) w/ Jest & React Native

查看:150
本文介绍了语法错误:意外令牌),带有Jest&反应本机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带响应本机(v0.38.0)项目的jest(v19.0.2),但是当我运行jest命令时,出现以下错误:

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)

这是它抱怨的文件:

 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?

推荐答案

将近一年后我将回答这个问题,所以我无法验证此修复程序是否适用于那些版本,但是我遇到了与React Native相同的问题53,Jest 21和TypeScript 2.7

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. 您的package.json,将以下内容添加到您的jest配置中:
  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. 在您的根文件夹(请参见上面的路径)中,创建一个包含以下内容的preprocessor.js文件(请注意您现有的tsconfig.json文件的使用):
  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. 使用npm install --S react-native-typescript-transformer安装转换器库
  1. use npm install --S react-native-typescript-transformer to install the transformer library

这篇关于语法错误:意外令牌),带有Jest&amp;反应本机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆