Unit tests
This section provide an in-depth overview of unit testing procedures for Airs. It outlines the tools, commands, and best practices essential for ensuring the reliability and functionality of individual components within the codebase.
Our unit test are seamlessly integrated into our
CI/CD pipeline,
running automatically every time a developer pushes changes to our repository.
They also can be executed locally. To run unit tests in Airs locally,
navigate to the universe
repository and execute the following command:
When writing unit tests, follow these steps to ensure that the tests are repeatable, fast, independent, and descriptive:
-
Test file: We organize our unit tests within each component, scene, or view of Airs. Each component directory contains its respective unit tests, tipically named
index.test.tsx
. -
Write the test: Once the file is ready, you can start writing the test cases. Consider the purpose of the function, method, or class that you want to test, and think about its behavior when different inputs are provided. Identify extreme scenarios to test within the test. These scenarios will form our test cases and are essential for writing our essential for writing our assertions.
With this setup, yo can run all tests using the following command:
To run a specific test, use the following command:
-
Mocks: When writing unit tests for Airs, it’s essential to identify what needs to be mocked. Tipically, we mock functions from Gatsby or its plugins. However, any package can be mocked following conventions of the mocks directory.
-
Mock data: To provide the necessary data for our unit tests, we utilize Jest. These allow us to have mock data available from
airs/front/__mocks__
files. -
Assertions: Test the expected behavior of the code. We use assertions to validate results, the number of function or mock calls, and the arguments used in mocks. Once your test is ready, ensure code quality by utilizing a linter:
Mocks are created using Jest functions in the airs/front/__mocks__
directory.
To use the Gatsby GraphQL API in tets, the airs/front/src/test-utils/mocks.ts
file is defined. When adding a field to the API, it must be added to the
airs/front/src/global.d.ts
schema and then added to
airs/front/src/test-utils/mocks.ts
to use it in tests.