javascript - Jest: automock modules, but only those defined in __mocks__, rather than all - Stack Overflow

TL;DRI would like to have some kind of automock feature enabled, but only (and only!) for the modules

TL;DR

I would like to have some kind of automock feature enabled, but only (and only!) for the modules that I have explicitly defined in a corresponding __mocks__ folder. Is there a way for this in Jest?

General advices and suggestions are also wele.

A bit of context: (optional)

Turns out I was totally misunderstanding Jests automock feature. Btw, looking back now, I don't understand why, 'cause docs are pretty clear on what it actually does:

This option tells Jest that all imported modules in your tests should be mocked automatically.

As if I just noticed ALL keyword. Maybe I was just thinking - but it doesn't makes sense to have an automock even for the imported function that I'm actually going to test here, does it? Like obviously I would like to automock third party stuff from node_modules, but not my own code. And it turns out that:

Note: Node modules are automatically mocked when you have a manual mock in place (e.g.: __mocks__/lodash.js).

Note: Core modules, like fs, are not mocked by default. They can be mocked explicitly, like jest.mock('fs')

So it's kind of doing the opposite of what I thought it was doing.

TL;DR

I would like to have some kind of automock feature enabled, but only (and only!) for the modules that I have explicitly defined in a corresponding __mocks__ folder. Is there a way for this in Jest?

General advices and suggestions are also wele.

A bit of context: (optional)

Turns out I was totally misunderstanding Jests automock feature. Btw, looking back now, I don't understand why, 'cause docs are pretty clear on what it actually does:

This option tells Jest that all imported modules in your tests should be mocked automatically.

As if I just noticed ALL keyword. Maybe I was just thinking - but it doesn't makes sense to have an automock even for the imported function that I'm actually going to test here, does it? Like obviously I would like to automock third party stuff from node_modules, but not my own code. And it turns out that:

Note: Node modules are automatically mocked when you have a manual mock in place (e.g.: __mocks__/lodash.js).

Note: Core modules, like fs, are not mocked by default. They can be mocked explicitly, like jest.mock('fs')

So it's kind of doing the opposite of what I thought it was doing.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 24, 2020 at 7:57 jayarjojayarjo 16.8k25 gold badges100 silver badges148 bronze badges 5
  • 1 What you want is the default in Jest out of the box, without changing its behavior. At least with create-react-app it behaves that way. – timotgl Commented Apr 24, 2020 at 8:19
  • Do you mean that in CRA, if you create a mock for the given module and place it in __mocks__ it will be included automatically instead of the original module, without any additional configuration? – jayarjo Commented Apr 24, 2020 at 8:46
  • @timotgl have you tried that on practice, or it's just an assumption? can you link corresponding doc on that? 'cause it's definitely not the case here. – jayarjo Commented Apr 24, 2020 at 9:25
  • I think you have misunderstood that functionality just as I did - modules from __mocks__ are not picked up automatically, one needs to explicitly call jest.mock('module_name') for it to be picked up. There's a dedicated warning on that here: jestjs.io/docs/en/manual-mocks#mocking-user-modules – jayarjo Commented Apr 24, 2020 at 9:40
  • Yeah you're right @jayarijo, I forgot about the manual jest.mock call. I'm as clueless as you then :D What you could do however is have an index.js that calls jest.mock on all your own mocks, and then only import this "mock loader" once in every test, instead of listing all the modules every time. – timotgl Commented Apr 25, 2020 at 9:11
Add a ment  | 

1 Answer 1

Reset to default 3

If I understand correctly, you have certain mock files in your __mocks__ folder which you would like to be globally replaced whenever they're needed as part of a test file imports.

Jest has a means of configuring that. If you go through the Jest Configuration Documentation, you'll find a property named moduleNameMapper which takes an object with the keys being regular expressions and the values being the paths for the mock for the corresponding modules which match the regex.

In your jest.config.js, you would need to add a separate parameter and specify the mock pattern and path.

moduleNameMapper: {
    "/^bootstrap/": "<root>/tests/__mocks__/bootstrapMock.js",
    "/^axios/": "<root>/tests/__mocks/axiosMock.js"
}

You can find more info about the usage in the Jest docs.

However, if you do not want to go through all this, this is also achievable by placing a __mocks__ folder next to your node_modules folder. However, as defined in the documentation for Manual Mocks:

If we want to mock Node's core modules (e.g.: fs or path), then explicitly calling e.g. jest.mock('path') is required, because core Node modules are not mocked by default.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745660814a4638824.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信