Skip to content
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

Automocking disabled by default but Jest warns about unmocking a manual mock #1552

Closed
GrigoryPtashko opened this issue Sep 1, 2016 · 15 comments

Comments

@GrigoryPtashko
Copy link

After upgrading to Jest 15 with all config options set to defaults it gives warning that I'm unmocking a manual mock.

I've got a manual mock for Relay in ___mocks__ and in the integration tests I want to use the real Relay. So I go with:

jest.unmock('react-relay');

import React from 'react'; // eslint-disable-line import/imports-first
import Relay from 'react-relay'; // eslint-disable-line import/imports-first
import TestUtils from 'react-addons-test-utils'; // eslint-disable-line import/imports-first

...

And get the warning which in my view must not be:

 PASS  __tests__/Me-test-int.js
  ● Console

    console.warn node_modules/jest-runtime/build/index.js:647
      jest.unmock('react-relay') was called but automocking is disabled. Remove the unnecessary call to `jest.unmock` or enable automocking for this test via `jest.enableAutomock();`. This warning is likely a result of a default configuration change in Jest 15.

      Release Blog Post: https://facebook.github.io/jest/blog/2016/09/01/jest-15.html

Here's my config

  "jest": {
    "scriptPreprocessor": "jestPreprocessor.js",
    "moduleNameMapper": {
      "^.+\\.(scss|gif)$": "<rootDir>/src/styleMock.js"
    }
  }

Am I misunderstanding something?

@ferrannp
Copy link
Contributor

ferrannp commented Sep 1, 2016

In Jest 15, the option automock is set to false by default. If you have a mock of Relay in __mocks__, it will only use that mock in the tests you call jest.mock('react-relay'). See the docs:

When a manual mock exists for a given module, Jest's module system will use that module when explicitly calling jest.mock('moduleNmae').

So, if I understood correctly, you can safely remove that line jest.unmock('react-relay'); since it will already be unmocked.

@GrigoryPtashko
Copy link
Author

No. When I remove jest.unmock('react-relay'); I get the exception

 FAIL  __tests__/Me-test-int.js
  ● Test suite failed to run

    TypeError: _reactRelay2.default.DefaultNetworkLayer is not a constructor

      at Object.<anonymous> (__tests__/Me-test-int.js:9:1)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)
      at process.emit (events.js:191:7)
      at process.nextTick (internal/child_process.js:719:12)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

Which means that Jest is actually using my manual mock.
Basically, this is the reason I created the issue.

@cpojer
Copy link
Member

cpojer commented Sep 1, 2016

Can you explain what you are trying to do a bit better? The example code above, is that from your manual mock? If so, you shouldn't call jest.unmock there but rather const Relay = require.requireActual('Relay').

@GrigoryPtashko
Copy link
Author

GrigoryPtashko commented Sep 1, 2016

No, it did not help. Look:

// jest.unmock('react-relay');

import React from 'react'; // eslint-disable-line import/imports-first
const Relay = require.requireActual('react-relay'); // eslint-disable-line import/imports-first
import TestUtils from 'react-addons-test-utils'; // eslint-disable-line import/imports-first

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('http://localhost:10000/q')
);

...

The output is

> jest "Me"

 FAIL  __tests__/Me-test-int.js
  ● Test suite failed to run

    TypeError: Relay.DefaultNetworkLayer is not a constructor

      at Object.<anonymous> (__tests__/Me-test-int.js:9:26)
      at process._tickCallback (internal/process/next_tick.js:103:7)

Test Summary
 › Ran all tests matching "Me".
 › 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 3.407s)
npm ERR! Test failed.  See above for more details.

Here's what I am trying to achieve.

  • My tests combine both unit tests and integration tests. In unit tests of my components there are Relay containers and thus I created the file __mocks__/react-relay.js:
const Relay = require.requireActual('react-relay');

module.exports = {
  QL: Relay.QL,
  Mutation: Relay.Mutation,
  Route: Relay.Route,
  createContainer: (component) => component,
};

So this way my components which have Relay containers can be tested good.

  • At the same time some of the tests are the integration tests where Relay must not be mocked. But it seems like Jest is always mocking the Relay because of my manual mock albeit the default option for automocking is disabled. So I see two options here:

-- The first one going with jest.unmock('react-relay'); which make Jest warn as I described in the original post.
-- The second one that you suggested const Relay = require.requireActual('react-relay');. But it makes Jest throw as I showed.

Does it make sense?

Sorry, but I'm going to repeat. Going with the jest.unmock('react-relay'); actually gives the result that I need - both unit tests with mocked Relay and integration tests with real Relay pass. It is Jest that gives me the warning.

@cpojer
Copy link
Member

cpojer commented Sep 1, 2016

Can you create a repo with this so I can take a look? I'm not really sure why this is happening and can't give hints without the full setup.

@GrigoryPtashko
Copy link
Author

GrigoryPtashko commented Sep 1, 2016

@cpojer yes, sure. Here it is https://github.com/GrigoryPtashko/jest-relay-integration-test.
Just npm install and npm test and you'll see the warning.

Thanks.

@cpojer
Copy link
Member

cpojer commented Sep 2, 2016

Ah, this might be a real issue with Jest 15. Can you try, as a workaround, to call:

jest.mock('react-relay', () => require.requireActual('react-relay'))

and let me know if that works?

@GrigoryPtashko
Copy link
Author

No, that did not help.
Here's the output.

> jest

 PASS  __tests__/SignIn-test.js
 FAIL  __tests__/Me-test-int.js
  ● Test suite failed to run

    TypeError: _reactRelay2.default.DefaultNetworkLayer is not a constructor

      at Object.<anonymous> (__tests__/Me-test-int.js:9:1)
      at emitTwo (events.js:106:13)
      at process.emit (events.js:191:7)
      at process.nextTick (internal/child_process.js:719:12)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

@cpojer
Copy link
Member

cpojer commented Sep 2, 2016

Thanks for the repro. I'd recommend for now to call jest.mock('react-relay').unmock('react-relay'). That should be the default in Jest but it seems like this is an actual bug.

@GrigoryPtashko
Copy link
Author

Yes, this ways it works.

@difelice
Copy link

difelice commented Oct 1, 2016

@cpojer What would be the fix for this bug? I mean, should we start adding manual .mock( even if now they are currently added automatically? Thanks.

@cpojer
Copy link
Member

cpojer commented Oct 2, 2016

Yeah the workaround I mentioned above is still recommended until we change how this works in Jest.

@delijah
Copy link

delijah commented Oct 9, 2016

Having this issue as well. Any plans for a fix?

@cpojer
Copy link
Member

cpojer commented Oct 28, 2016

Will be fixed in the next release. #2022

@cpojer cpojer closed this as completed Oct 28, 2016
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants