Unit tests - global-121/121-platform GitHub Wiki

What are Unit-tests?

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.

How to write Unit-tests

  • 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).
  • Tests are written in TypeScript and run using Jest, see notes.service.spec.ts as an example.

How to run Unit-tests

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 example npm 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