gpt4 book ai didi

javascript - 如何解决 "ReferenceError: expect is not defined"错误消息?

转载 作者:IT老高 更新时间:2023-10-28 21:52:17 24 4
gpt4 key购买 nike

我正在尝试用 mocha 测试 Javascript。我有这段代码:

describe('Array', function() {
describe('indexOf()', function() {
it("dovrebbe tornare -1 quando l'elemento non è presente", function() {
expect([1,2,3].indexOf(4)).to.equal(-1)
})
})
})

和一个 test/array.js 文件。 Mocha 安装了

$ npm install -g mocha

当我运行时

$ mocha

我收到此错误:

$ mocha


0 passing (5ms)
1 failing

1) Array indexOf() dovrebbe tornare -1 quando l'elemento non è presente:
ReferenceError: expect is not defined
at Context.<anonymous> (/Users/simonegentili/Desktop/Javascipt Best Practice/test/array.js:4:4)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:358:10)
at /usr/local/lib/node_modules/mocha/lib/runner.js:404:12
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:284:14)
at /usr/local/lib/node_modules/mocha/lib/runner.js:293:7
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:237:23)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:317:15)

最佳答案

Mocha 是一个测试框架;您需要提供自己的断言库为 https://mochajs.org/#assertions状态。因此,expect 确实是未定义的,因为您从未定义过它。

(我推荐chai)

npm install chai

然后

(参见 Amit Choukroune 的评论指出实际上需要 chai)

然后

var expect = chai.expect;

关于javascript - 如何解决 "ReferenceError: expect is not defined"错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com