74

我正在尝试用 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)
4

10 回答 10

97

Mocha 是一个测试框架;您需要提供自己的断言库,如https://mochajs.org/#assertions状态。因此,expect确实是未定义的,因为您从未定义过它。

(我推荐

npm install chai

然后

(见 Amit Choukroune 的评论指出实际上需要 chai)

然后

var expect = chai.expect;
于 2013-10-04T21:57:03.493 回答
11

尝试

一、在终端

npm install expect.js

在您的代码中:

var expect = require('expect');
于 2013-10-04T21:56:02.313 回答
8

按照其他帖子的建议安装 Chai 后,使用 es6 语法,您应该将导入放在顶部

import {expect} from 'chai';
于 2017-04-17T15:41:36.770 回答
3
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));
    });
  });
});
于 2018-10-23T05:52:15.207 回答
1

In my use case, I was running a mocha spec through karma. The solution was to install the karma integrations for my test framework libs:

npm install karma-mocha --save-dev
npm install karma-sinon-chai --save-dev

...and also to add the frameworks to my 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'
            }
        }
    })
}

Hope this helps someone else.

于 2016-05-17T19:11:47.950 回答
1

就我而言,我使用的是JEST Test Cases. 我将所有test.js文件放在 pages/下一个文件结构的文件夹中。

test.js因此,通过将所有文件从pages文件夹移动到外部文件夹或next file structure内部文件夹来解决我的问题__test__root of the folder

于 2021-09-17T11:40:36.717 回答
1

In order to expose expect globally, while using chai, following should be loaded by means of configuration for your prefered testing library:

require('chai/register-assert');  // Using Assert style
require('chai/register-expect');  // Using Expect style
require('chai/register-should');  // Using Should style

For example:

npx mocha --config .mocharc.js *.spec.js

于 2019-08-27T21:30:23.657 回答
0

如果您使用 Mocha 进行 TDD,请安装 Expect.js 或 Chai.js

所以,做npm install expectnpm install chai

于 2016-04-05T10:49:39.997 回答
0

在 html 文件中添加此脚本标记

<script src="https://unpkg.com/expect@%3C21/umd/expect.min.js"></script>

或安装包

npm install chai or expect
于 2019-05-28T18:41:58.930 回答
0

创建一个文件 chaiexpections.js 并将期望声明为全局

'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',   
  },

这可以防止必须在每个 stepdefinition 中声明 chai 异常的开销

于 2019-12-18T10:43:54.060 回答