admin管理员组文章数量:1026989
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 calljest.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
1 Answer
Reset to default 3If 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.
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 calljest.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
1 Answer
Reset to default 3If 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.
本文标签:
版权声明:本文标题:javascript - Jest: automock modules, but only those defined in __mocks__, rather than all - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660812a2161878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论