Skip to main content

Integration Tests

Integration Tests are used to test the interaction between different parts of the system. They differ to unit test in that they test the system as a whole, rather than individual parts. Commonly in nest, unit testing is performed by mocking the dependencies of a class or module. This is useful for testing the individual parts of the system, but it does not test the interaction between the different parts of the system. Furthermore, it introduces the risk of the mock not behaving the same as the real dependency.

To solve this problem, we use a "test suite" to boot a fully functional application and test the interaction between the different parts of the system. The test suite runs with a real database, rabbitmq, smtp and cerbos server, provided by docker-compose.ci.yaml. As such, the tests can only really be reliably run within the test runner.

When should I Write Integration Tests?

The answer here is easy: use unit test to test services that are free of dependencies. Use integration tests to test services that have dependencies.

Getting Started

In order to run integration tests on a nest application, the project needs to be flagged as an integration-test project. This is done by adding the integration-test tag to the project in the project.json file. This will trigger the build pipelines to test the project on a integration test runner.

{
"name": "my-project",
"tags": ["integration-test"]
}

Setting up the Test Suite

The test suite needs to be configured run with jest. If this has already been done, you can skip this step.