mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-11-01 10:26:49 +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
|
**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/>
|
<br/><br/><br/>
|
||||||
|
|
||||||
|
|||||||
@ -8,23 +8,23 @@ Many avoid Middleware testing because they represent a small portion of the syst
|
|||||||
|
|
||||||
<br/><br/>
|
<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
|
```javascript
|
||||||
//the middleware we want to test
|
//the middleware we want to test
|
||||||
const unitUnderTest = require('./middleware')
|
const unitUnderTest = require("./middleware");
|
||||||
const httpMocks = require('node-mocks-http');
|
const httpMocks = require("node-mocks-http");
|
||||||
//Jest syntax, equivelant to describe() & it() in Mocha
|
//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({
|
const request = httpMocks.createRequest({
|
||||||
method: 'GET',
|
method: "GET",
|
||||||
url: '/user/42',
|
url: "/user/42",
|
||||||
headers: {
|
headers: {
|
||||||
authentication: ''
|
authentication: ""
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const response = httpMocks.createResponse();
|
const response = httpMocks.createResponse();
|
||||||
unitUnderTest(request, response);
|
unitUnderTest(request, response);
|
||||||
expect(response.statusCode).toBe(403);
|
expect(response.statusCode).toBe(403);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user