我想用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)
var expect = require('chai').expect
。 - Amit Choukrounconst chai = require('chai')
。 - chovyvar expect = (typeof require === 'undefined') ? chai.expect : require('chai').expect;
。请注意,这不是一个解释,而是对原文进行的简洁翻译。 - Michiel Rooslet { expect } = require('chai')
- Gershom Maes