茉莉花期望不匹配数组结果

jasmine expect not matching array result

本文关键字:结果 数组 不匹配 期望 茉莉花      更新时间:2023-09-26

我正在尝试以下代码:

describe("array deletion", function () {
    it("leaves a hole in middle", function () {
        var array = ['one','two','three'];
        delete array[1]; //'two' deleted
        expect(array).toEqual(['one',undefined,'three']);
    });
});

这种期望失败了,但为什么?它不应该相等吗?

JavaScript 中有 3 个元素的数组与只有 2 个元素的数组之间存在差异,其中一个元素是undefined。例如

var a = [1,2,3];
delete a[1];
a.forEach(function(x) { console.log(x); });
// writes 1 3
[1,undefined,3].forEach(function(x) { console.log(x); })
// writes 1 undefined 3

1 in a
// returns false
1 in [1,undefined,2]
// returns true

源总是在你这边,如果你看toEquals匹配器代码,你会发现它使用了以下源文件中eq函数(它有点长,所以我只提供链接,底部是比较对象和数组的部分:https://github.com/jasmine/jasmine/blob/79206ccff5dd8a8b2970ccf5a6429cdab2c6010a/src/core/matchers/matchersUtil.js)。