Sunday, 23 April 2017

Node.js post request testing with supertest, express, mocha returning undefined

I've had a look at similar questions on the site and I can't see how they are applicable to my issue:

const request = require('supertest');
var express = require('express');
const mocha = require('mocha');

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

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

describe("Post valid ID", function(){
    it("returns expected data", function(done){
        var goodID = ({id: '100', name: 'Edith'});

        request("http://localhost:8000")
        .post("/users")
        .set('Accept', 'application/json')
        .type("json")
        .expect('Content-Type', 'application/json; charset=utf-8')
        .send(goodID)
        .expect(200)
        .expect([surname: 'Jones'], done);
    });
});

Running a POST request using Postman gives me the expected response, however this returns undefined. In my app code I've put a console.log on the req.body and it's showing up as undefined.

From what I can see on the internet these errors seem to have a lot to do with not having the body-parser in place but mine is there.



via Ilythya

No comments:

Post a Comment