Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request body undefined in supertest

I am testing an express API with supertest. I am trying to pass in body parameters into the test, as can be seen in the code snippets below, but it appears that the body parameters don't get passed in correctly since I get an error message that the body parameters are undefined.

Running the test with command mocha --recursive returns the following error:

Cannot read property 'email' of undefined


Below is the code from file email-suite.js referencing supertest

'use strict';
var express = require("express");
var bodyParser = require('body-parser');
var mongoose = require("mongoose");

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var supertest = require("supertest");
var chai = require("chai");
var should = chai.should();

var api = require("../server.js");

describe("Email Module", function() {
    this.timeout(25000);

    before(function(done){
        mongoose.createConnection(/* connectionstring */);

        mongoose.connection.on('open', function(err) {
            if(err) console.log(err);
            console.log('connected to server');
        });

        done();
    });

    it("Check Email Count", function(done) {
        var body = { email: "[email protected]" };

        supertest(api)
            .post("/emailCount")
            .set('Accept', 'application/json')
            .send(body)  // body is undefined
            .expect(200)
            .expect('Content-Type', /json/)
            .end(function(err, res) {
                if(err) return done(err);
                res.body.count.should.equal(2);
                done();
            });
    });
});

Below is the code from file email-api.js

'use strict';
var express = require("express");
var bodyParser = require('body-parser');
var router = express.Router();
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

router.post('/emailCount', function(req, res) {
    var email = req.body.email; // req.body is undefined
}

module.exports = router;

Below is the code from the file server.js

var express = require("express");
var app = express();

app.set("port", process.env.PORT || 3000);

var router = require("./user/email-api");

app.use('/', router);

app.listen(app.get("port"), function() {
    console.log("App started on port " + app.get("port"));
});

module.exports = app;
like image 797
dpen82 Avatar asked Sep 13 '16 12:09

dpen82


People also ask

How do I send a body in post request in SuperTest?

const request = require('supertest'); const bodyParser = require('body-parser'); let AValidator = require('../src/AValidator'); let BValidator = require('../src/BValidator'); BValidator = jest. fn(); AValidator = jest. fn(); app = require('../src/app'); app.

What does SuperTest request do?

SuperTest is a Node. js library that helps developers test APIs. It extends another library called superagent, a JavaScript HTTP client for Node. js and the browser.


1 Answers

Put body-parser always after express object and before every routes in main server file like this

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

//Router task start from here

Other wise always will get undefined since router call first and body parsed later.

like image 197
abdulbarik Avatar answered Sep 28 '22 09:09

abdulbarik