Created
December 26, 2018 14:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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