如何解决"ReferenceError:期望未定义"?错误信息? [英] How can I solve "ReferenceError: expect is not defined" error message?

查看:137
本文介绍了如何解决"ReferenceError:期望未定义"?错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Mocha测试Javascript.我有以下代码段:

I am trying to test Javascript with mocha. I've this snippet of code:

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已安装

and a test/array.js file. Mocha was installed with

$ 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确实是未定义的,因为您从未定义它.

Mocha is a test framework; you need to provide your own assertion lib as https://mochajs.org/#assertions states. Thus, expect is indeed undefined because you never defined it.

(我建议)

npm install chai

然后

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

(see Amit Choukroune's comment pointing out to actually require chai)

然后

var expect = chai.expect;

这篇关于如何解决"ReferenceError:期望未定义"?错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆