jest icon indicating copy to clipboard operation
jest copied to clipboard

functionality like `mock-require` to map moduleName to different module path

Open anilreddykatta opened this issue 9 years ago • 2 comments

Do you want to request a feature or report a bug? feature What is the current behavior? Currently, I have the following setup.

import { get } from 'utils/xhr'
// Above module can resolve to `utils/xhr.c` or `utils/xhr.g` depending on `NODE_ENV`
// But 'utils/xhr' itself doesn't exist.

How do I mock utils/xhr module to point to utils/xhr.c or utils/xhr.g depending on NODE_ENV.

I tried jest.setMock & jest.mock , but in vain. JEST always tries to resolve module(tries to point to module name indicated in es6 or require. If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior? I expect it to provide a feature like mock-require where I can point map one module to other module

const mock = require('mock-require')
mock('utils/xhr', 'utils/xhr.c') // it works!

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

anilreddykatta avatar Apr 27 '17 18:04 anilreddykatta

Got it! by using virtual: true option while mocking resolved this!

anilreddykatta avatar Apr 27 '17 19:04 anilreddykatta

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar May 13 '21 17:05 github-actions[bot]