javascript - Jest with React Native getting issue - Stack Overflow

I'm getting error when trying to run test casescode.i am using react native with jest . all was

I'm getting error when trying to run test cases code.i am using react native with jest . all was working fine before upgrade 0.40 . now is 0.42 all my test cases stop working and getting error following error.

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component, Children, PropTypes } from 'react';
                                                                                             ^^^^^^
    SyntaxError: Unexpected token import

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/AppRegistryInjection.js:3:22)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/SiblingsManager.js:3:27)

here is my .babelrc code

{
    "presets": [
        "react-native"
    ],
    "plugins": [
        "transform-decorators-legacy"
    ]
}

what is issue I am not getting .

I'm getting error when trying to run test cases code.i am using react native with jest . all was working fine before upgrade 0.40 . now is 0.42 all my test cases stop working and getting error following error.

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component, Children, PropTypes } from 'react';
                                                                                             ^^^^^^
    SyntaxError: Unexpected token import

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/AppRegistryInjection.js:3:22)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/SiblingsManager.js:3:27)

here is my .babelrc code

{
    "presets": [
        "react-native"
    ],
    "plugins": [
        "transform-decorators-legacy"
    ]
}

what is issue I am not getting .

Share Improve this question edited Oct 11, 2017 at 5:07 Sport asked Mar 31, 2017 at 9:22 SportSport 8,9936 gold badges49 silver badges66 bronze badges 2
  • What version of Node are you using? node -v in terminal will help. – Dan Commented Mar 31, 2017 at 9:54
  • v6.9.5 why is this related to node – Sport Commented Mar 31, 2017 at 10:04
Add a ment  | 

2 Answers 2

Reset to default 4

I ran into very same problem with 0.42. I banged my head until I found a solution piece by piece.

You need to to write ignores in package.json. Example of mine:

"jest": {
        "preset": "react-native",
        "setupFiles": [
            "<rootDir>/src/config/jest.js"
        ],
        "transformIgnorePatterns": [
            "<rootDir>/(node_modules)/(?!react-native|react-navigation|bugsnag-react-native)"
        ],
        "transform": {
            "^.+\\.js$": "<rootDir>/node_modules/babel-jest"
        }

And my /config/jest.js looks like:

jest.mock('Linking', () => ({
  addEventListener: jest.fn(),
  removeEventListener: jest.fn(),
  openURL: jest.fn(),
  canOpenURL: jest.fn(),
  getInitialURL: jest.fn().mockImplementation(() => new Promise((resolve) => resolve()))
}));

jest.mock('mobx-react/native', () => require('mobx-react/custom'));
jest.mock('react-native-mixpanel', () => ({
  sharedInstanceWithToken: jest.fn(),
  trackWithProperties: jest.fn()
}));
jest.mock('bugsnag-react-native', () => ({
  Client: jest.fn(),
  Configuration: jest.fn()
}));

I'm not guaranteed this solves all of your problems directly. However the idea is to ignore all the "evil-doers" (react-native-root-siblings in your case), thus avoiding such error messages.

I got solution

"jest": {
        "preset": "react-native",
        "transformIgnorePatterns": [
          "node_modules"
        ],
        "coveragePathIgnorePatterns": [
          "node_modules"
        ],
        "modulePathIgnorePatterns": [
          "node_modules"
        ]
      },

and in my devDependencies I have added "react-addons-test-utils", "react-dom" ,

"devDependencies": {
    "babel-core": "^6.17.0",
    "babel-eslint": "^7.2.1",
    "babel-jest": "^19.0.0",
    "babel-loader": "^6.2.5",
    "babel-plugin-transform-decorators": "^6.13.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2017": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-react-native": "^1.9.1",
    "babel-preset-stage-0": "^6.16.0",
    "enzyme": "^2.8.0",
    "jest": "^19.0.2",
    "npm": "^4.4.4",
    "react-addons-test-utils": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-test-renderer": "^15.4.2"
}

thats solved all issue .

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745148882a4613773.html

相关推荐

  • javascript - Jest with React Native getting issue - Stack Overflow

    I'm getting error when trying to run test casescode.i am using react native with jest . all was

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信