question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should this work with react native?

See original GitHub issue

I’m using jest and react native. I can test it by manually compiling the typescript into js and running jest on that. Trying to use this project I get

/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)

My relevant package.json

  "jest": {
    "preset": "react-native",
    "setupFiles": [
      "./jest/setup.js"
    ],
    "transform": {
      ".(ts|tsx)": "./node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ]
  }

I’ve read through the limitations but those don’t seem to apply to me, not targeting ES6 ect. Using node v6.9.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
felipemsantanacommented, Jan 3, 2017

I managed to make it work, install these modules:

yarn add -D babel-jest babel-preset-react-native

Create a .babelrc file with this content:

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

And finally in package.json, use this transform:

"transform": {
  "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
  ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
}
3reactions
bufkecommented, Jan 3, 2017

Cool that works thank you @fmatoss

So I guess the answer to can ts-jest work with react-native is yes but with some changes. As far as I can tell that includes

  • Import like this import { create } from "react-test-renderer"; not like import renderer from "react-test-renderer";
  • yarn add -D babel-jest babel-preset-react-native
  • Ensure .bablerc contains "presets": ["react-native"]
  • Ensure package.json contains
"transform": {
  "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
  ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Use React Native for Your Mobile App? - STX Next
With React Native, you create one codebase that works on both Android and iOS. And it doesn't just “work”—it compiles to native Java...
Read more >
Why You Should (or Shouldn't) Use React Native
There's a powerful argument to be made that React Native comes closer to mimicking native apps than any of its current competitors. The...
Read more >
React Native: What Is It and When Should You Use It?
React Native is based on React, one of the most popular JavaScript frameworks in the world, so knowledge of JavaScript is essential. Whilst ......
Read more >
The Good and the Bad of ReactJS and React Native - AltexSoft
React Native uses ReactJS as the JavaScript library, so it has all its advantages. To create a React Native app across platforms, developers ......
Read more >
What is React Native, and when to use it for your mobile App?
React Native is a JavaScript library based on ReactJS, which we use to build mobile apps using iOS and Android platforms. The advantage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found