Jest:无法调用未定义的方法'split'

时间:2015-02-07 05:16:27

标签: jestjs

给出以下jest测试(testfile-test.js):

jest.dontMock('../testfile');

describe('testfile', function() {

    it('can write', function() {
        var fs = require('fs');
        var testfile = require('../testfile');

        testfile.write("something");
        expect(fs.writeFile).toBeCalled();
    });

});

在package.json中也有以下jest配置:

  "jest": {
    "testPathDirs": ["web"],
    "testDirectoryName": "__tests__",
    "unmockedModulePathPatterns": [
        "tty",
        "net",
        "debug",
        "send",
        "mime"
    ],
    "testPathIgnorePatterns": [
        "/node_modules/",
        "/dist/",
        "/.tmp/",
        "/web/bower_components/",
        "/web/components/"
    ]
  }

但运行此测试会产生以下结果:

Using Jest CLI v0.2.2
Waiting on 1 test...
 FAIL  web\sites\site_core\back_end\lib\storage\__tests__\testfile-test.js (0.669s)
● testfile › it can write
  - TypeError: c:\Users\Dev\git\KeystoneUI\web\sites\site_core\back_end\lib\storage\testfile.js: c:\Users\Dev\git\KeystoneUI\node_modules\keyston
e\index.js: c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\index.js: c:\Users\Dev\git\KeystoneUI\node_modules\keystone\n
ode_modules\express\lib\express.js: c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\lib\application.js: c:\Users\Dev\git\
KeystoneUI\node_modules\keystone\node_modules\express\lib\utils.js: c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\node_mod
ules\send\index.js: c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\node_modules\send\node_modules\mime\mime.js: Cannot call
 method 'split' of undefined
        at Mime.load (c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\node_modules\send\node_modules\mime\mime.js:55:23)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\node_modules\send\node_modules\mime\mime.js:90:6
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:921:17)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\node_modules\send\index.js:12:12
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:921:17)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\lib\utils.js:7:12
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader._generateMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:276:30)
        at Loader.requireMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:796:43)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:919:17)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\lib\application.js:6:15
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader._generateMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:276:30)
        at Loader.requireMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:796:43)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:919:17)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\lib\express.js:7:13
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader._generateMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:276:30)
        at Loader.requireMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:796:43)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:919:17)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\node_modules\express\index.js:2:18
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader._generateMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:276:30)
        at Loader.requireMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:796:43)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:919:17)
        at c:\Users\Dev\git\KeystoneUI\node_modules\keystone\index.js:4:12
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader._generateMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:276:30)
        at Loader.requireMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:796:43)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:919:17)
        at c:\Users\Dev\git\KeystoneUI\web\sites\site_core\back_end\lib\storage\testfile.js:1:83
        at Object.runContentWithLocalBindings (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\lib\utils.js:357:17)
        at Loader._execModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:245:9)
        at Loader.requireModule (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:900:12)
        at Loader.requireModuleOrMock (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\src\HasteModuleLoader\HasteModuleLoader.js:921:17)
        at Spec.<anonymous> (c:\Users\Dev\git\KeystoneUI\web\sites\site_core\back_end\lib\storage\__tests__\testfile-test.js:14:23)
        at jasmine.Block.execute (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\vendor\jasmine\jasmine-1.3.0.js:1065:17)
        at jasmine.Queue.next_ (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\vendor\jasmine\jasmine-1.3.0.js:2098:31)
        at null._onTimeout (c:\Users\Dev\git\KeystoneUI\node_modules\jest-cli\vendor\jasmine\jasmine-1.3.0.js:2088:18)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test failed, 0 tests passed (1 total)
Run time: 0.913s
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

如果我删除&#34; var testfile = require(&#39; ../ testfile&#39;);&#34;从测试它工作正常。为什么send / mime模块没有被模拟?

1 个答案:

答案 0 :(得分:0)

unmockedModulePathPatterns是您定义不想模拟see here的模块列表的地方,您好像不想模仿send,{{ 1}}