Javascript “我如何解决?”;ReferenceError:未定义expect“;错误消息?
我正在尝试用mocha测试Javascript。我有一段代码:Javascript “我如何解决?”;ReferenceError:未定义expect“;错误消息?,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我正在尝试用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) }) }
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
․
0通过(5毫秒)
1失败
1) 数组indexOf()dovrebbe tornare-1 quando l'elemento nonèPresenter:
ReferenceError:未定义expect
在上下文中。(/Users/simonegentili/Desktop/Javascipt最佳实践/test/array.js:4:4)
在Test.Runnable.run(/usr/local/lib/node_modules/mocha/lib/Runnable.js:211:32)
在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
下一步(/usr/local/lib/node_modules/mocha/lib/runner.js:284:14)
at/usr/local/lib/node_modules/mocha/lib/runner.js:293:7
下一步(/usr/local/lib/node_modules/mocha/lib/runner.js:237:23)
at Object.\u onImmediate(/usr/local/lib/node\u modules/mocha/lib/runner.js:261:5)
在processImmediate[as_immediateCallback](timers.js:317:15)
试试看
首先,在候机楼
npm安装expect.js
在您的代码中:
var expect = require('expect');
摩卡是一个测试框架;您需要提供自己的断言库作为状态。因此,
expect
实际上是未定义的,因为您从未定义过它
(我推荐)
然后
(见Amit Choukroune的评论,指出实际上需要柴)
然后
如果您正在使用Mocha for TDD,请安装Expect.js或Chai.js
所以,在我的用例中,我通过
karma
运行摩卡咖啡规范。解决方案是为我的测试框架LIB安装karma集成:
npm install karma-mocha --save-dev
npm install karma-sinon-chai --save-dev
…还要将框架添加到我的karma.conf.js
:
module.exports = function(config) {
config.set({
browsers: ['Chrome'],
frameworks: ['mocha', 'sinon-chai'],
files: [
'.tmp/**/*.spec.js'
],
client: {
chai: {
includeStack: true
},
mocha: {
reporter: 'html',
ui: 'bdd'
}
}
})
}
希望这对其他人有所帮助。按照其他帖子的建议安装Chai后,使用es6语法,您应该将导入放在顶部
import {expect} from 'chai';
let chai=require('chai');
var assert=chai.assert;
描述('Array',function()){
描述(“#indexOf()”,函数(){
它('当值不存在时应返回-1',函数(){
assert.equal(-1[1,2,3].indexOf(4));
});
});
});
在html文件中添加此脚本标记
<script src="https://unpkg.com/expect@%3C21/umd/expect.min.js"></script>
为了在使用
chai
时在全局范围内公开expect
,应通过为首选测试库配置的方式加载以下内容:
require('chai/register-assert'); // Using Assert style
require('chai/register-expect'); // Using Expect style
require('chai/register-should'); // Using Should style
例如:
npx mocha--config.mocharc.js*.spec.js
创建一个文件chaiexpections.js并将expect声明为全局
'use strict';
// Configure chai
global.expect = require('chai').expect;
现在在cucumberopts下的配置文件中传递它,需要
cucumberOpts: {
require: ['./testsuites/*.js','./chaiexpections.js','./commons/hooks.js']
//tags: [],
strict: true,
format: [],
'dry-run': false,
compiler: [],
format: 'json:results.json',
},
这避免了在每个步骤定义中必须声明chai异常的开销。您应该使用
var expect=require('chai')。expect,
或const chai=require('chai')
在其上方。或者var expect=(typeof require=='undefined')?chai.expect:require('chai').expect
如果您希望在浏览器的页面上运行mocha中的测试。或者让{expect}=require('chai')
并从CLI使用mocha--require chai/register expect
require('chai/register-assert'); // Using Assert style
require('chai/register-expect'); // Using Expect style
require('chai/register-should'); // Using Should style
'use strict';
// Configure chai
global.expect = require('chai').expect;
cucumberOpts: {
require: ['./testsuites/*.js','./chaiexpections.js','./commons/hooks.js']
//tags: [],
strict: true,
format: [],
'dry-run': false,
compiler: [],
format: 'json:results.json',
},