Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Page Properties Report
cqllabel = "unit_tests" and space = currentSpace ( )

🛠️ Testing Frameworks and Tools

Framework Details

  • Just is the testing framework used

  • Babel is used It is used in this project to transform modern JavaScript and TypeScript code for testing purposes

Version Information

  • Jest: 29.7.0

  • Babel: Core version 7.22.20 with specific presets and plugins for React and TypeScript.

🧰 Setup and Configuration

Installation Instructions

  • Prerequisites

    • Node.js installed on your machine.

    • npm (Node Package Manager) available.

  • Install Project Dependencies

    • Navigate to the root directory of the project.

    • Run the following command to install all project dependencies, including those required for testing:

      Code Block
      npm install
  • Install Jest and Babel Dependencies

    Code Block
    npm install --save-dev jest babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript

Jest Configuration

Configuration file: jest.config.js

...

These configurations help tailor Jest to work effectively with the project's TypeScript and React code, ensuring a smooth testing process.

Babel Configuration

Configuration file: babel.config.jest.js

...

📒 Test Organization and Structure

Directory Structure

root/tests/name.test.tsx or .ts

...

🗒️ Writing Unit Tests

Test Structure

A typical test file in this project follows a structured format to ensure clarity and maintainability:

  • Imports: Import necessary libraries and components at the beginning of the file. This includes React, testing utilities from @testing-library/react, and any components or modules being tested.

  • Mocks: Define any mocks needed for the test. This can include mocking components, functions, or APIs that the component interacts with.

  • Setup and Teardown: Use beforeAll, beforeEach, afterAll, and afterEach hooks to set up and clean up any global state or configurations needed for the tests.

  • Test Suite: Use describe to group related tests together. This helps organize tests and provides a clear context for each group of tests.

  • Individual Tests: Use test or it to define individual test cases. Each test should focus on a single aspect of the component's behavior.

Test Naming Conventions

  • Test Files: Name test files to reflect the component or module being tested. For example, if testing a component named CopyButton, the test file should be named CopyButton.test.tsx.

  • Test Cases: Name test cases to clearly describe the expected behavior. Use a consistent format, such as "should [do something] when [condition]". For example, "button should be disabled when no content is provided".

Common Test Patterns

  • Setup and Teardown: Use beforeEach to reset mocks and any shared state before each test. This ensures that tests do not interfere with each other.

  • Mocking: Use jest.mock to mock dependencies that the component interacts with. This can include components, functions, or APIs. In the provided test, the IconButton component and the clipboard API are mocked.

  • Assertions: Use assertions to verify that the component behaves as expected. Common assertions include checking if elements are present in the document, if functions are called with the correct arguments, and if the component's state changes as expected.

  • Async Testing: Use waitFor to handle asynchronous operations, ensuring that assertions are made after the component has updated.

  • Timers: Use jest.useFakeTimers and jest.advanceTimersByTime to test components that rely on timing functions, such as setTimeout.

🏃‍♀️ Running Tests

Command Line Instructions

To run tests in different environments, use the following commands. These commands are defined in the package.json under the scripts section, allowing for easy execution of tests in different environments and facilitating efficient local testing.

...

This approach captures the test results and console logs in the jest-output.log file, which is particularly useful when dealing with a large number of tests. It allows for easier troubleshooting and review of test outputs compared to viewing them directly in the terminal.

Continuous Integration

The unit tests are integrated into the CI/CD pipeline to ensure code quality and prevent regressions before deploying to staging or production environments.

...