Skip to main content

Testing

Testing guidelines and practices across Sprout repositories.

Test Structure

Most repositories use Jest for testing:

// Example test file: *.spec.ts
import { Test } from '@nestjs/testing';
import { MyService } from './my.service';

describe('MyService', () => {
let service: MyService;

beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [MyService],
}).compile();

service = module.get<MyService>(MyService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});

Running Tests

sprout_backend & sprout_etl

# Run all tests
bun test

# Watch mode
bun run test:watch

# Coverage
bun run test:cov

# Debug mode
bun run test:debug

# E2E tests
bun run test:e2e

Other Repositories

# Run tests (if configured)
bun test
# or
npm test

Test Types

Unit Tests

  • Test individual functions and classes
  • Mock external dependencies
  • Fast execution
  • Located in *.spec.ts files

Integration Tests

  • Test component interactions
  • May use test database
  • Slower than unit tests

E2E Tests

  • Test full workflows
  • Use real or test database
  • Located in test/ or e2e/ directories

Test Configuration

Jest Configuration

Most repos have Jest configured in package.json:

{
"jest": {
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/$1"
},
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"testEnvironment": "node"
}
}

Testing Best Practices

1. Test Isolation

  • Each test should be independent
  • Use beforeEach and afterEach for setup/teardown
  • Don't rely on test execution order

2. Mocking

// Mock external services
jest.mock('@nestjs/axios');
jest.mock('meilisearch');

3. Test Data

  • Use factories for test data
  • Clean up test data after tests
  • Use transactions for database tests

4. Assertions

  • Use descriptive test names
  • Test one thing per test
  • Use appropriate matchers

Testing with Docker

For integration tests that need databases:

# Start test database
docker-compose -f docker-compose.test.yml up -d

# Run tests
bun test

# Cleanup
docker-compose -f docker-compose.test.yml down

Coverage

View Coverage Report

# Generate coverage
bun run test:cov

# Coverage report is in coverage/ directory
open coverage/lcov-report/index.html

Coverage Goals

  • Aim for >80% coverage
  • Focus on critical paths
  • Don't sacrifice quality for coverage

Continuous Integration

Tests should run in CI:

# Example GitHub Actions
- name: Run tests
run: bun test

- name: Check coverage
run: bun run test:cov

Next Steps