Skip to content

Instantly share code, notes, and snippets.

@nardhar
Last active September 13, 2017 20:07
Show Gist options
  • Save nardhar/845c1da9d005aee01dd0301c8e7d78b7 to your computer and use it in GitHub Desktop.
Save nardhar/845c1da9d005aee01dd0301c8e7d78b7 to your computer and use it in GitHub Desktop.
node js Test Server Should idea
// Is there something like this?
const RequestTest = require('should-request-test');
describe('Starting controller testing', () => {
// calling the express app
const server = require('src/index');
// controller endpoint
const endpoint = '/api/v1/myResource';
const requestTest;
before((done) => {
const token = getToken();
// send the same header on all petitions
requestTest = new RequestTest({
server,
endpoint,
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
});
// all set up and now I just need to send body and test for the response
// done() could be called after the it callback, like a wrapper
it('POST a record', () => {
requestTest.post(
// body of the request
{
number: 123,
name: 'my sample name',
},
// response callback
(res) => {
should(res.status).be.eql(201);
should(res.body).have.property('name', 'my sample name');
should(res.body).have.property('number', 123);
},
);
});
it('PUT a record with query and body', () => {
requestTest.put(
'1',
// body of the request
{
number: 123,
name: 'my sample name',
},
// response callback
(res) => {
should(res.status).be.eql(200);
should(res.body).have.property('name', 'my sample name');
should(res.body).have.property('number', 123);
},
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment