TypeError: Expect is not a function

TypeError: Expect is not a function

为什么我在 运行 运行此测试文件时得到 TypeError: expect is not a function

我已经在本地安装了 mocha 和 chai,并且 运行 通过 yarn run test 进行了测试,运行 只是 "test": "mocha"

var chai = require('chai')
var expect = chai.expect()

describe('Array', function () {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      expect([1, 2, 3].indexOf(4)).to.be.equal(-1)
    })
  })
})

设置expect时,需要这样做:

var expect = chai.expect

您正在使用 () 计算函数,这是不正确的 according to the documentation

您将调用函数 chai.expect 的结果分配给您的变量 chai,这没有意义。

相反,您需要为此函数分配一个引用,如下所示:

var expect = chai.expect;

(没有括号)