柴:无法读取未定义的 属性 'not'

chai: Cannot read property 'not' of undefined

我是 chaimocha 的新手,我将示例代码用于我的第一个测试用例。这是我的代码。

var chai = require("chai");
var mocha = require("mocha");
var expect = chai.expect;

describe("Test", function() {
    it("Test", function() {
        expect([1, 2]).to.be.an('array').that.does.not.include(3);
    });
});

我运行mocha test.js

结果是:

TypeError: Cannot read property 'not' of undefined

我怎么了?似乎.does return未定义。我删除 .does 并且它工作正常。正确的用法是什么?

以下代码有效。

expect([1, 2]).to.be.an('array').that.not.include(3);

如果我 运行 你的代码使用 Chai 4,它就可以工作。当我将它降级到 Chai 3 时,我得到了你得到的错误。 does 在 Chai 4.0.0 中作为空操作断言添加。您正在使用早于 4.0.0 的 Chai 版本。

如果您查看 releases 信息,您会发现版本 4.0.0 是这样的:

Add does and but as new no-op assertion. (Related Issues: #700, #339 PRs: #621, #701)

(您可以在 Github release 中找到相同的信息,还有一个好处是问题编号是指向实际问题的链接。)