Jest: Doc on mock inconsistent with actual behavior

Created on 17 Feb 2017  ยท  7Comments  ยท  Source: facebook/jest


Do you want to request a feature or report a bug?

Bug

What is the current behavior?

__mocks__ are not loaded by default.

$> git clone https://github.com/facebook/jest.git
$> cd examples/manual_mocks
$> yarn

Edit the __tests__/FileSummarizer-test.js and comment line 5 (jest.mock('fs')).

$> yarn test

And it fails.

What is the expected behavior?

The mock is loaded properly as stated in the documentation.
This is the sentence I'm referring to in particular: However, manual mocks will take precedence over node modules even if jest.mock('moduleName') is not called.

Or am I understanding something wrong?

Most helpful comment

Well, mocking relative modules definitely works but you have to be explicit about it.
Which the documentation only implies, and that's where the confusion arises (at least to me).

Different parts of the doc seem to indicate different things.

If you have this fs tree :

.
โ”œโ”€โ”€ models
โ”‚ย ย  โ”œโ”€โ”€ __mocks__
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ user.js
โ”‚ย ย  โ””โ”€โ”€ user.js
โ””โ”€โ”€ specs
    โ””โ”€โ”€ models
        โ””โ”€โ”€ user.spec.js

4 directories, 3 files

Then in your `specs/models/user.spec.js' you can use the mock by doing the following:

// models/__mocks__/user.js
module.exports = 'MOCKED'

// models/user.js 
module.exports = 'NOT MOCKED'

// specs/models/user.spec.js
const modelPath = '../../models/user'
jest.mock(modelPath)

const User = require(modelPath)

describe('some test', () => {
   it('does something', () => {
      expect(User).toEqual('MOCKED')
   })
})
) ./node_modules/.bin/jest
 PASS  specs/models/user.spec.js
  some test
    โœ“ does something (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.878s, estimated 1s
Ran all test suites.

I just wish the doc was clearer on this.

All 7 comments

This doesn't happen for core modules, like fs. You have to manually call jest.mock('fs') on them.

@cpojer I set up a small repo so you can try this with a non-core module here and I beg to differ.

Besides :

This doesn't happen for core modules, like fs. You have to manually call jest.mock('fs') on them.

The documentation doesn't state that.

@cpojer Sorry to ping you again but can you please take a look at https://github.com/Shahor/jest-error-example ?

@cpojer The documentation should state that __mocks__ works only with node_modules, it would be less misleading I think. Because @Shahor tried to use it to mock relative module in his example which definitely doesn't work. Also the documentation doesn't state in the manual mocks page that core modules are not mocked by default and that you should use jest.mock(). I think that this note should be added to it, since not everyone is going there. It could save a lot of time to people that are new to Jest.

They don't exclusively work for node modules. They also work for haste modules which is Facebook's custom module system.

Well, mocking relative modules definitely works but you have to be explicit about it.
Which the documentation only implies, and that's where the confusion arises (at least to me).

Different parts of the doc seem to indicate different things.

If you have this fs tree :

.
โ”œโ”€โ”€ models
โ”‚ย ย  โ”œโ”€โ”€ __mocks__
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ user.js
โ”‚ย ย  โ””โ”€โ”€ user.js
โ””โ”€โ”€ specs
    โ””โ”€โ”€ models
        โ””โ”€โ”€ user.spec.js

4 directories, 3 files

Then in your `specs/models/user.spec.js' you can use the mock by doing the following:

// models/__mocks__/user.js
module.exports = 'MOCKED'

// models/user.js 
module.exports = 'NOT MOCKED'

// specs/models/user.spec.js
const modelPath = '../../models/user'
jest.mock(modelPath)

const User = require(modelPath)

describe('some test', () => {
   it('does something', () => {
      expect(User).toEqual('MOCKED')
   })
})
) ./node_modules/.bin/jest
 PASS  specs/models/user.spec.js
  some test
    โœ“ does something (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.878s, estimated 1s
Ran all test suites.

I just wish the doc was clearer on this.

Was this page helpful?
0 / 5 - 0 ratings