Tests
This document outlines the guidelines for writing tests.
Runners​
We use Jest as our test runner and React Testing Library for testing React components.
Categories​
We divide our tests into two categories: unit tests and e2e tests. Unit tests are for testing individual functions, and e2e tests are for testing the entire application.
We write unit tests in the tests/unit/tests
folder and e2e tests in the tests/e2e/tests
folder, both ending with the .test.ts
or .test.tsx
extension.
File Structure​
The inital file structure for tests in your package should be as follows:
tests/
e2e/
tests/
myFunction.test.ts
babel.config.cjs
jest.config.ts
unit/
tests/
myOtherFunction.test.ts
babel.config.cjs
jest.config.ts
tsconfig.json
jest.config.ts
Configuration​
-
Follow the installation instructions from @ttoss/config to set up Jest.
-
Define the configuration for Jest in the
jest.config.ts
file at the root of the package. This configuration set up Jest projects.import { jestRootConfig } from '@ttoss/config';
export default jestRootConfig(); -
For unit tests, create a
jest.config.ts
file in thetests/unit
folder.import { jestUnitConfig } from '@ttoss/config';
export default jestUnitConfig(); -
For e2e tests, create a
jest.config.ts
file in thetests/e2e
folder.import { jestE2EConfig } from '@ttoss/config';
export default jestE2EConfig(); -
Create a Babel configuration file in each test folder:
tests/unit/babel.config.cjs
andtests/e2e/babel.config.cjs
. Jest needs Babel to transpile the code before running the tests.const { babelConfig } = require('@ttoss/config');
const config = babelConfig({});
module.exports = config; -
Create a
tsconfig.json
file attests/tsconfig.json
with the following content:{
"extends": "@ttoss/config/tsconfig.test.json",
"compilerOptions": {
"paths": {
"src/*": ["../src/*"],
"tests/*": ["./*"]
}
}
}This way, you can import files from the
src
folder in your tests. For example:import { myFunction } from 'src/myFunction';
-
Add the following scripts to the
package.json
file:{
"scripts": {
"e2e": "jest --projects tests/e2e",
"test": "jest --projects tests/unit"
}
} -
Finnaly, write your tests in the
tests/unit/tests
andtests/e2e/tests
folders. -
Run the unit tests with the following command:
pnpm test
Or run only the e2e tests with:
pnpm e2e