mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-30 00:57:04 +08:00
New BP: test middlewares in isolation
This commit is contained in:
@ -543,7 +543,7 @@ All statements above will return false if used with `===`
|
||||
|
||||
**Otherwise:** A bug in Express middleware === a bug in all or most requests
|
||||
|
||||
🔗 [**Read More: Choosing CI platform**](/sections/testingandquality/test-middlewares.md)
|
||||
🔗 [**Read More: Test middlewares in isolation**](/sections/testingandquality/test-middlewares.md)
|
||||
|
||||
<br/><br/><br/>
|
||||
|
||||
|
||||
@ -8,23 +8,23 @@ Many avoid Middleware testing because they represent a small portion of the syst
|
||||
|
||||
<br/><br/>
|
||||
|
||||
### Code example: Testing middleware in isolation without issuing network calls and waking-up the entire web framework
|
||||
### Code example: Testing middleware in isolation
|
||||
|
||||
```javascript
|
||||
//the middleware we want to test
|
||||
const unitUnderTest = require('./middleware')
|
||||
const httpMocks = require('node-mocks-http');
|
||||
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', () => {
|
||||
test("A request without authentication header, should return http status 403", () => {
|
||||
const request = httpMocks.createRequest({
|
||||
method: 'GET',
|
||||
url: '/user/42',
|
||||
method: "GET",
|
||||
url: "/user/42",
|
||||
headers: {
|
||||
authentication: ''
|
||||
authentication: ""
|
||||
}
|
||||
});
|
||||
const response = httpMocks.createResponse();
|
||||
unitUnderTest(request, response);
|
||||
expect(response.statusCode).toBe(403);
|
||||
});
|
||||
```
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user