Defining Testing Conventions for JavaScript Functions

Create a consistent approach to writing and organizing tests for your JavaScript functions to improve coverage and reliability.

0 likes
39 views

Rule Content

Title: Defining Testing Conventions for JavaScript Functions

Description: Establish a consistent approach to writing and organizing tests for JavaScript functions to improve coverage and reliability.

Category: JavaScript Cursor Rules

Category Context: Covers structure, syntax, and consistency in JavaScript projects. Helps avoid bugs, enforce style, and improve code readability across files.

Rule:

- **Test Framework**:
  - Use [Jest](https://jestjs.io/) as the primary testing framework for JavaScript functions.

- **Test Structure**:
  - Organize test files in a `__tests__` directory adjacent to the source files, or use a `.test.js` suffix for test files.
  - Ensure each test file corresponds to a specific module or function.

- **Test Naming Conventions**:
  - Name test files to match the module they test, appending `.test.js` (e.g., `mathUtils.test.js` for `mathUtils.js`).
  - Use descriptive names for test cases that clearly state the expected behavior.

- **Test Coverage**:
  - Aim for a minimum of 80% code coverage, including branches, functions, and lines.
  - Utilize Jest's coverage tools to monitor and report coverage metrics.

- **Test Practices**:
  - Write unit tests for all functions, focusing on individual units of logic.
  - Implement integration tests for modules that interact with external systems or dependencies.
  - Use [Test-Driven Development (TDD)](https://en.wikipedia.org/wiki/Test-driven_development) practices where feasible.

- **Mocking and Stubbing**:
  - Use Jest's mocking capabilities to isolate units of code during testing.
  - Stub external dependencies to ensure tests are deterministic and do not rely on external systems.

- **Continuous Integration**:
  - Integrate tests into the continuous integration pipeline to run on every commit.
  - Ensure that the pipeline fails if tests do not pass or if coverage thresholds are not met.

- **Documentation**:
  - Document testing strategies and conventions in the project's `README.md` or a dedicated `TESTING.md` file.
  - Include instructions on how to run tests and interpret results.

By adhering to these conventions, JavaScript projects will maintain high-quality, reliable, and maintainable codebases.