Jest: Jest 11 & react-native : using react-native bundled preprocessor and env

Created on 20 Apr 2016  Â·  13Comments  Â·  Source: facebook/jest

Hi,

I'm using Jest to unit test some custom components, and it's working well with some of them.
I saw a few times the preprocessor and env in https://github.com/facebook/react-native/tree/master/jestSupport was used like this in some projects' package.json file:

    "scriptPreprocessor": "node_modules/react-native/jestSupport/preprocessor.js",
    "setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",

However, when I do that I get an error about a missing Module: ErrorUtils :

Runtime Error
Error: Cannot find module 'ErrorUtils' from 'ErrorUtils.js'
    at Runtime._resolveNodeModule (/Users/slamus/dev/platform-app/node_modules/jest-cli/src/Runtime/Runtime.js:449:11)
    at Object.<anonymous>.ErrorUtils.applyWithGuard.mockImplementation (/Users/slamus/dev/platform-app/node_modules/fbjs-haste/stubs/__mocks__/ErrorUtils.js:12:23)
    at Object.<anonymous> (/Users/slamus/dev/platform-app/node_modules/react-native/jestSupport/env.js:22:28)

I'm using react-native 0.23.1 and jest 11.0.2

I've did rm -rf node_modules && npm cache clean && npm install
and my node version is v5.9.1

Are these preprocessors useful? And if yes, how can I fix that ?

Thanks a lot :)

Most helpful comment

@cliedeman the correct solution is to provide a new setup file and config example for Jest that works well with react-native. If you'd like to send a PR to react-native and fix the docs, that would be fantastic and well appreciated!

All 13 comments

Can you try adding this: https://github.com/fbsamples/f8app/blob/master/package.json#L51-L60 to your Jest config? That should work.

The docs in react-native about Jest testing are unfortunately entirely outdated and no one is bothering to update them :(

I have a working sample here

Its pretty rough, I just used it to make sure I could get some fetch async tests working that also retrieve values from AsyncStorage. Also it only works with react-native >= 0.22

@slamus In short the preprocessor is not needed if you use the correct .babelrc and have babel-jest setup correctly but you do need an env like the one I created.

@cpojer I copied some code from react native to get the env working - particularly the NativeModules and an empty implementation for ErrorUtils. Looks like react-native uses this during its bootstrap process.
Is there a cleaner way to do this?

Thanks
Ciaran

@cliedeman the correct solution is to provide a new setup file and config example for Jest that works well with react-native. If you'd like to send a PR to react-native and fix the docs, that would be fantastic and well appreciated!

if @cliedeman doesn't have time for this, I could take a stab at putting together the docs. Thanks for the clarification on the setup. Testing RN apps could use a lot more documentation.

I agree! I'm sorry we are a bit behind on the documentation – we are committed to make Jest the best testing tool for react-native but there are a couple of fires burning at FB at all times that I need to help put out, so this is moving a bit slowly in open source :)

no problem! I realized that might have come across harsh, didn't intend for that at all. I've been working on a RN project for the last few months and have found it to be very powerful, and most of my issues have involved my own misunderstandings of the underlying mobile platform. really, great work on the product.

given that, where would be a good place for constructive feedback such as testability?

I would like to have a guide next to the React guide on the Jest website that explains the React Native setup well so that we don't have to go over to the react-native repo to fix the testing docs for Jest. If you have ideas on how to improve Jest's support for react-native, feel free to send as many PRs as necessary – I'm happy to help as much as I can :)

Awesome, I'm about to start on a new project next week so I'll make sure and document all the things. Thanks!

Any updates on this?
I added what's in https://github.com/fbsamples/f8app/blob/master/package.json#L51-L60 to my jest config, still getting this:
Error: Cannot find module 'ErrorUtils' from 'env.js'

Same problem here, jest tests are super slow without this config in the package.json, but getting this ErrorUtils issue. Please update the doc, thanks

How is this still an issue. Not being able to get Jest up and running for react-native makes it essentially worthless at this stage. I've tried following both the facebook docs and the prescriptions on this thread to no avail.

For the record: this issue still happens on Jest v13.2.3 with react-native 0.29.0

Thank you for your patience. We launched Jest 14.0 with experimental react-native support:

Please feel free to create new issues after trying out the new integration if any issues remain.

Was this page helpful?
0 / 5 - 0 ratings