Skip to main content

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​

  1. Follow the installation instructions from @ttoss/config to set up Jest.

  2. 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();
  3. For unit tests, create a jest.config.ts file in the tests/unit folder.

    import { jestUnitConfig } from '@ttoss/config';

    export default jestUnitConfig();
  4. For e2e tests, create a jest.config.ts file in the tests/e2e folder.

    import { jestE2EConfig } from '@ttoss/config';

    export default jestE2EConfig();
  5. Create a Babel configuration file in each test folder: tests/unit/babel.config.cjs and tests/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;
  6. Create a tsconfig.json file at tests/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';
  7. Add the following scripts to the package.json file:

    {
    "scripts": {
    "e2e": "jest --projects tests/e2e",
    "test": "jest --projects tests/unit"
    }
    }
  8. Finnaly, write your tests in the tests/unit/tests and tests/e2e/tests folders.

  9. Run the unit tests with the following command:

    pnpm test

    Or run only the e2e tests with:

    pnpm e2e