Enhancing Code Quality: Achieving 100% Test Coverage with Jest
Writing comprehensive tests is crucial for ensuring the reliability and maintainability of any software project. This challenge focuses on leveraging Jest to achieve a high level of test coverage for a given TypeScript function. By understanding how to write tests that cover all logical paths within your code, you can identify potential bugs and improve the overall quality of your application.
Problem Description
Your task is to write a set of Jest tests for a provided TypeScript function that calculates the area of different geometric shapes. The goal is to achieve 100% statement, branch, and function coverage for this function. You will need to consider various inputs, including valid shape types and dimensions, as well as edge cases and invalid inputs, to ensure all parts of the function are exercised.
What needs to be achieved:
- Write Jest unit tests for the
calculateShapeAreafunction. - Achieve 100% test coverage (statement, branch, and function) as reported by Jest.
Key requirements:
- The tests must be written in TypeScript.
- Use Jest as the testing framework.
- The provided function
calculateShapeAreamust be the target of your tests. - Handle different shape types (Circle, Rectangle, Triangle).
- Consider valid inputs for each shape.
- Consider edge cases such as zero dimensions.
- Consider invalid inputs for shape types and dimensions.
Expected behavior:
- For valid inputs, the function should return the correct area.
- For invalid inputs (e.g., unknown shape type, negative dimensions), the function should throw a specific error.
Any important edge cases to consider:
- Shapes with zero dimensions (e.g., a circle with radius 0).
- Inputs that are not valid numbers for dimensions.
- Invalid string inputs for shape types.
Examples
Example 1: Valid Circle Input
// Assume this is the function you are testing
function calculateShapeArea(shapeType: string, dimensions: Record<string, number>): number {
if (shapeType === 'Circle') {
const radius = dimensions['radius'];
if (radius === undefined || typeof radius !== 'number' || radius < 0) {
throw new Error('Invalid radius for Circle.');
}
return Math.PI * radius * radius;
} else if (shapeType === 'Rectangle') {
const width = dimensions['width'];
const height = dimensions['height'];
if (width === undefined || typeof width !== 'number' || width < 0 || height === undefined || typeof height !== 'number' || height < 0) {
throw new Error('Invalid dimensions for Rectangle.');
}
return width * height;
} else if (shapeType === 'Triangle') {
const base = dimensions['base'];
const height = dimensions['height'];
if (base === undefined || typeof base !== 'number' || base < 0 || height === undefined || typeof height !== 'number' || height < 0) {
throw new Error('Invalid dimensions for Triangle.');
}
return 0.5 * base * height;
} else {
throw new Error('Unknown shape type.');
}
}
// Your test should assert the following:
const shapeType = 'Circle';
const dimensions = { radius: 5 };
// Expected output: Math.PI * 5 * 5 (approximately 78.54)
Explanation: This example tests a standard, valid input for a circle. Your test should verify that the function correctly calculates the area using the provided radius.
Example 2: Invalid Rectangle Input (Negative Dimension)
// Assume the same calculateShapeArea function as above
// Your test should assert the following:
const shapeType = 'Rectangle';
const dimensions = { width: 10, height: -5 };
// Expected output: Throws an Error: 'Invalid dimensions for Rectangle.'
Explanation: This example tests an edge case where one of the dimensions for a rectangle is negative, which is an invalid input. Your test should confirm that the function throws the expected error.
Example 3: Unknown Shape Type
// Assume the same calculateShapeArea function as above
// Your test should assert the following:
const shapeType = 'Square'; // An unsupported shape
const dimensions = { side: 4 };
// Expected output: Throws an Error: 'Unknown shape type.'
Explanation: This example tests the scenario where an unsupported shape type is provided. Your test should verify that the function handles this case by throwing the appropriate error.
Constraints
- The
calculateShapeAreafunction will be provided as a separate TypeScript file. - Your Jest tests should reside in a
*.test.tsfile within a designated test directory. - You must ensure that running
jest --coveragereports 100% coverage for thecalculateShapeAreafunction. - Inputs for dimensions will be numbers or undefined.
- Shape types will be strings.
- Performance is not a primary concern for this challenge; focus on correctness and coverage.
Notes
- You will need to have Jest installed and configured in your project.
- You can use
jest.fn()if you need to mock any dependencies, though this specific function is self-contained. - Pay close attention to the error messages thrown by the function, as they are part of the expected behavior.
- Consider using
describeanditblocks in Jest to organize your tests logically by shape type and by valid/invalid inputs. - Remember to import the
calculateShapeAreafunction into your test file.