![](/img/trans.png)
[英]How can i solve [Vue warn]: Error in data(): “ReferenceError: total is not defined” in Laravel Vue
[英]How can I solve “ReferenceError: expect is not defined” error message?
我正在尝试用 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.org/#assertions所述。 因此, expect
确实是未定义的,因为您从未定义过它。
(我推荐柴)
npm install chai
那么
(见 Amit Choukroune 的评论指出实际上需要柴)
那么
var expect = chai.expect;
试试
首先,在终端
npm install expect.js
在你的代码中:
var expect = require('expect');
按照其他帖子的建议安装 Chai 后,使用 es6 语法,您应该将导入放在顶部
import {expect} from 'chai';
let chai = require('chai'); var assert = chai.assert; describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { assert.equal(-1, [1, 2, 3].indexOf(4)); }); }); });
在我的用例中,我通过karma
运行 mocha 规范。 解决方案是为我的测试框架库安装 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'
}
}
})
}
希望这对其他人有帮助。
为了在全球公开expect
,在使用chai
,应通过配置为您首选的测试库加载以下内容:
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
如果您使用 Mocha 进行 TDD,请安装 Expect.js 或 Chai.js
所以,做npm install expect
或npm install chai
在 html 文件中添加这个脚本标签
<script src="https://unpkg.com/expect@%3C21/umd/expect.min.js"></script>
或安装包
npm install chai or expect
创建文件 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 异常的开销
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.