diff --git a/.all-contributorsrc b/.all-contributorsrc index e2fa9c02..46d09875 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1650,6 +1650,15 @@ "contributions": [ "content" ] + }, + { + "login": "Alex-Dumitru", + "name": "Alex Dumitru", + "avatar_url": "https://avatars.githubusercontent.com/u/43738450?v=4", + "profile": "https://github.com/Alex-Dumitru", + "contributions": [ + "content" + ] } ], "projectName": "nodebestpractices", diff --git a/README.md b/README.md index b7b19cd2..65fa996c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@
- 102 items Last update: February 07, 2022 Updated for Node 14.0.0 + 102 items Last update: February 13, 2022 Updated for Node 14.0.0

@@ -1810,6 +1810,7 @@ Thanks goes to these wonderful people who have contributed to this repository!
Fajar Budhi Iswanda

๐Ÿ–‹
์ด์ฃผํ˜ธ

๐Ÿ–‹
Singh

๐Ÿ–‹ +
Alex Dumitru

๐Ÿ–‹ diff --git a/sections/testingandquality/refactoring.korean.md b/sections/testingandquality/refactoring.korean.md new file mode 100644 index 00000000..379dccb0 --- /dev/null +++ b/sections/testingandquality/refactoring.korean.md @@ -0,0 +1,41 @@ +# Refactoring + +

+ +### One Paragraph Explainer + +๋ฆฌํŒฉํ† ๋ง์€ ๋ฐ˜๋ณต์ ์ธ ๊ฐœ๋ฐœ ํ๋ฆ„์—์„œ ์ค‘์š”ํ•œ ๊ณผ์ •์ด๋‹ค. ์ค‘๋ณต ์ฝ”๋“œ, ๊ธด ๋ฉ”์„œ๋“œ, ๊ธด ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋ชฉ๋ก๊ณผ ๊ฐ™์€ "์ฝ”๋“œ ์Šค๋ฉœ"(๋ถˆ๋Ÿ‰ ์ฝ”๋”ฉ ๊ด€ํ–‰)์„ ์ œ๊ฑฐํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ํ–ฅ์ƒ๋˜๊ณ  ์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ๋”์šฑ ์šฉ์ดํ•ด์ง‘๋‹ˆ๋‹ค. ์ •์  ๋ถ„์„ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ์ฝ”๋“œ ์Šค๋ฉœ์„ ์ฐพ๊ณ  ๋ฆฌํŒฉํ† ๋ง์— ๋Œ€ํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. CI ๋นŒ๋“œ์— ์ด๋Ÿฌํ•œ ๋„๊ตฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ํ’ˆ์งˆ ๊ฒ€์‚ฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž๋™ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. CI๊ฐ€ ์†Œ๋‚˜ ๋˜๋Š” ์ฝ”๋“œ ๊ธฐํ›„์™€ ๊ฐ™์€ ๋„๊ตฌ์™€ ํ†ตํ•ฉ๋  ๊ฒฝ์šฐ ์ฝ”๋“œ ์Šค๋ฉœ์„ ๊ฐ์ง€ํ•˜๊ณ  ์ž‘์„ฑ์ž์—๊ฒŒ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๋ฉด ๋นŒ๋“œ๊ฐ€ ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •์  ๋ถ„์„ ๋„๊ตฌ๋Š” ESLint์™€ ๊ฐ™์€ ๋ณดํ’€ ๋„๊ตฌ๋ฅผ ๋ณด์™„ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ณดํ’€ ๋„๊ตฌ๋Š” ๋‹จ์ผ ํŒŒ์ผ์—์„œ ๋“ค์—ฌ์“ฐ๊ธฐ์™€ ๋ˆ„๋ฝ๋œ ์„ธ๋ฏธ์ฝœ๋ก ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ ์Šคํƒ€์ผ(์ผ๋ถ€๋Š” ๊ธด ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ฝ”๋“œ ๋ƒ„์ƒˆ๊ฐ€ ๋‚˜๊ธฐ๋„ ํ•จ)์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ์ •์  ๋ถ„์„ ๋„๊ตฌ๋Š” ๋‹จ์ผ ํŒŒ์ผ๊ณผ ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ์žˆ๋Š” ์ฝ”๋“œ ๋ƒ„์ƒˆ(์ค‘๋ณต ์ฝ”๋“œ, ๋ณต์žก์„ฑ ๋ถ„์„ ๋“ฑ)๋ฅผ ์ฐพ๋Š” ๋ฐ ์ดˆ์ ์„ ๋งž์ถฅ๋‹ˆ๋‹ค. + +

+ +### Martin Fowler - Chief Scientist at ThoughtWorks + +์ฑ…, "JavaScript ์ˆ˜์ •: ๋ถˆ๋Ÿ‰ ์ฝ”๋“œ๋ฅผ ์ •์ƒ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ" + +> ๋ฆฌํŒฉํ† ๋ง์€ ๊ธฐ์กด ์ฝ”๋“œ๋ฒ ์ด์Šค์˜ ์„ค๊ณ„๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ ํ†ต์ œ๋œ ๊ธฐ์ˆ ์ด๋‹ค. + +

+ +### Evan Burchard - Web Development Consultant and Author + +์ฑ…, "JavaScript ์ˆ˜์ •: ๋ถˆ๋Ÿ‰ ์ฝ”๋“œ๋ฅผ ์ •์ƒ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ" + +> ์‚ฌ์šฉํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ +> "์ปดํŒŒ์ผ-ํˆฌ-JS" ์–ธ์–ด ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ฌด์—‡์ด๋“  +> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ํ’ˆ์งˆ์ด ๋‚ฎ์œผ๋ฉด ๋ฒ„๊ทธ์™€ ์„ฑ๋Šฅ์— ๋Œ€ํ•œ ์šฐ๋ ค๋Š” ํ•ญ์ƒ ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. + +

+ +### Example: Complex methods analysis with CodeClimate (commercial) + +![alt text](../../assets/images/codeanalysis-climate-complex-methods.PNG "Complex methods analysis") + +### Example: Code analysis trends and history with CodeClimate (commercial) + +![alt text](../../assets/images/codeanalysis-climate-history.PNG "Code analysis history") + +### Example: Code analysis summary and trends with SonarQube (commercial) + +![alt text](../../assets/images/codeanalysis-sonarqube-dashboard.PNG "Code analysis history") + +

diff --git a/sections/testingandquality/refactoring.md b/sections/testingandquality/refactoring.md index db9f04c5..804f9147 100644 --- a/sections/testingandquality/refactoring.md +++ b/sections/testingandquality/refactoring.md @@ -4,29 +4,30 @@ ### One Paragraph Explainer -๋ฆฌํŒฉํ† ๋ง์€ ๋ฐ˜๋ณต์ ์ธ ๊ฐœ๋ฐœ ํ๋ฆ„์—์„œ ์ค‘์š”ํ•œ ๊ณผ์ •์ด๋‹ค. ์ค‘๋ณต ์ฝ”๋“œ, ๊ธด ๋ฉ”์„œ๋“œ, ๊ธด ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋ชฉ๋ก๊ณผ ๊ฐ™์€ "์ฝ”๋“œ ์Šค๋ฉœ"(๋ถˆ๋Ÿ‰ ์ฝ”๋”ฉ ๊ด€ํ–‰)์„ ์ œ๊ฑฐํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ํ–ฅ์ƒ๋˜๊ณ  ์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ๋”์šฑ ์šฉ์ดํ•ด์ง‘๋‹ˆ๋‹ค. ์ •์  ๋ถ„์„ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ์ฝ”๋“œ ์Šค๋ฉœ์„ ์ฐพ๊ณ  ๋ฆฌํŒฉํ† ๋ง์— ๋Œ€ํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. CI ๋นŒ๋“œ์— ์ด๋Ÿฌํ•œ ๋„๊ตฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ํ’ˆ์งˆ ๊ฒ€์‚ฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž๋™ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. CI๊ฐ€ ์†Œ๋‚˜ ๋˜๋Š” ์ฝ”๋“œ ๊ธฐํ›„์™€ ๊ฐ™์€ ๋„๊ตฌ์™€ ํ†ตํ•ฉ๋  ๊ฒฝ์šฐ ์ฝ”๋“œ ์Šค๋ฉœ์„ ๊ฐ์ง€ํ•˜๊ณ  ์ž‘์„ฑ์ž์—๊ฒŒ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๋ฉด ๋นŒ๋“œ๊ฐ€ ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •์  ๋ถ„์„ ๋„๊ตฌ๋Š” ESLint์™€ ๊ฐ™์€ ๋ณดํ’€ ๋„๊ตฌ๋ฅผ ๋ณด์™„ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ณดํ’€ ๋„๊ตฌ๋Š” ๋‹จ์ผ ํŒŒ์ผ์—์„œ ๋“ค์—ฌ์“ฐ๊ธฐ์™€ ๋ˆ„๋ฝ๋œ ์„ธ๋ฏธ์ฝœ๋ก ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ ์Šคํƒ€์ผ(์ผ๋ถ€๋Š” ๊ธด ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ฝ”๋“œ ๋ƒ„์ƒˆ๊ฐ€ ๋‚˜๊ธฐ๋„ ํ•จ)์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ์ •์  ๋ถ„์„ ๋„๊ตฌ๋Š” ๋‹จ์ผ ํŒŒ์ผ๊ณผ ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ์žˆ๋Š” ์ฝ”๋“œ ๋ƒ„์ƒˆ(์ค‘๋ณต ์ฝ”๋“œ, ๋ณต์žก์„ฑ ๋ถ„์„ ๋“ฑ)๋ฅผ ์ฐพ๋Š” ๋ฐ ์ดˆ์ ์„ ๋งž์ถฅ๋‹ˆ๋‹ค. +Refactoring is an important process in the iterative development flow. Removing "Code Smells" (bad coding practices) such as Duplicated Code, Long Methods, Long Parameter list will improve your code and making it more maintainable. Using a static analysis tools will assist you in finding these code smells and build a process around refactoring. Adding these tools to your CI build will help automate the quality checking process. If your CI integrates with a tool like Sonar or Code Climate, the build will fail if it detects code smells and inform the author on how to address the issue. Theses static analysis tools will complement lint tools such as ESLint. Most linting tools will focus on code styles like indentation and missing semicolons (although some will find code smells like Long functions) in a single file while static analysis tools will focus on finding code smells (duplicate code, complexity analysis, etc) that are in single files and multiple files.

+ ### Martin Fowler - Chief Scientist at ThoughtWorks -์ฑ…, "JavaScript ์ˆ˜์ •: ๋ถˆ๋Ÿ‰ ์ฝ”๋“œ๋ฅผ ์ •์ƒ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ" + From the book, "Refactoring - Improving the Design of Existing Code" -> ๋ฆฌํŒฉํ† ๋ง์€ ๊ธฐ์กด ์ฝ”๋“œ๋ฒ ์ด์Šค์˜ ์„ค๊ณ„๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ ํ†ต์ œ๋œ ๊ธฐ์ˆ ์ด๋‹ค. + > Refactoring is a controlled technique for improving the design of an existing code base.

### Evan Burchard - Web Development Consultant and Author -์ฑ…, "JavaScript ์ˆ˜์ •: ๋ถˆ๋Ÿ‰ ์ฝ”๋“œ๋ฅผ ์ •์ƒ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ" + From the book, "Refactoring JavaScript: Turning Bad Code into Good Code" -> ์‚ฌ์šฉํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ -> "์ปดํŒŒ์ผ-ํˆฌ-JS" ์–ธ์–ด ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ฌด์—‡์ด๋“  -> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ํ’ˆ์งˆ์ด ๋‚ฎ์œผ๋ฉด ๋ฒ„๊ทธ์™€ ์„ฑ๋Šฅ์— ๋Œ€ํ•œ ์šฐ๋ ค๋Š” ํ•ญ์ƒ ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. + > No matter what framework or +โ€œcompiles-to-JSโ€ language or library you use, bugs and performance concerns +will always be an issue if the underlying quality of your JavaScript is poor.

-### Example: Complex methods analysis with CodeClimate (commercial) + ### Example: Complex methods analysis with CodeClimate (commercial) ![alt text](../../assets/images/codeanalysis-climate-complex-methods.PNG "Complex methods analysis") @@ -38,4 +39,5 @@ ![alt text](../../assets/images/codeanalysis-sonarqube-dashboard.PNG "Code analysis history") +

diff --git a/sections/testingandquality/test-middlewares.korean.md b/sections/testingandquality/test-middlewares.korean.md new file mode 100644 index 00000000..b052b54f --- /dev/null +++ b/sections/testingandquality/test-middlewares.korean.md @@ -0,0 +1,30 @@ +# Test your middlewares in isolation + +

+ +### One Paragraph Explainer + +๋ฏธ๋“ค์›จ์–ด ํ…Œ์ŠคํŠธ๋Š” ์‹œ์Šคํ…œ์˜ ์ž‘์€ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜๋ฉฐ ๋ผ์ด๋ธŒ Express ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„ ๋ฏธ๋“ค์›จ์–ด ํ…Œ์ŠคํŠธ๋ฅผ ํšŒํ”ผํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ์ด์œ  ๋ชจ๋‘ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋“ค์›จ์–ด๋Š” ์ž‘์ง€๋งŒ ์š”์ฒญ์˜ ์ „๋ถ€ ๋˜๋Š” ๋Œ€๋ถ€๋ถ„์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋ฉฐ '{req,res}' JS ๊ฐœ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ์‰ฝ๊ฒŒ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  {req,res}๊ฐœ ๊ฐœ์ฒด์™€์˜ ์ƒํ˜ธ ์ž‘์šฉ์— ๋Œ€ํ•ด [์˜ˆ๋ฅผ ๋“ค์–ด Sinon ์‚ฌ์šฉ](https://www.npmjs.com/package/sinon)))์„ ์ŠคํŒŒ์ดํ•˜์—ฌ ํ•จ์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ [node-controls-products](https://www.npmjs.com/package/node-mocks-http)๋Š” ๋” ๋‚˜์•„๊ฐ€์„œ {req,res}๊ฐœ์˜ ๊ฐ์ฒด์˜ ๋™์ž‘์— ๋Œ€ํ•œ ์ŠคํŒŒ์ด ํ™œ๋™๊ณผ ํ•จ๊ป˜ ์ธ์ž๋ฅผ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด res ๊ฐœ์ฒด์— ์„ค์ •๋œ http ์ƒํƒœ๊ฐ€ ์˜ˆ์ƒ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์•„๋ž˜ ์˜ˆ ์ฐธ์กฐ). + +

+ +### Code example: Testing middleware in isolation + +```javascript +//the middleware we want to test +const unitUnderTest = require("./middleware"); +const httpMocks = require("node-mocks-http"); +//Jest syntax, equivalent 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); +}); +``` diff --git a/sections/testingandquality/test-middlewares.md b/sections/testingandquality/test-middlewares.md index b052b54f..195acbec 100644 --- a/sections/testingandquality/test-middlewares.md +++ b/sections/testingandquality/test-middlewares.md @@ -4,7 +4,7 @@ ### One Paragraph Explainer -๋ฏธ๋“ค์›จ์–ด ํ…Œ์ŠคํŠธ๋Š” ์‹œ์Šคํ…œ์˜ ์ž‘์€ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜๋ฉฐ ๋ผ์ด๋ธŒ Express ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„ ๋ฏธ๋“ค์›จ์–ด ํ…Œ์ŠคํŠธ๋ฅผ ํšŒํ”ผํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ์ด์œ  ๋ชจ๋‘ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋“ค์›จ์–ด๋Š” ์ž‘์ง€๋งŒ ์š”์ฒญ์˜ ์ „๋ถ€ ๋˜๋Š” ๋Œ€๋ถ€๋ถ„์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋ฉฐ '{req,res}' JS ๊ฐœ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ์‰ฝ๊ฒŒ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  {req,res}๊ฐœ ๊ฐœ์ฒด์™€์˜ ์ƒํ˜ธ ์ž‘์šฉ์— ๋Œ€ํ•ด [์˜ˆ๋ฅผ ๋“ค์–ด Sinon ์‚ฌ์šฉ](https://www.npmjs.com/package/sinon)))์„ ์ŠคํŒŒ์ดํ•˜์—ฌ ํ•จ์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ [node-controls-products](https://www.npmjs.com/package/node-mocks-http)๋Š” ๋” ๋‚˜์•„๊ฐ€์„œ {req,res}๊ฐœ์˜ ๊ฐ์ฒด์˜ ๋™์ž‘์— ๋Œ€ํ•œ ์ŠคํŒŒ์ด ํ™œ๋™๊ณผ ํ•จ๊ป˜ ์ธ์ž๋ฅผ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด res ๊ฐœ์ฒด์— ์„ค์ •๋œ http ์ƒํƒœ๊ฐ€ ์˜ˆ์ƒ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์•„๋ž˜ ์˜ˆ ์ฐธ์กฐ). +Many avoid Middleware testing because they represent a small portion of the system and require a live Express server. Both reasons are wrong โ€” Middlewares are small but affect all or most of the requests and can be tested easily as pure functions that get `{req,res}` JS objects. To test a middleware function one should just invoke it and spy ([using Sinon for example](https://www.npmjs.com/package/sinon)) on the interaction with the {req,res} objects to ensure the function performed the right action. The library [node-mock-http](https://www.npmjs.com/package/node-mocks-http) takes it even further and factors the {req,res} objects along with spying on their behavior. For example, it can assert whether the http status that was set on the res object matches the expectation (See example below)

@@ -20,11 +20,11 @@ test("A request without authentication header, should return http status 403", ( method: "GET", url: "/user/42", headers: { - authentication: "", - }, + authentication: "" + } }); const response = httpMocks.createResponse(); unitUnderTest(request, response); expect(response.statusCode).toBe(403); }); -``` +``` \ No newline at end of file