Skip to content

Advanced Topics: Testing

alessbarb edited this page Sep 22, 2023 · 4 revisions

Introduction

Effective testing is an integral part of maintaining code quality and ensuring the functionality of your enhancements or bug fixes. This guide will walk you through our testing methodology, tools, and best practices to help you get started with writing and running tests for error-enhanced.

Testing Framework

We primarily use Jest as our testing framework due to its flexibility, ease of use, and integration with TypeScript.

Running Tests

To run the existing test suite, execute the following command:

npm run test

This will execute all unit and integration tests, providing a summary of test cases that passed or failed.

Writing Unit Tests

Unit tests focus on testing individual modules or functions in isolation. When writing unit tests for error-enhanced, make sure to:

  1. Cover all edge cases.
  2. Check for both expected successes and failures.

Example:

import { ErrorEnhanced, IdentifiersEnhancer } from '@labrynx/error-enhanced';

describe('IdentifiersEnhancer', () => {
  it('should generate a unique identifier', () => {
    const error = new ErrorEnhanced([
      new IdentifiersEnhancer()
    ]);

    expect(typeof error.id).toBe('string');
    expect(error.id).toHaveLength(36);  // Assuming UUID
  });
});

Writing Integration Tests

Integration tests aim to test the interaction between multiple components. These tests are crucial for ensuring that the various enhancers and utilities work well together.

Example:

import { ErrorEnhanced, IdentifiersEnhancer, Severity } from '@labrynx/error-enhanced';

describe('Integration Test', () => {
  it('should correctly set and retrieve enhanced properties', () => {
    const error = new ErrorEnhanced([
      new IdentifiersEnhancer()
    ]);

    error.setErrorCode(404).setSeverity(Severity.HIGH);

    expect(error.id).toBeDefined();
    expect(error.errorCode).toBe(404);
    expect(error.severity).toBe(Severity.HIGH);
  });
});

Best Practices

  • Always write tests before fixing a bug or adding a new feature.
  • Keep your tests small and focused.
  • Mock external dependencies to ensure tests run quickly and are not flaky.

For any further questions or issues, please consult the FAQs or feel free to contribute to the repository.

Feel free to navigate through other topics using the sidebar, or you can return to the Home page for an overview.


Clone this wiki locally