【问题标题】:TypeError: Expect is not a functionTypeError:Expect 不是函数
【发布时间】:2017-10-31 10:01:06
【问题描述】:

为什么我在运行这个测试文件时得到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)
    })
  })
})

【问题讨论】:

    标签: javascript unit-testing mocha.js chai


    【解决方案1】:

    设置expect时,需要这样做:

    var expect = chai.expect
    

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

    【讨论】:

      【解决方案2】:

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

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

      var expect = chai.expect;
      

      (不带括号)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 2019-12-20
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多