Skip to content

Instantly share code, notes, and snippets.

@goldbergyoni
Created December 26, 2018 14:29
Show Gist options
  • Select an option

  • Save goldbergyoni/5b483c936a5835cec9f89df0a20cb8d6 to your computer and use it in GitHub Desktop.

Select an option

Save goldbergyoni/5b483c936a5835cec9f89df0a20cb8d6 to your computer and use it in GitHub Desktop.
Doing It Right Example: Testing middleware in isolation without issuing network calls and waking-up the entire Express machine
//the middleware we want to test
const unitUnderTest = require('./middleware')
const httpMocks = require('node-mocks-http');
//Jest syntax, equivelant to describe() & it() in Mocha
test('A request without authentication header, should return http status 403', () => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
headers: {
authentication: ''
}
});
const response = httpMocks.createResponse();
unitUnderTest(request, response);
expect(response.statusCode).toBe(403);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment