This project uses Jest as the testing framework with TypeScript support. Tests help ensure code quality and catch regressions as the project evolves.
jest- Testing framework@types/jest- TypeScript types for Jestts-jest- Jest transformer for TypeScript
Jest is configured in jest.config.js with the following settings:
- TypeScript support via
ts-jestpreset - Test files located in
src - Coverage reporting enabled
- 10 second timeout for tests
# Run all tests
npm test
# Run tests in watch mode (re-runs on file changes)
npm run test:watch
# Run tests with coverage report
npm run test:coverage
# Run tests in CI mode (no watch, with coverage)
npm run test:ciTests should follow these naming conventions:
__tests__/directory for test files- Files ending with
.test.tsor.spec.ts - Test files should be co-located with the code they test
import { functionToTest } from '../module';
describe('Module Name', () => {
describe('functionToTest', () => {
it('should handle normal case', () => {
const result = functionToTest('input');
expect(result).toBe('expected output');
});
it('should handle edge case', () => {
const result = functionToTest('');
expect(result).toBe('');
});
it('should throw error for invalid input', () => {
expect(() => functionToTest(null)).toThrow('Invalid input');
});
});
});Test individual functions in isolation:
it('should parse valid JSON', () => {
const input = '{"name": "test"}';
const result = parseJSON(input);
expect(result).toEqual({ name: 'test' });
});Use Jest mocks for external dependencies:
jest.mock('fs');
const mockReadFileSync = require('fs').readFileSync;
mockReadFileSync.mockReturnValue('file content');Test error conditions and edge cases:
it('should handle file not found', () => {
const error = new Error('ENOENT');
(error as any).code = 'ENOENT';
mockReadFileSync.mockImplementation(() => { throw error; });
expect(() => readFile('/nonexistent')).toThrow('File not found');
});The project aims for good test coverage. Current coverage:
- Parsers: ~26% (markdown parser well tested)
- Generators: ~3% (express-app generator tested)
- Utils: ~24% (filesystem utilities partially tested)
Located in src/parsers/__tests__/markdown.test.ts
- Tests parsing of valid readerspec files
- Tests error handling for missing code blocks
- Tests JSON parsing errors
- Tests edge cases like empty blocks
Located in src/generators/__tests__/express-app.test.ts
- Tests generation of Express.js applications
- Tests handling of multiple resources
- Tests middleware and route generation
- Tests package.json generation
Located in src/utils/__tests__/filesystem.test.ts
- Tests file reading functionality
- Tests error handling for file operations
- Tests file path parsing
- Test Organization: Group related tests using
describeblocks - Test Names: Use descriptive test names that explain the expected behavior
- Assertions: Use specific assertions (
toBe,toEqual,toContain) rather than generic ones - Mocking: Mock external dependencies to isolate the code under test
- Edge Cases: Test error conditions, boundary values, and unexpected inputs
- Coverage: Aim for high test coverage, especially for critical business logic
When adding new functionality:
- Create test files in the appropriate
__tests__directory - Write tests before or alongside the implementation (TDD approach)
- Ensure tests cover normal operation, edge cases, and error conditions
- Run the test suite to ensure all tests pass
- Check coverage to identify untested code paths
Tests are automatically run in CI environments using:
npm run test:ciThis command:
- Runs all tests without watch mode
- Generates coverage reports
- Exits with non-zero code if tests fail
- Is suitable for automated build systems
TypeScript Errors: Ensure @types/jest is installed and Jest is configured for TypeScript
Mock Not Working: Check that mocks are set up before the function under test is called
Coverage Issues: Verify that test files are in the correct location and follow naming conventions
- Use
console.login tests for debugging (will show in test output) - Run individual test files:
npx jest path/to/test.ts - Use Jest's
--verboseflag for more detailed output - Check Jest configuration in
jest.config.js