Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test runner (jest) failing to import expo modules

I'm writing some component tests for a React Native app. I'm using expo's BarCodeScanner for one of my components. In my jest test, I have a line that says import { BarCodeScanner } from 'expo';. This line alone (without anything that uses it further down in my test code) causes the following error:

The Expo SDK requires Expo to run. It appears the native Expo modules are unavailable and this code is not running on Expo. Visit https://docs.expo.io to learn more about developing an Expo project.

Anybody have any idea on how I can import the BarCodeScanner to my test file?

like image 506
thisissami Avatar asked Jul 30 '18 19:07

thisissami


1 Answers

I did a bit of digging and it appears that this is a common problem with Expo and Jest. There are a couple of issues open currently related to Jest Tests:

https://github.com/expo/expo/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+jest

One solution here sparked my interest:

https://github.com/expo/expo/issues/1705

It appears if you create your own Jest setup file and "require()" the expo component in there and downgrade expo and expo-jest to v26 it works. Example from the link above:

package.json:

...
"jest": {
    "preset": "jest-expo",
    "setupTestFrameworkScriptFile": "./setupJest"
},
...

setupJest.js:

require('stacktrace-parser');

Hope this helps you some bit. Let me know how you get on or if you have any queries and i will dig a bit more if possible :)

like image 127
ShaneG Avatar answered Nov 14 '22 09:11

ShaneG