ReferenceError: description没有定义NodeJs

时间:2022-11-06 15:27:05

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

我正在尝试定义一些端点并使用nodejs进行测试。在服务器上。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.js I am trying to use this endpoint:

现在在测试。我正在尝试使用这个端点:

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.js I am getting this error:

但是当我运行节点测试时。我得到这个错误:

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?

我如何解决这个问题?

4 个解决方案

#1


132  

Assuming you are testing via mocha, you have to run your tests using the mocha command instead of the node executable.

假设您正在通过mocha进行测试,那么您必须使用mocha命令而不是节点可执行文件来运行测试。

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

如果你还没有,确保你已经安装了mocha -g。然后在项目的根目录中运行mocha。

#2


34  

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安装Mocha—save-dev)

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

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

• In your package.json, add a section for scripts and target the mocha binary

•在你的包。json,为脚本添加一节,目标是mocha二进制

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

• Put your spec files in a directory named /test in your root directory

•将您的spec文件放在根目录中的一个名为/test的目录中

• In your spec files, import the assertion library

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

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

• You don't need to import mocha, run mocha.setup, or call mocha.run()

•你不需要进口摩卡,运行摩卡。设置,或叫mocha.run()

• Then run the script from your project root:

•然后从项目根目录中运行脚本:

npm test

#3


11  

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/

#4


2  

OP asked about running from node not 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 tdd like in the docs, but that didn't work, bdd worked though.

我在文档中尝试了tdd,但是没有成功,但是bdd成功了。

#1


132  

Assuming you are testing via mocha, you have to run your tests using the mocha command instead of the node executable.

假设您正在通过mocha进行测试,那么您必须使用mocha命令而不是节点可执行文件来运行测试。

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

如果你还没有,确保你已经安装了mocha -g。然后在项目的根目录中运行mocha。

#2


34  

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安装Mocha—save-dev)

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

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

• In your package.json, add a section for scripts and target the mocha binary

•在你的包。json,为脚本添加一节,目标是mocha二进制

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

• Put your spec files in a directory named /test in your root directory

•将您的spec文件放在根目录中的一个名为/test的目录中

• In your spec files, import the assertion library

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

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

• You don't need to import mocha, run mocha.setup, or call mocha.run()

•你不需要进口摩卡,运行摩卡。设置,或叫mocha.run()

• Then run the script from your project root:

•然后从项目根目录中运行脚本:

npm test

#3


11  

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/

#4


2  

OP asked about running from node not 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 tdd like in the docs, but that didn't work, bdd worked though.

我在文档中尝试了tdd,但是没有成功,但是bdd成功了。