Unit test guidelines
Guidelines for running and writing unit tests.
Running unit tests
-
Install package dependencies:
npm install -
Build carta-protobuf and WebAssembly libraries:
npm run build-protobufnpm run build-libsnpm run build-wrappers -
Run unit tests:
npm testBy default, Jest runs tests related to changed files.
To display individual test results, use the
--verboseflag:npm test --verboseFor more options available in Jest, please refer to the Jest documentation.
Writing unit tests
Structures
-
Directory structure: colocate the test file in the same directory and name with
.test.ts/tsxsuffix. For example,.└── src└── components└── AComponent├── AComponent.tsx├── AComponent.scss└── AComponent.test.tsx└── utilities└── math├── math.ts└── math.test.ts -
Test code structure: use
describeto structure the tests. For example,describe("[unit]", () => {test("[expected behavior]", () => {});describe("[sub unit]", () => {test("[expected behavior]", () => {});});}); -
Make sure to implement low-level tests that focus on a certain class or function. Mock imported classes and functions with Jest when necessary.
-
TypeScript enum: import TypeScript enum without index files to avoid compile failure.
Testing React components
- Avoid mocking blueprint.js objects to prevent having complex setups.
- Avoid testing snapshots to prevent having large files in the codebase.
- Follow the order of priority suggested by React Testing Library when querying elements.