-
Notifications
You must be signed in to change notification settings - Fork 48
Unable to use jest.mock
with Wallaby
#872
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
Comments
Interesting, if you change import * as example from './example';
import DateProvider from '../module/dateProvider';
jest.mock('../module/dateProvider'); to jest.mock('../module/dateProvider');
const example = require('./example')
const DateProvider = require('../module/dateProvider').default; then it starts working. Looks like |
In the Jest documentation, it mentions:
https://facebook.github.io/jest/docs/api.html#jestmockmodulename-factory-options Could this be related? |
Yes, it looks like the case. Wallaby instrumentation is probably breaking the hoisting behaviour. I'm looking into the ways to fix it. |
Ok, so I have investigated what exactly is going on. import * as example from './example';
import DateProvider from '../module/dateProvider';
{
jest.mock('../module/dateProvider');
} or import * as example from './example';
import DateProvider from '../module/dateProvider';
1, jest.mock('../module/dateProvider'); To make it work, the preset has to be applied during wallaby babel compilation. There're a few options to make it work.
plus process.env.NODE_ENV = 'wallaby'; in your wallaby config.
process.env.NODE_ENV = 'test';
const babelConfig = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, '.babelrc'))).env.test;
babelConfig.presets.push('babel-preset-jest');
// eslint-disable-next-line func-names
module.exports = function (wallaby) {
return {
...
compilers: {
'**/*.js*': wallaby.compilers.babel(babelConfig)
},
...
};
}; Whatever way you choose, it should only affect wallaby, |
Excellent. Thanks very much for looking into this and getting back to me On 17 Nov. 2016 15:19, "Artem Govorov" [email protected] wrote:
|
Forgot to mention one more way, probably the easiest one, with the only one change to wallaby config: ...
compilers: {
'**/*.js*': wallaby.compilers.babel({babelrc: true, presets: ['babel-preset-jest']})
}, |
When I add
Wallaby.js:
I have an issue I am trying to track down where the mocks in I am also using Babel 7. Edit: Issue may be jestjs/jest#6126 |
@gregorskii This doesn't look like Wallaby specific issue. Looks like it is Babel version mismatch and may indeed be related to the issue you've referenced. If Jest CLI is working for you, but Wallaby doesn't for the same project, please create a sample repo demonstrating the issue, we are happy to take a look and help. |
@ArtemGovorov yep jest CLI does work. It was related to this I can try to put together a simpler repo to demonstrate, this project is a client's project. Let me try a few more things and then Ill create the repo. Have you seen any issues with using the jest Seems like |
@ArtemGovorov here is a smaller fork of my project: https://github.com/gregorskii/wallaby-native-test When running wallaby I see a lot of issues with long execution:
At the end of it env variables that should be set by
Running
The value Config.TEST is set, which is defined in the The details of how On my actual project one of the things being supplied by the Config is the API URL. This results in the tests waiting a LOOONG time to timeout before completing. I will get to mocking this API so that the tests are not trying to call the API, but I have not gotten there yet. I need the tests to run in the same state across jest CLI and wallaby. Thank you. |
Thanks for providing the repo.
This is expected (Jest initialising react-native). If you run Jest cold start (without cache, for example by cloning your sample repo from the scratch), you'd get similar timings, even longer. However, once Wallaby.js has started, editing the same tests should be fast.
All you need to do is to add the |
Ah yes. That makes perfect sense. Will add the .env and instruct the team the delay in starting is expected (makes sense it would be). I was initiated thinking the delay was due to the missing env variable, but it does make sense it has to mount a lot to run react native code. Thank you! |
How am I supposed to workaround it with |
@meltedspark Since different questions had been raised/answered in this issue, could you please provide some more details about the issue that you have? Perhaps even create a separate issue - we are happy to help. |
The issue is exactly the same - when running with |
@meltedspark Could you please share some more details about your project:
|
Sure, here you go:
|
@meltedspark Thanks. I can not access the repo that you've listed (looks like it's private Wix repo), but your Wallaby with Jest and Typescript needs the following preprocessor to make Jest hoisting work: module.exports = wallaby => {
const config = require('yoshi/config/wallaby-jest')(wallaby);
config.workers = {recycle: false};
config.preprocessors = {
'**/*.js?(x)': file =>
require('babel-core').transform(file.content, {
sourceMap: true,
filename: file.path,
presets: ['babel-preset-jest']
})
};
return config;
}; I have also set |
Oops, I just noticed that I posted in the wrong issue ))) I meant to post in in yoshi's repository, there is an issue with the exact same title. facepalm. |
Issue description or question
When running tests in wallaby, modules mocked using
jest.mock
are imported rather than the requested mocked version.A minimal reproduction can be found at https://github.com/narthollis/minimal_jest_mock
Output from npm run -s test (
jest --no-cache --config jest.config.json
)Output from Wallaby console
Wallaby.js configuration file
Code editor or IDE name and version
WebStorm 2016.3
OS name and version
Windows 7
A Min
The text was updated successfully, but these errors were encountered: