Last active
June 13, 2021 10:33
-
-
Save mattiaerre/4cdc922be4ba56f831f88aafb4c760c9 to your computer and use it in GitHub Desktop.
Revisions
-
mattiaerre revised this gist
Apr 15, 2019 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,11 @@ app.get('/playground', handler); `http://localhost:9000/playground` ```txt Request URL: http://localhost:9000/playground Request Method: GET Status Code: 200 OK ``` ```json {"message":"OK"} @@ -20,9 +22,11 @@ Status Code: 200 OK `http://localhost:9000/playground?error=KO` ```txt Request URL: http://localhost:9000/playground?error=KO Request Method: GET Status Code: 500 Internal Server Error ``` ```json {"message":"KO"} -
mattiaerre revised this gist
Apr 15, 2019 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,22 @@ app.get('/playground', handler); `http://localhost:9000/playground` Request URL: http://localhost:9000/playground Request Method: GET Status Code: 200 OK ```json {"message":"OK"} ``` ## 500 `http://localhost:9000/playground?error=KO` Request URL: http://localhost:9000/playground?error=KO Request Method: GET Status Code: 500 Internal Server Error ```json {"message":"KO"} ``` -
mattiaerre revised this gist
Apr 15, 2019 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ # How to test it in an Express app ```javascript app.get('/playground', handler); ``` ## 200 `http://localhost:9000/playground` ## 500 `http://localhost:9000/playground?error=KO` -
mattiaerre revised this gist
Apr 15, 2019 . 3 changed files with 15 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ async function handler({ query: { error } }, res) { if (error) { return res.status(500).send({ message: error }); } return res.status(200).send({ message: 'OK' }); } 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 charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ describe('handler', () => { it(description, async () => { const req = { query: { error } }; const res = { send: jest.fn(), status: jest.fn(() => res) }; 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`handler w/ error 1`] = ` Object { "message": "Oh Noes!", } `; exports[`handler w/o error 1`] = ` Object { "message": "OK", } `; -
mattiaerre created this gist
Apr 15, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ async function handler({ query: { error } }, res) { if (error) { return res.send({ message: error }).status(500); } return res.status(200).send({ message: 'OK' }); } module.exports = handler; 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ const handler = require('./handler'); describe('handler', () => { const scenarios = [ { description: 'w/o error', error: null, status: 200 }, { description: 'w/ error', error: 'Oh Noes!', status: 500 } ]; scenarios.forEach(({ description, error, status }) => { it(description, async () => { const req = { query: { error } }; const res = { send: jest.fn(() => res), status: jest.fn(() => res) }; await handler(req, res); expect(res.status).toBeCalledWith(status); expect(res.send.mock.calls[0][0]).toMatchSnapshot(); }); }); });