如何解决“ReferenceError: expect is not defined”错误信息?

编程入门 行业动态 更新时间:2024-10-04 11:16:21

如何解决“ReferenceError: expect is not defined”<a href=https://www.elefans.com/category/jswz/34/1771053.html style=错误信息?"/>

如何解决“ReferenceError: expect is not defined”错误信息?

我正在尝试用 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
文件。摩卡安装了

$ 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/#assertions 所述。因此,

expect
确实是未定义的,因为您从未定义过它。

(我推荐chai)

npm install chai

然后

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

然后

var expect = chai.expect;

更多推荐

如何解决“ReferenceError: expect is not defined”错误信息?

本文发布于:2024-05-30 21:32:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误信息   如何解决   expect   ReferenceError   defined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!