Global setup¶
If you have many tests that require the same container, you may not want to spin up one per test.
Info
There is a misconception that containers are heavyweight.
Sure, if your container has a slow startup time (e.g., a database, which on startup runs large migration scripts), it may be better to just start and manage one instance. But keep in mind that this limits your tests to run sequentially, and you may need to manage the state of the container between tests.
In many cases it is far easier to start a new container for each test and run them in parallel. Of course, this depends on your specific use case.
Many popular test frameworks like Jest and Vitest support global setup and teardown scripts.
Here's an example which sets up a single Redis container globally, so it can be reused across tests. In this case we're using Vitest:
setup.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
vite.config.js | |
---|---|
1 2 3 4 5 6 7 |
|
And to use the container/client in your tests:
1 2 |
|