Skip to content

Testing (Code Review) #74

@jamiecoe

Description

@jamiecoe

Jest CLI

The reason you're having to use jest --detectOpenHandles is because the app you provide to supertest is listening on a port, (eg: app.listen()). Therefore, when you run the tests it never closes, which causes jest to complain. If you move this section to a separate file, you can run jest without the --detectOpenHandles flag.

routes.test.js

Currently you’re just testing the status code, rather than the content of the response that comes back. This can be dangerous, for example in your 500 route test. This test is currently passing, but if we run console.log(response.text) - it's not the 500 message you're expecting. It's this:

Error: No default engine was specified and no extension was provided.

What is actually happening is the error thrown and passed to next() is being passed to the 404 route as it also has 4 arguments and comes before the 500 route. However, because you then try to use res.render() instead of res.send(), a 500 response is returned (as you are not using a rendering engine) which unfortunately causes the test to pass.

Testing the response content as well as the status code would catch unlucky coincidences like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions