ReferenceError: describe is not defined NodeJs

ReferenceError: describe is not defined NodeJs

我正在尝试定义一些端点并使用 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);

并在 func1.js 中:

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

现在 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();
                });
        });
});

但是当我 运行 node test.js 我收到这个错误:

描述('Account',函数(){
^

ReferenceError:描述未定义
    在对象。 (/test/test.js:9:1)
    在 Module._compile (module.js:456:26)
    在 Object.Module._extensions..js (module.js:474:10)
    在 Module.load (module.js:356:32)
    在 Function.Module._load (module.js:312:12)
    在 Function.Module.runMain (module.js:497:10)
    启动时 (node.js:119:16)
    在 node.js:906:3

我该如何解决这个问题?

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

因此,如果您还没有这样做,请务必这样做 npm install mocha -g。然后只是 运行 mocha 在你的项目的根目录中。

要在不全局安装 Mocha 的情况下使用 node/npm 进行 运行 测试,您可以这样做:

• 在您的项目本地安装 Mocha (npm install mocha --save-dev)

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

• 在您的 package.json 中,为 scripts 添加一个部分并针对 mocha 二进制文件

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

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

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

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

• 您不需要 导入 mocha,运行 mocha.setup,或调用 mocha.run()

• 然后 运行 来自项目根目录的脚本:

npm test

你也可以这样做:

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

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

OP 从 node 而非 mocha 询问了 运行。这是一个非常常见的用例,请参阅 Using Mocha Programatically

这是我测试中注入的 describe 和它。

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

我像在文档中那样尝试了 tdd,但这没有用,但 bdd 有效。

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

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

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

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

    {
      "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"
},

我在使用“--ui tdd”时遇到这个错误。 删除它或使用“--ui bdd”修复问题。

  • 确保您有一个名为 test 的文件夹,其中包含您的 test.js 文件。
  • 还要确保你的项目中有 mocha,方法是在终端中 运行ning mocha -version(在项目路径中)
  • 确保您的项目有 package.json 可用,如果没有 运行 npm init -y
  • 最后 运行 mocha 测试脚本,在终端上(在项目路径上)运行 npm test

对于 Jest,您必须将 "jest": true 添加到 .eslintrc

{
  "env": {
    "browser": true,
    "es6": true,
    "jest": true
  },
...