最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin28浏览0评论

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

如何解决“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;
发布评论

评论列表(0)

  1. 暂无评论