Skip to main content
Make sure you are using the minimum required version of the plugin: @codspeed/vitest-plugin>=3.1.0
All the code shown on this page is available in the CodSpeedHQ/codspeed-nestjs-mongodb repository. It uses the following technologies:

Sample application

We are going to use a simple NestJS application exposing a REST API to manage cats. The following Cat model is defined:
src/cats/schemas/cat.schema.ts
The CatsController exposes the following endpoints:
src/cats/cats.controller.ts

Complete setup with Docker

Setup SWC + Vitest

To use swc and vitest with nest-js, follow the setup guide on the NestJS website. At the end of this setup, you should be able to run e2e tests with a running MongoDB instance, using the following command:

Setup CodSpeed and Vitest for e2e benchmarks

Install the dependencies: vite-tsconfig-paths is used to resolve the paths defined in the tsconfig.json file automatically. Rename the file vitest.config.e2e.ts to vitest.config.e2e.mts since @codspeed/vitest-plugin is only available in ESM. Apply the following modifications to the file:
vitest.config.e2e.mts

Create benchmarks

Similar to how we would create an e2e test in NestJS in a *.e2e.spec.ts file, we can create a benchmark in a *.e2e.bench.ts file.
src/cats/cats.controller.e2e.bench.ts
Here we have defined 4 benchmarks for the cats endpoints:
  • GET /cats: retrieve all the cats
  • GET /cats/name/:name: retrieve all the cats with the given name
  • GET /cats/breed/:breed: retrieve all the cats with the given breed
  • GET /cats/age/greater/:age: retrieve all the cats with an age greater than the given age
Note the use the usage of expect in the benchmarks.
This allows for a better experience when authoring benchmarks, as it provides a way to ensure that everything went well. This is optional, you can remove the assertions if you want:

Setup Docker locally

Add the following file to the root of the project:
docker-compose.yml
Run the following command to start the MongoDB instance:

Run the benchmarks locally

Add the following script to your package.json:
package.json
Run the following command to run the benchmarks:
terminal

Run the benchmarks in the CI

Add the following file to the project:
.github/workflows/codspeed.yml
With this configuration, the CodSpeed MongoDB instrument will be activated and data from MongoDB queries will be sent to CodSpeed.

Setup using testcontainers

Instead of relying on an externally provided Docker instance, we can leverage testcontainers to start a MongoDB instance dynamically during the benchmarks. For this setup, we assume that the state of the application is similar to the one described in the above section.

Setup Vitest + testcontainers

Install the testcontainers dependencies: Create a new file src/global.d.ts with the following content:
src/global.d.ts
This will make the globalThis.__MONGO_URI__ variable available in the whole application with the correct type.⚠️ Make sure to use var and not let or const, as otherwise the TypeScript type will not be set.
Create a new file src/testUtils/setup-vitest.ts with the following content:
src/testUtils/setup-vitest.ts
testcontainers on macOSOn macOS, we recommend using colima to run Docker containers. However there are issues using testcontainers on macOS. To bypass those issues, some environment variables need to be set when running the tests:
To make testcontainers work on macOS with colima, the following environment variables need to be set:
We will add a function to enforce that they are set when running vitest. Add the following function to your src/testUtils/setup-vitest.ts file:
src/testUtils/setup-vitest.ts
And use it at the top of the the setupMongoDB function:
src/testUtils/setup-vitest.ts
Now the execution will stop with an explicit error message if the environment variables are not set when running on macOS.
Add the file as a setupFiles entry in vite.config.e2e.mts:
vitest.config.e2e.mts
We can now change the src/app.module.ts file to use the globalThis.__MONGO_URI__ variable instead of the MONGO_URL environment variable when it is defined:
src/app.module.ts

Run the benchmarks locally

You can now run the benchmarks locally without having to start a MongoDB instance:

Run the benchmarks in the CI

You can now simplify the codspeed.yml file to the following:
  • Remove the mongodb-cluster-action step
  • Remove the mongo-uri-env-name input
  • Remove the MONGO_URI environment variable
.github/workflows/codspeed.yml