Skip to content

Instantly share code, notes, and snippets.

@mattiaerre
Last active June 13, 2021 10:33
Show Gist options
  • Select an option

  • Save mattiaerre/4cdc922be4ba56f831f88aafb4c760c9 to your computer and use it in GitHub Desktop.

Select an option

Save mattiaerre/4cdc922be4ba56f831f88aafb4c760c9 to your computer and use it in GitHub Desktop.

Revisions

  1. mattiaerre revised this gist Apr 15, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original 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"}
  2. mattiaerre revised this gist Apr 15, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions README.md
    Original 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"}
    ```
  3. mattiaerre revised this gist Apr 15, 2019. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions README.md
    Original 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`
  4. mattiaerre revised this gist Apr 15, 2019. 3 changed files with 15 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion handler.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    async function handler({ query: { error } }, res) {
    if (error) {
    return res.send({ message: error }).status(500);
    return res.status(500).send({ message: error });
    }
    return res.status(200).send({ message: 'OK' });
    }
    2 changes: 1 addition & 1 deletion handler.test.js
    Original 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(() => res),
    send: jest.fn(),
    status: jest.fn(() => res)
    };

    13 changes: 13 additions & 0 deletions handler.test.js.snap
    Original 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",
    }
    `;
  5. mattiaerre created this gist Apr 15, 2019.
    8 changes: 8 additions & 0 deletions handler.js
    Original 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;
    30 changes: 30 additions & 0 deletions handler.test.js
    Original 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();
    });
    });
    });