Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get jest to ignore the fairly common `debug` node package?

Tags:

jestjs

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?

like image 519
gman Avatar asked Jun 14 '14 05:06

gman


People also ask

Does console log work in Jest?

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.

Which node module helps debug node applications?

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.

How do I debug Jest in Chrome?

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.


1 Answers

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.

like image 92
duereg Avatar answered Nov 22 '22 21:11

duereg