【问题标题】:testing React Native Application with Jest - Jest encountered an unexpected token用 Jest 测试 React Native Application - Jest 遇到了一个意外的令牌
【发布时间】:2018-06-29 02:55:07
【问题描述】:

我尝试在 react native 中运行 jest 测试用例,但它总是失败。终端打印此错误“Jest 遇到了意外的令牌”。我真的很陌生,我已经尝试了几个小时来解决这个问题,但找不到办法。请帮我解决这个问题。

这是我的代码:-

import 'react-native';
import React from 'react';
import LoginScreen from '../pages/LoginScreen'

import renderer from 'react-test-renderer';

// snapshot testing
// update snapsshot to use npm test -- -u
// test('MainScreen snapShot', ()=>{
//     const snap = renderer.create(
//         <MainScreen />
//     ).toJSON();
//     expect(snap).toMatchSnapshot();
// });

it('fucntion test', () =>{
    let MainData = renderer.create(<LoginScreen />).getInstance();
    MainData.change(2)

    expect(MainData.change(2)).toEqual(20)
})

包.json:

{
  "name": "App_name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.3.1",
    "react-native": "0.55.4",
    "react-native-fcm": "^16.0.0",
    "react-native-loading-spinner-overlay": "^0.5.2",
    "react-native-looped-carousel": "^0.1.13",
    "react-native-navigation": "^1.1.469",
    "react-native-pathjs-charts": "0.0.34",
    "react-native-swiper": "^1.5.13",
    "react-native-vector-icons": "^4.6.0",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0"
  },
  "devDependencies": {
    "babel-jest": "23.0.1",
    "babel-preset-react-native": "4.0.0",
    "jest": "^23.1.0",
    "react-test-renderer": "16.3.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

错误是:

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:
        15 |
        16 | it('fucntion test', () =>{
      > 17 |     let MainData = renderer.create(<LoginScreen />).getInstance();
           |                                    ^
        18 |     MainData.change(2)
        19 |
        20 |     expect(MainData.change(2)).toEqual(20)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        6.807s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

谢谢。

【问题讨论】:

  • 也分享你的.babelrc文件
  • 对不起,我找不到 .babelrc 文件。
  • 它应该在根文件夹中
  • 您好,您找到解决方案了吗?

标签: react-native jestjs


【解决方案1】:

我看到了类似的东西,尝试在你的项目的根目录中创建一个.babelrc

{
  "presets": ["react-native"]
}

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2019-10-07
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2019-08-24
    • 2019-01-09
    相关资源
    最近更新 更多