Implementing Testing Conventions in JavaScript Development
Create a standard for writing and structuring tests in JavaScript to ensure robust and reliable code.
0 likes
4 views
Rule Content
--- description: Enforce standardized testing conventions in JavaScript development to ensure robust and reliable code. globs: ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] tags: [javascript, testing, best-practices] priority: 1 version: 1.0.0 --- # Implementing Testing Conventions in JavaScript Development ## Context - Applicable to all JavaScript and TypeScript projects. - Ensures consistency and reliability in test code across the codebase. ## Requirements - **Test Structure**: Organize tests using the `describe` and `it` functions to clearly define test suites and individual test cases. - **Test Naming**: Use descriptive and clear names for test cases to convey their purpose effectively. - **Avoid Overuse of Hooks**: Limit the use of `beforeAll`, `beforeEach`, `afterAll`, and `afterEach` hooks to maintain test clarity and prevent complexity. ([blog.logrocket.com](https://blog.logrocket.com/javascript-testing-best-practices/?utm_source=openai)) - **Mocking**: Mock external dependencies and modules to isolate the unit under test, but avoid excessive mocking to ensure tests remain meaningful. ([genqe.ai](https://genqe.ai/ai-blogs/2025/02/22/13-javascript-testing-best-practices-you-should-know-2025/?utm_source=openai)) - **Assertions**: Utilize a variety of assertions to validate different aspects of the code's behavior. - **Error Handling**: Test both expected and unexpected scenarios, including edge cases and error conditions. - **Code Coverage**: Aim for a high percentage of code coverage, ensuring that all critical paths are tested. - **Continuous Integration**: Integrate tests into the CI/CD pipeline to automatically run tests on new commits and pull requests. ## Examples // Good: Organized test suite with clear structure and descriptive names describe('User Authentication Module', () => { describe('Login Functionality', () => { it('should authenticate user with valid credentials', () => { // Test implementation }); it('should reject user with invalid credentials', () => { // Test implementation }); }); describe('Registration Functionality', () => { it('should register user with valid data', () => { // Test implementation }); it('should not register user with missing fields', () => { // Test implementation }); }); }); // Bad: Overuse of hooks leading to complex and hard-to-follow tests describe('User Service', () => { beforeAll(() => { // Setup code }); beforeEach(() => { // Setup code }); afterEach(() => { // Teardown code }); afterAll(() => { // Teardown code }); it('should create a new user', () => { // Test implementation }); it('should delete a user', () => { // Test implementation }); });