代码之家  ›  专栏  ›  技术社区  ›  user3142695

TypeError:Expect不是函数

  •  3
  • user3142695  · 技术社区  · 8 年前

    为什么我会得到一个 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)
        })
      })
    })
    
    2 回复  |  直到 8 年前
        1
  •  12
  •   James Monger    8 年前

    设置时 expect ,您需要执行以下操作:

    var expect = chai.expect
    

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

        2
  •  2
  •   Patrick Hund    8 年前

    您正在分配调用函数的结果 柴。预料 到您的变量 ,这没有意义。

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

    var expect = chai.expect;
    

    (不带括号)