2

Code I'm working with:

// redux-reducer.js

import { combineReducers, loop, Effects } from 'redux-loop'

import { loginStart } from './actions'

import {
  signUp,
  login,
  refreshAccessTokenStart,
  replayAction,
  postConfirmationToken,
  postRequestForPasswordReset,
  postNewPassword
} from './effects'
...
...
...

// redux-reducer-test.js

import combinedReducer from './reducers'

beforeEach(() => {
  require.requireMock('effects') // something like this
})
...
...
...

The problem is that the local ./effects module calls react native environment config scripts.

I thought to mock the ./effects module in such a way that ./effects doesn't get executed.

Things that haven't yet worked:

  • using the require.requireMock('effects') method
  • letting it be mocked automagically
  • using proxyquire: I thought proxyquire.noCallThru() would prevent ./effects being run. Seems proxyquire isn't compatible with Jest, and that there should(?) somehow be this functionality within Jest?

Interested to hear your thoughts and learn more about Jest!

Pete
  • 35
  • 1
  • 9

2 Answers2

0

Not yet in the docs, this worked for me:

From Jest blog about v15 changes:

Automocking is now disabled by default in Jest. This is by far the most confusing feature for new users and in many ways it doesn't make sense for small projects. We introduced automocking at Facebook and it worked great for us when unit testing was adopted in a large existing code base with few existing tests, but over time it felt like people spent more time fighting with mocked/unmocked modules than it would have taken them to write a test normally. We also noticed that library authors often require a huge number of basic modules that always have to be manually unmocked. Even for Jest itself we realized that the majority of tests had automocking disabled manually. We still believe that explicit automocking can be incredibly valuable. This change simply trades implicit mocks for explicit mocks via calls to jest.mock(moduleName).

If you would still like to use automocking by default, enable the automock setting in your configuration or manually call jest.enableAutomock() in your test or setup file.

Automocking now works with my package.json jest config as below:

  "jest": {
    "preset": "jest-react-native",
    "testRegex": "\\.test\\.js$",
    "setupFiles": [
      "./setupJest.js"
    ],
    "automock": true
  },

Seems docs are incomplete, keep an eye on the blog!

Pete
  • 35
  • 1
  • 9
0

It's in the docs, here: Jest Docs

automock [boolean]

Default: false

This option is disabled by default. If you are introducing Jest to a large organization with an existing codebase but few tests, enabling this option can be helpful to introduce unit tests gradually. Modules can be explicitly auto-mocked using jest.mock(moduleName).

Community
  • 1
  • 1
Brian
  • 331
  • 1
  • 6
  • 19