我有一个看起来像这样的节点js模块
"use strict";
var debug = require('debug')('foo');
var Foo = function() {
this.x = 123;
debug("init");
};
module.exports = Foo;
我的测试看起来像这样
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);
});
});
但是当我用
运行时node node_modules/jest-cli/bin/jest.js
我得到了
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)
如何让jest忽略调试包,为什么当我告诉它不要进行模拟时呢?
答案 0 :(得分:3)
现在,jest 0.1.18(已修复here,记录为here)中存在一个错误,其中核心节点模块无法被模拟。
一旦facebook接受了拉取请求,这个问题就会消失。
在此之前,您可以将package.json指向修复程序的回购:
"jest-cli": "git://github.com/adaschevici/jest.git#cf4c6ff97d7009ff8627dd7d3a59cfeff1f3c8b8"
应解决该问题。
答案 1 :(得分:1)
发生这种情况的原因是因为jest.dontMock('debug');
只会阻止模拟调试模块的根文件,而所有其他内部结构仍然会被模拟。
一种可能的解决方案是使用unmockedModulePathPatterns
配置选项阻止模拟调试库:
{
"name": "...",
"version": "0.0.0",
...
"jest": {
"unmockedModulePathPatterns": ["/node_modules/debug"]
},
}
答案 2 :(得分:0)