Files
ISA-Frontend/.github/testing-instructions.md
2025-04-22 12:23:39 +02:00

1.9 KiB

Testing Instructions

Framework and Tools

  • Use Jest as the testing framework.
  • For unit tests, utilize Spectator to simplify Angular component testing.

Guidelines

  1. Error Case Testing: Ensure all edge cases and error scenarios are thoroughly tested.
  2. Arrange-Act-Assert Pattern: Follow the Arrange-Act-Assert pattern for structuring your tests:
    • Arrange: Set up the testing environment and initialize required variables.
    • Act: Execute the functionality being tested.
    • Assert: Verify the expected outcomes.

Best Practices

  • Write clear and descriptive test names.
  • Ensure tests are isolated and do not depend on each other.
  • Mock external dependencies to avoid side effects.
  • Aim for high code coverage without compromising test quality.

Example Test Structure

// Example using Jest and Spectator
import { createComponentFactory, Spectator } from '@ngneat/spectator';
import { MyComponent } from './my-component.component';

describe('MyComponent', () => {
  let spectator: Spectator<MyComponent>;
  const createComponent = createComponentFactory(MyComponent);

  beforeEach(() => {
    spectator = createComponent();
  });

  it('should display the correct title', () => {
    // Arrange
    const expectedTitle = 'Hello World';

    // Act
    spectator.component.title = expectedTitle;
    spectator.detectChanges();

    // Assert
    expect(spectator.query('h1')).toHaveText(expectedTitle);
  });

  it('should handle error cases gracefully', () => {
    // Arrange
    const invalidInput = null;

    // Act
    spectator.component.input = invalidInput;

    // Assert
    expect(() => spectator.component.processInput()).toThrowError('Invalid input');
  });
});

Additional Resources