node.js 如何解决“ReferenceError: expect is not defined”错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19191384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:52:31  来源:igfitidea点击:

How can I solve "ReferenceError: expect is not defined" error message?

javascriptnode.jsmocha

提问by sensorario

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

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

and a test/array.jsfile. Mocha was installed with

和一个test/array.js文件。摩卡安装了

$ npm install -g mocha

When I run

当我跑

$ mocha

I get this error:

我收到此错误:

$ 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)

回答by Dan Heberden

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

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

(I recommend chai)

(我推荐

npm install chai

then

然后

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

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

then

然后

var expect = chai.expect;

回答by Paul Rad

Try

尝试

First, in the terminal

首先,在终端

npm install expect.js

npm install expect.js

And in your code:

在你的代码中:

var expect = require('expect');

回答by dmchk

After you install Chai as other posts suggest, with the es6 syntax you should put the import at the top

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

import {expect} from 'chai';

回答by shael

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));
    });
  });
});

回答by micrub

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

为了在expect全局公开,在使用时chai,应通过配置为您首选的测试库加载以下内容:

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

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

回答by PDHide

Create a file chaiexpections.js and declare expect as global

创建文件 chaiexpections.js 并声明 expect 为全局

'use strict';

// Configure chai
global.expect = require('chai').expect;

Now pass it in config file under cucumberopts, require

现在在cucumberopts下的配置文件中传递它,需要

cucumberOpts: {
    require: ['./testsuites/*.js','./chaiexpections.js','./commons/hooks.js']
    //tags: [],
    strict: true,    
    format: [],  
    'dry-run': false,           
    compiler: [],
    format: 'json:results.json',   
  },

This prevents the overhead of having to declare chai exception in each stepdefinition

这可以防止必须在每个步骤定义中声明 chai 异常的开销

回答by ASHISH RANJAN

Either add this script tag in html file

在 html 文件中添加这个脚本标签

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

or install package

或安装包

npm install chai or expect

回答by ALoK VeRMa

Install Expect.js or Chai.js if you are using Mocha for TDD

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

So, do npm install expector npm install chai

所以,做npm install expectnpm install chai

回答by RMorrisey

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:

在我的用例中,我通过karma. 解决方案是为我的测试框架库安装 karma 集成:

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

...and also to add the frameworks to my karma.conf.js:

...并将框架添加到我的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.

希望这对其他人有帮助。