ReferenceError:describe 未定义 NodeJs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28400459/
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 18:24:09  来源:igfitidea点击:

ReferenceError: describe is not defined NodeJs

node.jsresttesting

提问by N34

I am trying to define some endpoints and do a test using nodejs. In server.jsI have:

我正在尝试定义一些端点并使用nodejs. 在server.js我有:

var express = require('express');
var func1 = require('./func1.js');
var port = 8080;
var server = express();

server.configure(function(){
  server.use(express.bodyParser());
});

server.post('/testend/', func1.testend);

and in func1.js:

并在func1.js

    var testend = function(req, res) {
           serialPort.write("1", function(err, results) {
           serialPort.write("2" + "\n", function(err, results) {
           });
      });
   });
    exports.testend = testend;

Now in test.jsI am trying to use this endpoint:

现在,test.js我正在尝试使用此端点:

var should = require('should');
var assert = require('assert');
var request = require('supertest');
var http = require('http');
var app = require('./../server.js');
var port = 8080;

describe('Account', function() {
        var url = "http://localhost:" + port.toString();
        it('test starts', function(done) {
                request(url).post('/testend/')
                // end handles the response
                .end(function(err, res) {
                        if (err) {
                                throw err;
                        }
                        res.body.error.should.type('string');
                        done();
                });
        });
});

But when I run node test.jsI am getting this error:

但是当我运行时,node test.js我收到此错误:

describe('Account', function() {
^

ReferenceError: describe is not defined
    at Object. (/test/test.js:9:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

How can I fix the issue?

我该如何解决这个问题?

回答by mscdex

Assuming you are testing via mocha, you have to run your tests using the mochacommand instead of the nodeexecutable.

假设您通过 进行测试mocha,则必须使用mocha命令而不是node可执行文件来运行测试。

So if you haven't already, make sure you do npm install mocha -g. Then just run mochain your project's root directory.

所以,如果你还没有,请确保你这样做npm install mocha -g。然后只需mocha在项目的根目录中运行即可。

回答by toffee

if you are using vscode, want to debug your files

如果你正在使用 vscode,想要调试你的文件

I used tddbefore, it throw ReferenceError: describe is not defined

tdd以前用过,它抛出ReferenceError: describe is not defined

But, when I use bdd, it works!

但是,当我使用时bdd,它起作用了!

waste half day to solve it....

浪费半天时间解决它....

    {
      "type": "node",
      "request": "launch",
      "name": "Mocha Tests",
      "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
      "args": [
        "-u",
        "bdd",// set to bdd, not tdd
        "--timeout",
        "999999",
        "--colors",
        "${workspaceFolder}/test/**/*.js"
      ],
      "internalConsoleOptions": "openOnSessionStart"
},

回答by cantera

To run tests with node/npm without installing Mocha globally, you can do this:

要在不全局安装 Mocha 的情况下使用 node/npm 运行测试,您可以执行以下操作:

? Install Mocha locally to your project (npm install mocha --save-dev)

? 将 Mocha 本地安装到您的项目 ( npm install mocha --save-dev)

? Optionally install an assertion library (npm install chai --save-dev)

? 可选择安装断言库 ( npm install chai --save-dev)

? In your package.json, add a section for scriptsand target the mocha binary

? 在您的 中package.json,为scriptsmocha 二进制文件添加一个部分并将其作为目标

"scripts": {
  "test": "node ./node_modules/mocha/bin/mocha"
}

? Put your spec files in a directory named /testin your root directory

? 将您的规范文件放在/test根目录中命名的目录中

? In your spec files, import the assertion library

? 在您的规范文件中,导入断言库

var expect = require('chai').expect;

? You don'tneed to import mocha, run mocha.setup, or call mocha.run()

? 你并不需要进口摩卡,运行mocha.setup,或致电mocha.run()

? Then run the script from your project root:

? 然后从您的项目根目录运行脚本:

npm test

回答by subhojit777

You can also do like this:

你也可以这样做:

  var mocha = require('mocha')
  var describe = mocha.describe
  var it = mocha.it
  var assert = require('chai').assert

  describe('#indexOf()', function() {
    it('should return -1 when not present', function() {
      assert.equal([1,2,3].indexOf(4), -1)
    })
  })

Reference: http://mochajs.org/#require

参考:http: //mochajs.org/#require

回答by eighteyes

OP asked about running from nodenot from mocha. This is a very common use case, see Using Mocha Programatically

OP 询问node是否从mocha. 这是一个非常常见的用例,请参阅以编程方式使用 Mocha

This is what injected describe and it into my tests.

这就是将描述和它注入到我的测试中的内容。

mocha.ui('bdd').run(function (failures) {
    process.on('exit', function () {
      process.exit(failures);
    });
  });

I tried tddlike in the docs, but that didn't work, bdd worked though.

tdd像在文档中一样尝试过,但这不起作用,但 bdd 起作用了。

回答by GraySe7en

i have this error when using "--ui tdd". remove this or using "--ui bdd" fix problem.

使用“--ui tdd”时出现此错误。删除此或使用“--ui bdd”修复问题。