I've got a node js module that looks like this
"use strict";
var debug = require('debug')('foo');
var Foo = function() {
this.x = 123;
debug("init");
};
module.exports = Foo;
And my test looks like this
jest.dontMock('../lib/foo');
jest.dontMock('debug');
describe('footest', function() {
it('checks the foo', function() {
var Foo = require('../lib/foo');
var foo = new Foo();
expect(foo.x).toBe(123);
});
});
But when I run jest with
node node_modules/jest-cli/bin/jest.js
I get
Found 1 matching tests...
FAIL __tests__/foo-test.js (0.02s)
? footest › it checks the foo
- TypeError: /Users/gregg/src/jest-test/lib/foo.js: /Users/gregg/src/jest-test/node_modules/debug/node.js: Cannot read property 'buffer' of undefined
at Socket.self [as bytesWritten] (net.js:688:8)
at _getMetadata (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/moduleMocker.js:279:49)
at _getMetadata (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/moduleMocker.js:286:23)
at _getMetadata (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/moduleMocker.js:279:27)
at _getMetadata (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/moduleMocker.js:279:27)
at Object.module.exports.getMetadata (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/moduleMocker.js:388:20)
at Loader._generateMock (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:280:56)
at Loader.requireMock (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:782:43)
at Loader.requireModuleOrMock (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:897:17)
at /Users/gregg/src/jest-test/node_modules/debug/node.js:6:11
at Object.runContentWithLocalBindings (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/utils.js:309:17)
at Loader._execModule (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
at Loader.requireModule (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:879:10)
at Loader.requireModuleOrMock (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:899:17)
at /Users/gregg/src/jest-test/lib/foo.js:3:13
at Object.runContentWithLocalBindings (/Users/gregg/src/jest-test/node_modules/jest-cli/src/lib/utils.js:309:17)
at Loader._execModule (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
at Loader.requireModule (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:879:10)
at Loader.requireModuleOrMock (/Users/gregg/src/jest-test/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:899:17)
at Spec.<anonymous> (/Users/gregg/src/jest-test/__tests__/foo-test.js:7:14)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
How do I get jest to ignore the debug package and why does it appear to be making a mock when I told it not to?
Does console log work in jest? Jest by default prints all console. log (warnings, errors, etc) messages to the console. That's great – it helps you understand what's going on in your code when tests run.
WebStorm makes it easier to debug Node. js applications. You can put breakpoints right in your JavaScript or TypeScript code so you no longer need any debugger and console.
To debug in Google Chrome (or any Chromium-based browser), open your browser and go to chrome://inspect and click on "Open Dedicated DevTools for Node", which will give you a list of available node instances you can connect to.
Right now there is a bug in jest 0.1.18 (being fixed here, documented here) where core node modules cannot be ignored from being mocked.
Once the pull request is accepted by facebook, this problem should go away.
Until then, you can point your package.json to the repo of the fix:
"jest-cli": "git://github.com/adaschevici/jest.git#cf4c6ff97d7009ff8627dd7d3a59cfeff1f3c8b8"
which should resolve that issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With