-
Notifications
You must be signed in to change notification settings - Fork 15
Unit tests
Elwin Schmitz edited this page Oct 24, 2023
·
4 revisions
Unit-tests are a type of software testing that evaluate that a single unit works as expected.
Unit-tests are conducted before integration tests, which tests if different components integrate well together, while unit-tests verify that all individual components are functioning in isolation.
- A test-file is set up in an individual Nest.js component-folder as e.g.
src/notes/notes.service.spec.ts
.- The
.service.
part shows that we test the code of the service (notes.service.ts
), not the controller. - The
.spec.
part is to distinguish it from Integration/API-test (which follow a*.test.ts
-pattern).
- The
- Tests are written in TypeScript and run using Jest, see
notes.service.spec.ts
as an example.
All configuration/set-up to run the unit-tests is contained in the Docker-container of the 121-service.
So be sure to run all commands below within the shell of the Docker-container, or prefixed with docker exec 121-service
- To run all unit-tests, use:
npm run test:unit:all
- To run an individual file:
npm run test:unit:all <grep-pattern-to-match-filename/path>
, for examplenpm run test:unit:all 'notes'
- To watch for local changes:
npm run test:unit:watch
- Other options are in the Jest CLI docs: https://jestjs.io/docs/cli