0

我有一个mixin/Utils.js要测试的地方underscore,模块中需要和使用的地方。就像是:

var _ = require('underscore');
var Utils = {

   foo: function(arrayOfArray) {
    return _.sortBy(arrayOfArray, function(array) {

      return -1 * array[1].length;

    }) || {};


   }

};
module.exports = Utils;

当我尝试使用 Jest 对其进行测试时,我做了如下的事情。但是测试失败了。我有一种感觉,因为我没有嘲笑underscore. 但是我应该如何嘲笑呢?一般来说,如果模块具有类似的依赖项underscore,我应该如何正确设置模拟并测试模块?

 var __path__ = "PATH_TO/mixins/Utils.js";


 jest.dontMock(__path__);



 describe("Test for mixins/Utils.js", function() {

    var Utils;

    beforeEach(function() {
    Utils = require(__path__);
});

   describe("countInversion", function() {

    it('Passing in [[0, [1,2]], [1, [1,2,3]]] and should get [[1, [1,2,3]]],[0, [1,2]] ', function()        {
        var testInput = [[0, [1,2]], [1, [1,2,3]]];
        var expectedOutput = [[1, [1,2,3]]],[0, [1,2]]; 
        expect(Utils.FOO(testInput)).toEqual(expectedOutput);
    }); 
});

});

4

1 回答 1

2

Jest 默认模拟所有内容,因此如果您的单元测试依赖于下划线中的功能,您实际上不想模拟下划线。您应该添加jest.dontMock('underscore')到您的测试中,或者将其包含在unmockedModulePathPatterns您的 jest 配置的属性中以获得您期望的输出。

编辑

正如@pgericson 在评论中指出的那样,自 jest 15 起,Jest 不再自动运行Jasmine 间谍可以用来代替 automocks。

于 2014-12-08T00:30:03.940 回答