Make sure you are using the minimum required version of the plugin:
@codspeed/vitest-plugin>=3.1.0CodSpeedHQ/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 followingCat model is defined:
src/cats/schemas/cat.schema.ts
CatsController exposes the following endpoints:
src/cats/cats.controller.ts
Complete setup with Docker
Setup SWC + Vitest
To useswc 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
cats endpoints:
GET /cats: retrieve all the catsGET /cats/name/:name: retrieve all the cats with the given nameGET /cats/breed/:breed: retrieve all the cats with the given breedGET /cats/age/greater/:age: retrieve all the cats with an age greater than the given age
Setup Docker locally
Add the following file to the root of the project:docker-compose.yml
Run the benchmarks locally
Add the following script to yourpackage.json:
package.json
terminal
Run the benchmarks in the CI
Add the following file to the project:.github/workflows/codspeed.yml
Setup using testcontainers
Instead of relying on an externally provided Docker instance, we can leveragetestcontainers 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 thetestcontainers 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.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:Enforce a check that the correct environment variables are set
Enforce a check that the correct environment variables are set
To make We will add a function to enforce that they are set when running And use it at the top of the the Now the execution will stop with an explicit error message if the environment variables are not set when running on macOS.
testcontainers work on macOS with colima, the following environment variables need to be set:vitest. Add the following function to your src/testUtils/setup-vitest.ts file:src/testUtils/setup-vitest.ts
setupMongoDB function:src/testUtils/setup-vitest.ts
setupFiles entry in vite.config.e2e.mts:
vitest.config.e2e.mts
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 thecodspeed.yml file to the following:
- Remove the
mongodb-cluster-actionstep - Remove the
mongo-uri-env-nameinput - Remove the
MONGO_URIenvironment variable
.github/workflows/codspeed.yml