ReferenceError:描述未定义NodeJs [英] ReferenceError: describe is not defined NodeJs

查看:349
本文介绍了ReferenceError:描述未定义NodeJs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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中,我正在尝试使用此端点:

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

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

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

如何解决此问题?

推荐答案

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

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

因此,如果您还没有这样做,请确保您已执行npm install mocha -g.然后只需在项目的根目录中运行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.

这篇关于ReferenceError:描述未定义NodeJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆