mirror of
				https://github.com/goldbergyoni/nodebestpractices.git
				synced 2025-10-31 17:47:26 +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,19 +8,19 @@ 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(); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Yoni
					Yoni