Testing
Testing for the project
About
This project uses
- vitest to write tests
- react-testing-library to write tests for the React components
Running tests
To run the tests, you need to run the following command first from the root of the monorepo project:
docker-compose upThat will start the local PostgreSQL database that will be used by the tests.
Then run the tests with the following command from the root of the monorepo project:
pnpm run testCoverage
To run the tests for all apps and packages with coverage, you can run the following command from the root of the monorepo project:
pnpm run coverage:combinedThe coverage report location will be displayed in the output of the command.
You can also run the tests inside each app or package. For example, to run the tests inside the web app, you can run the following command from the apps/web directory:
pnpm run testTo run the tests with coverage inside the web app, you can run the following command from the apps/web directory:
pnpm run coverageReact Server Components
Unfortunately, react-testing-library does not support React Server Components (RSC) yet. Fortunately, someone already created a small helper to test RSC. You can find the helper in the @workspace/utils package, in the test-async-rsc.ts file.
Notes
- In this project, you will see that we write
TEST#<number>comments in the code to indicate the test case. This is very useful to keep track of the test cases and to know quickly which part of the code is being tested in the test cases. - We do something that is not very common here. We do not mock the prisma client in most of the tests. Instead, we use a test database. Each test will create a new schema in the test database. See packages/database/src/test-utils.ts for more information. Then the function that is being tested will use the test database through the dependency injection mechanism, i.e., async local storage to provide the database context. See the tests for more information.