Reporting a bug
Using
What is the current behavior?
Tests fail with
TypeError: require(...) is not a function
at Object.self.help (/Users/tmcw/src/zeroarg/node_modules/yargs/lib/usage.js:155:30)
at Object.self.showHelp (/Users/tmcw/src/zeroarg/node_modules/yargs/lib/usage.js:381:15)
at Object.Yargs.self.showHelp (/Users/tmcw/src/zeroarg/node_modules/yargs/yargs.js:712:11)
at /Users/tmcw/src/zeroarg/node_modules/yargs/yargs.js:1046:18
at Array.forEach (native)
at Object.Yargs.self._parseArgs (/Users/tmcw/src/zeroarg/node_modules/yargs/yargs.js:1041:27)
at Object.Yargs.self.parse (/Users/tmcw/src/zeroarg/node_modules/yargs/yargs.js:533:23)
tested-module.test.js
jest.dontMock('yargs');
var path = require('path');
var testedModule = require('./tested-module');
test('empty', () => {
testedModule( '--help')
});
tested-module.js
var yargs = require('yargs');
function testedModule(input) {
yargs.help();
yargs.usage(input);
}
module.exports = testedModule;
What is the expected behavior?
Tests would be able to load and use the yargs module. The failed line is this dynamic require in yargs
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Current jest config is:
"jest": {
"automock": false
}
Can you let us know what console.log(require.resolve('cliui')) in your test outputs? This seems like an odd test. Do you have a repository that we can install and see this error?
@tmcw ping.
Sorry for the delay, will get a minimal test case this week.
Closing, as this doesn't seem to affect more folks. I'll reopen if it still happens and you're still interested in providing a repro :)
Hi, I have similar problem, have created minimal example accessible here:
jest_import_bug
I would like to use jest for unit and also integration testing in my node.js project, but cannot get this going. When running in jest environment (npm test), library codependency inside node_modules is trying to require module {exports: , filename: 'mock.js', id: 'mockParent'}, it looks like some mocked object created by jest? This object does not have .require function, but jest should have automocking disabled by default and I also have turned it off in package.json, am I missing something here?
TypeError: baseMod.require is not a function
at realRequire (node_modules/codependency/index.js:76:18)
at requirePeer (node_modules/codependency/index.js:284:10)
at Object.<anonymous> (node_modules/le_node/lib/logger.js:948:15)
at Object.<anonymous> (logger.js:3:177)
at Object.<anonymous> (require.test.js:1:117)
at Generator.next (<anonymous>)
at new Promise (<anonymous>)
at Generator.next (<anonymous>)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Thanks in advance for reply
@krutijan1 Looks like something does module.parent in your code. That should be fixed in #4614, can you install jest@test an verify?
Can confirm in test project I posted it works! But introduced in my real project (jest: ^21.3.0-beta.4) still same issue. Wiped out npm_modules, cleared cache. Any advice how could I find out what is going on? What could have impact on this behaviour?
Looks like something does module.parent in your code.
My transpiled code from babel does not show any usage of .parent call (but it is a build from babel cli, test are run by jest on non transpiled code), so maybe this behaviour is caused by some node_module introduced in dependencies?
It can be in a dependency, yes. That said, module.parent is implemented in that beta version, so there's some other issue there. Can you create a new repro?
Seeing this too when trying to use le_node which uses codependency to optionally require different winston versions. Will update if I get a reproducible sample I can share.
le_node creates this situation for us too.
Also having this problem with le_node
Can somebody create a reproducible example? The example posted previously (https://github.com/krutijan1/jest_import_bug) is fixed on Jest master (install jest@test to see).
EDIT: https://github.com/Wizcorp/codependency/pull/14#issuecomment-337151852 is also fixed
le_node presents this problem for me too
Do you have a reproduction?
I ran into the same issue while exercising this code kata.
Here is how to reproduce the bug:
git clone https://github.com/iilei/mars-rover-kata.git
cd mars-rover-kata
git checkout tags/v0.0.1 -b jest_issue_3419
npm install
npm run tdd src/index.spec.js
Interestingly, it doesn't occur when running npm run test.
I am guessing it might be a side effect of jest --watch 馃
Most helpful comment
Seeing this too when trying to use le_node which uses codependency to optionally require different
winstonversions. Will update if I get a reproducible sample I can share.