Writing Unit Tests for Our Application

Learn about unit tests and the feedback we get from them.

Unit tests - expect

When setting up the tests, we have the option of grouping all the expect calls within a single test function. This can be a good idea if multiple tests require the same setup steps and those steps are slow. In this case, combining them reduces the work that needs to be done and can speed up the tests. On the other hand, if our tests are slow, then they aren’t good unit tests. Unit tests should be fast because they will be run on every commit to the repository. Most of the time, it’s better to put the expect calls in separate functions. Once one expect fails, the test stops running, so we get no further information. Separating the expect calls maximizes the information we get from running the test suite when errors are present.

The following tests show how we might check that the getCollatzSequence function is operating properly.

Try it yourself

Run the tests in the widget below.

Get hands-on with 1200+ tech skills courses.