-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing using jest broken since 0.14.0 #3999
Comments
I had the same problem. I solved it by removing Once removed, |
Hmm having done that I'm getting: "babel.Transformer is not a function". Oh - this I think is related to master though and babel 6 specifically. |
@admmasters Do you have any babel-plugin version 5 installed? |
I basically created a new react-native project, installed jest and get the following error (running Jest): Setup: Config:
/cc @cpojer |
Im having the same issue too, my settings are: |
As a quick-fix, if you have babel-core in your I guess the problem comes from here : https://github.com/facebook/react-native/blob/master/jestSupport/preprocessor.js#L29 |
This issue comes with npm 3.x as it try to flatten dependencies (see this article). You can resolve it by using the |
@ide I think you can close this issue 🎉 |
@ahanriat RN engine specs says anything greater than 4. https://github.com/facebook/react-native/blob/master/package.json#L11 Installing or upgrading node on OS X results in a borked jest. |
Have people been able to get their own tests working with jest? Happy to close this but would like to point people at a blog post or similar resource if there's one available. |
@ide I got Jest running on 0.16. NPM 2.x has to be installed. Won't work on vanilla NodeJS 5.x. The docs only recommend using 2.x:
http://facebook.github.io/react-native/docs/getting-started.html Babel also does not seem to be transforming properly. Doing |
@ide same here, managed to get it working by going back to NPM 2.x @masom I believe that's due to the change to Babel 6 in 0.16: it used to return the default export as both |
@gethinwebster In the app itself doing:
works but when executing with Jest something screws up the transform and this happens:
|
Odd - pretty much the opposite behaviour to the related issue I saw! Probably worth opening a new issue for that, as it sounds like it's related to Babel 6 in 0.16, rather than the earlier changes to preprocessor. |
Yeah there are a bunch of new technologies that got shipped pretty quickly and we didn't have enough time to figure all of this out in jest. We'll add better npm3 mocking support to jest "soon" and fix the issue with babel's imports. |
@masom Totally agree, as long as node 5.x is packaged by default with npm 3, there is a breaking change in the way node modules are arranged. At this point there are two solution:
|
@cpojer I think I narrowed it down to a bug in this babel plugin https://github.com/facebook/fbjs/blob/master/scripts/babel-6/inline-requires.js If you set |
The above issue about export default is unrelated to inline-requires. We are currently building a plugin to fix this but any help would be appreciated (we need someone to write a babel plugin :) ). The inline-require transform shouldn't be related to the export default issue. However, it might be an issue for your existing code. We transform require statements (not imports) to be inline, which might break some of your tests and you have to fix them up manually. We've been using this transform for a long time though, did this just now get shipped with react-native? |
@cpojer The script I mentioned is quite new. It's first commit is on the 7th of November. It is the babel-6 version of the script, which has some slight differences from the babel-5 one. I am using It might still be that somehow our test-code is not handling the inline-requires too well. Not sure. |
babel 6 changed how modules are imported. If you are using |
@cpojer that would be great! I'm already using the |
Can you share a module that isn't working for you? |
I am on RN v0.17, node v5.1.0 and npm v2.14.15. I still get |
@akshaysmurthy you should try with node 4.x.x (packaged with npm 2), this way you won't have this problem :) |
Summary: Related to #3999 There is an issue with the `preprocessor` script when using node >=5 as it uses npm 3. ~~There are (at least) two solutions, (I'm submitting the first one):~~ - ~~specify min required node version to 5.x and modify [this the preprocessor script](https://github.com/facebook/react-native/blob/0.16-stable/jestSupport/preprocessor.js#L29) to match npm 3 requirements~~ - ~~specifify node version >= 4.x and < 5.x and let the preprocessor script as it is~~ **EDIT**: Using `require.resolve` will do the trick ! -- Thank you guys for this amazing project by the way ;) Closes #4903 Reviewed By: svcscm Differential Revision: D2838759 Pulled By: androidtrunkagent fb-gh-sync-id: ebb12f225a519ea23afc4f013bb063a920193719
@akshaysmurthy I overcome this issue by modifying ./node_modules/react-native/jestSupport/preprocessor.js. |
@ahanriat I got @Anderson916's solution working, thanks! |
\o/ |
Summary: Related to facebook#3999 There is an issue with the `preprocessor` script when using node >=5 as it uses npm 3. ~~There are (at least) two solutions, (I'm submitting the first one):~~ - ~~specify min required node version to 5.x and modify [this the preprocessor script](https://github.com/facebook/react-native/blob/0.16-stable/jestSupport/preprocessor.js#L29) to match npm 3 requirements~~ - ~~specifify node version >= 4.x and < 5.x and let the preprocessor script as it is~~ **EDIT**: Using `require.resolve` will do the trick ! -- Thank you guys for this amazing project by the way ;) Closes facebook#4903 Reviewed By: svcscm Differential Revision: D2838759 Pulled By: androidtrunkagent fb-gh-sync-id: ebb12f225a519ea23afc4f013bb063a920193719
Summary: Related to facebook#3999 There is an issue with the `preprocessor` script when using node >=5 as it uses npm 3. ~~There are (at least) two solutions, (I'm submitting the first one):~~ - ~~specify min required node version to 5.x and modify [this the preprocessor script](https://github.com/facebook/react-native/blob/0.16-stable/jestSupport/preprocessor.js#L29) to match npm 3 requirements~~ - ~~specifify node version >= 4.x and < 5.x and let the preprocessor script as it is~~ **EDIT**: Using `require.resolve` will do the trick ! -- Thank you guys for this amazing project by the way ;) Closes facebook#4903 Reviewed By: svcscm Differential Revision: D2838759 Pulled By: androidtrunkagent fb-gh-sync-id: ebb12f225a519ea23afc4f013bb063a920193719
I have same issue , on React Native v0.19.0: Error is thrown:
For this source: import {combineReducers} from 'redux';
export default combineReducers({
time: (state = 0, action = {}) => {
switch (action.type) {
case SET_TIME:
return action.time;
case APPEND_TIME:
return state + action.time;
default:
return state;
}
},
}); It gets transpiled to: Object.defineProperty(exports,"__esModule",{value:true});exports.default=
(0,_redux.combineReducers)({
time:function(){var state=arguments.length<=0||arguments[0]===undefined?0:arguments[0];var action=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
switch(action.type){
case SET_TIME:
return action.time;
case APPEND_TIME:
return state+action.time;
default:
return state;}}}); ( But if I change Object.defineProperty(exports,"__esModule",{value:true});var _redux=require('redux');exports.default=
(0,_redux.combineReducers)({
time:function(){var state=arguments.length<=0||arguments[0]===undefined?0:arguments[0];var action=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
switch(action.type){
case SET_TIME:
return action.time;
case APPEND_TIME:
return state+action.time;
default:
return state;}}}); (See that UPD: same for RN |
The latest version of babel6 should have a way to add a separate transform pass, see: https://github.com/babel/babel/blob/master/CHANGELOG.md#a-traversal-per-preset-experimental can you try putting the inline require transform into a separate pass, after all the others and show he output? I'm currently working on fixing imports for jest 0.9 as well and hope this problem will go away. |
Patched react-native/packager/transformer.js: //if (options.inlineRequires) {
// extraPlugins.push(inlineRequires);
//}
config.plugins = resolvePlugins(extraPlugins.concat(config.plugins));
var babelConfig = Object.assign({}, babelRC, config);
babelConfig.passPerPreset = true;
babelConfig.presets = [ {"plugins":babelConfig.plugins}, {"plugins":[inlineRequires]} ];
delete babelConfig.plugins;
const result = babel.transform(src, babelConfig); Transpilled script, for same source as above: Object.defineProperty(exports,"__esModule",{value:true});exports.default=
(0,require('redux').combineReducers)({
time:function(){var state=arguments.length<=0||arguments[0]===undefined?0:arguments[0];var action=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];
switch(action.type){
case SET_TIME:
return action.time;
case APPEND_TIME:
return state+action.time;
default:
return state;}}}); But then ...
wtf?! this may be unrelated to this issue |
Just double checking, is |
No, I find other way to require it (for now):
Should I check |
I'll recommend doing that instead of disabling/enabling auto-mocking. [email protected] will be tagged soon as another beta for the new jest version. It will support proper transitive dependency unmocking for npm3, so that might also fix your issues. I'll tag it sometime this week :) |
I'd like to use:
But it returns mocked version of redux on my source. I see commit 7b44ca in Jest . Nice! Waiting for npm publish :) And... what about inlineRequires? |
@gethinwebster @ColCh is this issue still relevant? Let me know if we can close it in favor of a PR or more specific issue. |
My tests still break because of some |
I'm currently rewriting the documentation around Jest. The latest version of Jest and |
@jsierles my case works for me now |
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. |
The
jestSupport/scriptPreprocess.js
file that is recommended for use in testing (see https://facebook.github.io/react-native/docs/testing.html#jest-tests) was removed in 0.14.0:54b1402
Using
preprocessor.js
instead doesn't work, because it makes assumptions about the folder structure, and therefore wherebabel-core/package.json
can be found.Can scriptPreprocess be added back in, or is there now a different recommendation for running tests?
The text was updated successfully, but these errors were encountered: