-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Babel Jest Not Being used. #770
Comments
I literally just pushed an update that makes all of this better. I deprecated https://github.com/babel/babel-jest (and just pushed the README change, thanks for reminding me!) because it moved into this repository. Update to 0.9.0 and babel-jest 9.0.0 and it should be good. Switch the |
@cpojer I updated everything as per the new getting started docs but now when I run the tests, same code as above:
How is import not a thing, also why isnt |
I'm unsure what's going on. Can you paste your |
@cpojer I have kinda the same problem. Trying to write unit tests for react native. I use this script in package.json My test test case: // jest.unmock('../cool');
jest.autoMockOff();
import add from '../cool';
// import {sub} from '../cool';
describe('Cool', () => {
it('cool test', () => {
console.log('add', add); // console.log('sub', sub);
const resp = add(2, 3);
expect(resp).toBe(5);
});
}); cool.js: export default function add(a, b) {
return a+b;
}
export function sub(a, b) {
return a-b;
} console output:
I don't understand why the function gets mocked? (unmock doesn't work too) When I uncomment and try to import {sub}, I got a different error:
I also got Thanks for your work and I'm looking forward for more React-Native integration 👍 |
@AdamKyle would you mind posting your updated package.json? @ms88privat would you mind also pasting your package.json? Your directory structure would also be good to know, for example, where is your Also, please remove Please make sure you are using I'm also planning to update the docs on react-native testing soon. @ms88privat thanks for linking that page, I wasn't aware that react-native had their own page on testing, which is now outdated with |
@cpojer At the moment I don't have a I started this project yesterday completely from the ground up (with I put package.json: {
"name": "app",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"jest": {
"scriptPreprocessor": "node_modules/react-native/jestSupport/preprocessor.js",
"setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
],
"testFileExtensions": [
"js"
],
"unmockedModulePathPatterns": [
"promise",
"source-map"
]
},
"dependencies": {
"lodash": "^4.6.1",
"normalizr": "^2.0.0",
"react-native": "^0.21.0",
"react-native-router-flux": "^2.3.10",
"react-native-vector-icons": "^1.3.2",
"react-redux": "^4.4.0",
"redux": "^3.3.1",
"redux-diff-logger": "0.0.9",
"redux-logger": "^2.6.1",
"redux-saga": "^0.9.3",
"reselect": "^2.1.0"
},
"devDependencies": {
"babel-jest": "^9.0.0",
"eslint": "^2.3.0",
"eslint-config-standard": "^5.1.0",
"eslint-config-standard-react": "^2.3.0",
"eslint-plugin-promise": "^1.1.0",
"eslint-plugin-react": "^4.1.0",
"eslint-plugin-react-native": "^1.0.0",
"eslint-plugin-standard": "^1.3.2",
"jest-cli": "^0.9.0"
}
} I also had more babel dev dependencies installed (like babel-polyfill), but they didn't change a thing and so I had removed them again. EDIT: my folder structure: https://db.tt/iwUIjIeu Looking forward for your step by step guide with a fresh react-native project :) |
You have a custom setup for After you are done, I recommend installing the react-native preset: If you, for some reason, don't want to go through this, then you cannot use jest.unmock('Foo');
const Foo = require('Foo'); The reason this is, is because Yet another solution, if you are using Jest 0.9.0 and you don't want to use automocking, you can set Is that helpful for now? As said, there'll be more documentation soon. |
This is long been solved, apologies for not getting back to it. In another of my issues you saw the toy box package.json, thats why my json file looks like now and everything as we have both seen works :) |
@cpojer Thanks! It is working now. Wrap-up for other people who want to use it with react-native:
package.json: {
"name": "App",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
],
"testFileExtensions": [
"js"
],
"unmockedModulePathPatterns": [
"promise",
"source-map"
]
},
"dependencies": {
"react-native": "^0.21.0"
},
"devDependencies": {
"babel-jest": "^9.0.0",
"babel-polyfill": "^6.6.1",
"jest-cli": "^0.9.0"
}
} |
@cpojer I got another problem or maybe I misunderstand some concepts here (new to unit tests...) If I
Files to test:
(not working)
Edit: The same happens with the .babelrc and react-native preset. |
The react example ( https://github.com/facebook/jest/tree/master/examples/react ) I mentioned earlier does this with the correct setup and it works. I'm afraid I can't help you unless you push a repro somewhere on github that I can take a look at. I'm pretty sure this is some sort of configuration issue on your part. |
Oh, I think I know why this has happened: Jest uses caching, and when you added a |
@cpojer I'm not sure what I'm doing wrong, I installed .babelrc again with the presets and --no-cache, but it didn't solve my problem :( import/export is working unless I have another import statement in the file I imported for testing: test file in // cool-test.js
jest.unmock('../cool');
import TEST from '../cool';
describe ... is working with: // src/logic/cool.js
export default 'TEST'; not working with: // src/logic/cool.js
import something from 'something';
export default 'TEST'; Thanks for your patience with me... If you want to try it out, you could install a new react-native project with |
@cpojer here is my repo: https://github.com/ms88privat/react-native-jest-test |
thank you. That was very useful. The problem was that react-native was required but it wasn't being processed by |
Summary:This changes it so react-native always gets compiled properly. I thought long and hard about whether there should be an explicit whitelist, and yeah, probably there should be, but I don't want to overcomplicate things for now. One of the goals of Jest is to simplify the setup process, especially around Facebook's open source projects, and this helps greatly. New documentation for react-native testing is coming probably tomorrow. see #770. Closes #774 Differential Revision: D3017346 fb-gh-sync-id: e0c1444ce7c247fa3187ae024b38c81d19035f54 shipit-source-id: e0c1444ce7c247fa3187ae024b38c81d19035f54
@cpojer thanks for the quick turn around against @ms88privat problem, I downloaded her repo and upgraded
Is there something else that I may be missing? edit: sorry if ms88privat is not female. |
@nitsujri hey! Not sure if you are using react-native or react, but whichever one you are using needs to be part of your Sorry, I still didn't have enough time to polish up the docs for react-native :) |
@cpojer yep on native, so that was it! thanks! |
@cpojer +1 for those would-be-uber-helpful docs. Ran into a super basic Not asking for help, just req the docs 👍. We're totally cool waiting a few days for them. Thanks for all the awesomeness from Singapore. |
Yep, working on it as hard as I can! :) |
Apologies if I've overlooked the answer but was there a solution to this @nitsujri?
I've hit the same issue and couldn't discern from reading the conversation whether there was a straight forward fix? I have a React Native project with following
|
@logicalicy sadly not yet, I'm excitedly waiting for docs the from @cpojer. |
The fix is probably going to look something like this as part of the "jest" config in "haste": {
"defaultPlatform": "ios",
"platforms": [
"ios",
"android"
],
"providesModuleNodeModules": [
"react-native"
]
} It may not work with 0.10.0 yet, but there will be point releases in which I'll make it work soon. |
@cpojer I followed what you mentioned above and notice that the jest setup is similar in the f8app repo. It fixes my Any word on when this will be covered in the docs? Just noticed that the last post in this thread was almost a month ago and wondering if I should just skip trying to get these tests working in the meantime. Edit: |
Also stuck on this issue with |
I have this same issue with jest-cli 14.0.
package.json: "jest": {
"automock": false,
"moduleFileExtensions": ["js", "jsx"],
"moduleDirectories": ["node_modules"],
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react/",
"<rootDir>/node_modules/react-dom/"
]
} .babelrc {
"presets": [
"es2015",
"stage-1",
"react"
],
"plugins": [
"transform-runtime"
]
}
How is babel-jest installed in such a way that it doesn't have to be mentioned in any config? EditI added ❯ npm run jest --no-cache -s
Using Jest CLI v14.0.0, jasmine2, babel-jest
PASS __tests__/sum-test.js (0.271s)
Running 1 test suite...Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
PASS __tests__/CheckboxWithLabel-test.js (0.778s)
2 tests passed (2 total in 2 test suites, run time 1.9s) Double edit
|
I was able to identify the problem. It was related to {
"presets": [
"react",
[
"es2015",
{
"loose": true,
"modules": false
}
]
]
} I have to figure out how to fix that for production while Jest still work. |
I just ran into this same issue. I fixed it another way. The parsing error I was running into was on module related stuff like Turns out I had disabled "presets": [
["es2015", {"modules": false}]
] To work around it, I added a "env": {
"test": {
"presets": [
"es2015"
]
}
} I didn't need to use |
I'm having a problem possibly related to this where I can't destructure exports from a file that has:
in the test when I'm
x is undefined where as if I do:
it works correctly. Any ideas ? My .babelrc is:
I have installed babel-jest, babel-polyfill, jest etc... Appreciate any help or suggestions, can provide more details |
Same problem here.., my babelrc is exactly the same
and I've installed everything mentioned in docs
tried every solution in this thread (install I just tried mocha, with |
I also have this issue. It's very mind boggling, because I have setup 2 repos and while first one works, second does not. |
Same issue right now and exact configuration as @CodinCat , I'll post if I find a solution. UPDATE: Did the following and it started working:
It appears unlinking the current folder and reinstalling |
I was having a similar issue when I realized I had an |
This issue is frustrating as I think its a environment specific issue. 2 machines, 1 is affected by this and the other is not. On the affected machine, I've been trying to completely remove node and reinstall but the issue not going away. |
Having this error over and over, very annoying. Here is my output from running }
Test Suites: 1 failed, 1 total package.json with current transformIgnorePatterns package.json with node_modules ignored
Test Suites: 1 failed, 1 total Any ideas and help would be appreciated, sorry can't provide github link since private repo but we are currently moving from angular typescript to react typescript. Thank you in advance. |
@JMStudiosJoe I think it might be because you modified the transform config see second note so babel-jest wont load |
@jamedranoa can you provide a better example for the transform option, since I have tried many different combinations now and even the one the compiler was saying to use does not work. |
@JMStudiosJoe May I ask how your preprocessor file looks like? this might help #1466 |
@jamedranoa thank you for fast responses first of all, and my transform option is now working
|
@JMStudiosJoe ahh ok this might be related kulshekhar/ts-jest#121 |
@jamedranoa Looks like it and their fix seems to be working now for the import error but then get another error complaining about my .tsx syntax and rendering my html
Says line 36 when that is not true the line containing the return(<div is further down in the render method. Is this a babel translation issue and my tsx files not compiling to js properly? |
@JMStudiosJoe would you mind sharing your current jest config? do you have set "jsx": "react" on TS_CONFIG? |
@jamedranoa my current tsconfig.json along with jest in package.json "globals": { |
@JMStudiosJoe looks like when you specify |
back to my favorite import error... FAIL react/tests/test_catalogs_component.ts
Test Suites: 1 failed, 1 total is this right file to be using for jest? at e (../../../.nvm/versions/node/v6.3.1/lib/node_modules/jest/node_modules/jest-runtime/build/transform.js:320:12) ?? UPDATE: in package.json added jest
Test Suites: 1 failed, 1 total I believe I have 2 tests in my test file contrary to what the error is saying import React from 'react'; import BuyerCatalogDashboard from '../src/views/BuyerCatalogDashboard/BuyerCatalogDashboard'; describe('', () => { UPDATE AGAIN jest
Test Suites: 1 failed, 1 total |
@JMStudiosJoe What node version do you have, >4? try changing the target config to ES5 if that's the case. try copying all your compilerOptions and putting them directly on the
|
@jamedranoa my node is 6.3.1 tsconfig is now looking like and.... same error ran with --debug option this time here is output: jest --debug
Test Suites: 1 failed, 1 total |
UPDATE from above and now getting new errors which is progress, IMO I felt that my previous import errors was a translation issue between ts and js and adding this transform seemed to help with the reading of import in js files but now getting a weird error in my test for trying to render my component npm t -- --no-cache
FAIL react/tests/test_catalogs_component.ts
Test Suites: 1 failed, 1 total code for test: import React from 'react'; import BuyerCatalogDashboard from '../src/views/BuyerCatalogDashboard/BuyerCatalogDashboard'; describe('', () => { |
@jamedranoa I think I got things to work, now to make sure I can write meaningful tests. here is my configuration and this is my currently working tests, not sure how meaningful they are but its not complaining (yet) import * as React from 'react'; import BuyerCatalogDashboard from '../src/views/BuyerCatalogDashboard/BuyerCatalogDashboard';
}); @jamedranoa thanks again for all your help, found the transform solution from this repo |
FWIW, my issue was simply that my setup framework script file name was "jest.config.js" in the root directory... and that's the automatic configuration filename for Jest 🤕 🥇 Moving and renaming it to setup.js fixed it. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Seems the Babel-Jest Project hasn't been touched in 4 months so Ill post this issue here.
Consider the following code:
Consider the following test:
Now consider the following package.json:
And the .babel-rc
Finally the result from the test:
Not sure how import is an unexpected token, also if you notice, babel-jest isnt being used in the compile step:
Using Jest CLI v0.8.2, jasmine1
The text was updated successfully, but these errors were encountered: