Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 错误:超过了XX毫秒的超时时间。确保done()回调正在..supertest+;快报()_Javascript_Node.js_Express_Mocha.js_Supertest - Fatal编程技术网

Javascript 错误:超过了XX毫秒的超时时间。确保done()回调正在..supertest+;快报()

Javascript 错误:超过了XX毫秒的超时时间。确保done()回调正在..supertest+;快报(),javascript,node.js,express,mocha.js,supertest,Javascript,Node.js,Express,Mocha.js,Supertest,我正在尝试通过mocha+supertest测试我的节点服务器的API(使用express)。 我在我的app.js中有一个post API,如下所示: app.post('/product/createProduct',routes.createProduct); 在路由中,此api类似于: functions.createProduct = function(req,res){ var body =""; var jsonObj={}; console.lo

我正在尝试通过mocha+supertest测试我的节点服务器的API(使用express)。 我在我的app.js中有一个post API,如下所示:

    app.post('/product/createProduct',routes.createProduct);
在路由中,此api类似于:

functions.createProduct = function(req,res){
    var body ="";
    var jsonObj={};
    console.log('create product called..');
    req.on('data',function(chunk){
        body += chunk;
    });

    req.on('end',function(){
         jsonObj = JSON.parse(body);
         createProductAsync(req,jsonObj,res);
        });
它接受请求主体中包含产品信息的json。这个api与postman配合得很好,但当我使用supertest+mocha调用它时

it('should create a new product', function (done) {
            var req = supertest(app).post('/product/createProduct');
            agent.attachCookies(req);
            req.send({name:"someName"});
            req.expect(404)
            req.end(function(err,res){
                if(err) throw(err);
                done();
            })
 });`
我得到的时间超过错误

`Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test`
我已经尝试过--timeout选项或this.setTimeout,但这也没有帮助。有线索吗? 我正在使用的训练

    functions.createProduct = function(req,res){
    var body ="";
    var jsonObj={};
    console.log('create product called..');
    req.on('data',function(chunk){
        body += chunk;
    });

    req.on('end',function(){
         jsonObj = JSON.parse(body);
         ......
        });
    if(DEBUG){
        /*using supertest, req.body is not received in chunks,hence .req.end event is never transmitted.*/
        console.log('debug mode');
        jsonObj = req.body;
        ......
    }

};

我使用express、mocha和supertest创建了一个示例

这是我的
app.js

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.post('/api/products', function(req, res) {
  var product = req.body;
  res.send({
    message: 'product was saved succesfully.',
    product: product
  });
});

app.listen(4040, function() {
  console.log('server up and running at 4040');
});

module.exports = app;
var request = require('supertest');
var app = require('./app.js');

describe('Test Products', function() {
  it('should create a product', function(done) {
    request(app)
      .post('/api/products')
      .send({name: 'chairman'})
      .end(function (err, res) {
        console.log(res.text);
        done();
      });
  });
});
这是我的
test.js

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.post('/api/products', function(req, res) {
  var product = req.body;
  res.send({
    message: 'product was saved succesfully.',
    product: product
  });
});

app.listen(4040, function() {
  console.log('server up and running at 4040');
});

module.exports = app;
var request = require('supertest');
var app = require('./app.js');

describe('Test Products', function() {
  it('should create a product', function(done) {
    request(app)
      .post('/api/products')
      .send({name: 'chairman'})
      .end(function (err, res) {
        console.log(res.text);
        done();
      });
  });
});

这主要发生在我的createProduct函数中,因为我正在等待这样的事件:
functions.createProduct=function(req,res){var jsonObj={};req.on('data',function(chunk){body+=chunk;});req.on('end',function(){jsonObj=JSON.parse(body);…}
您能否将该代码添加到您的问题中,而不是将其作为注释?您如何在上面的post请求中使用postman?Json作为有效负载发送参数?@AnkitKhare看看这个,它得到了答案。