Allow mocking `require.resolve`
See original GitHub issueš Feature Proposal
The require.resolve function is re-created for every file: this is needed so that it starts resolving from the correct location.
However, this makes it impossible to mock it: require.resolve = jest.fn() doesnāt work.
I propose adding an API similar to jest.mock specifically for require.resolveās behavior.
Motivation
We have different tests in @babel/core that test plugins/presets aliasing (ref). For example, we test that when someone uses @babel/env in their config Babel tries to resolve @babel/preset-env.
We currently rely on āfakeā node_modules folders, however:
- We donāt want to test nodeās resolution algorithm, we should only test what we are asking node to resolve.
- Using real FS just to check plugins/presets aliasing makes our tests slower.
- This approach doesnāt work with Yarn PnP, because it changes the resolution behavior (and it shouldnāt affect our tests).
Example
Maybe something like this?
jest.mockResolution("@babel/env", __dirname + "/fake/@babel/preset-env");
// test
jest.unmockResolution("@babel/env");
Pitch
Mocks are already handled by the Jest core platform š
Issue Analytics
- State:
- Created 4 years ago
- Reactions:15
- Comments:25 (13 by maintainers)
Top Results From Across the Web
Mocking and testing require.resolve calls in jest - Stack Overflow
I created my own resolver like so: const glob = require('glob'); let mapping = {}; // Looks for "moduleĀ ...
Read more >How to mock a dependency in a Node.js, and why you should ...
As a good start ā let's test simple module. The SkyNet. import React, { PropTypes } from 'react'; import destroyAllHumans from 'very-actions';
Read more >[Solved]-Mocking and testing require.resolve calls in jest-node.js
I created my own resolver like so: const glob = require('glob'); let mapping = {}; // Looks for "module-resolution.json" files in all the...
Read more >Manual Mocks - Jest
In order to mock properly, Jest needs jest.mock('moduleName') to be in the same scope as the require/import statement. Here's a contrivedĀ ...
Read more >mock-require JavaScript and Node.js code examples - Tabnine
const mock = function (path, obj) { return mockRequire(resolve(path), obj);
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The feature has been accepted, so no need to try to convince us š PR very much welcome!
Hey guys ! I know thatās not exactly what youāre looking for, but hereās my workaround to be able to āmockā
require.resolve. Thereās an interesting feature in jest allowing you to define your own module resolver: https://jestjs.io/docs/en/configuration#resolver-stringHaving that in mind, here is, basically, what Iāve done:
modules-resolution.jsonfiles in my test folders that look like this:This do the job for me so far and I think that, starting from this example, we could do something more complex but more developer friendly ! Of course, that would be even better if this feature could be directly included in jest ! Anyway, I hope this will help some of you š