Mastering Jest Matchers: A Comprehensive Testing Challenge
Jest's matchers are the core of writing effective and readable tests. This challenge focuses on utilizing Jest's powerful matchers to verify various aspects of your code, ensuring its correctness and robustness. You'll be implementing tests for a simple utility function, demonstrating your ability to use different matchers to cover various scenarios.
Problem Description
You are tasked with writing Jest tests for a utility function called formatString. This function takes a string as input and applies the following rules:
- If the input string is empty, it should return an empty string.
- If the input string is not empty, it should capitalize the first letter and add an exclamation mark at the end.
Your goal is to write a suite of Jest tests that thoroughly verify the formatString function's behavior, using a variety of Jest matchers to cover different input scenarios and expected outputs. You should aim for high test coverage and ensure that your tests are clear, concise, and easy to understand.
Examples
Example 1:
Input: ""
Output: ""
Explanation: An empty string should return an empty string.
Example 2:
Input: "hello"
Output: "Hello!"
Explanation: A non-empty string should be capitalized and have an exclamation mark appended.
Example 3:
Input: "world"
Output: "World!"
Explanation: Another non-empty string example.
Example 4:
Input: "typescript"
Output: "Typescript!"
Explanation: Demonstrates capitalization of a longer word.
Constraints
- The
formatStringfunction will always receive a string as input. - Your tests should cover both positive and negative scenarios (empty string vs. non-empty string).
- You should use at least 5 different Jest matchers in your test suite (e.g.,
toBe,toEqual,toMatch,toBeDefined,toBeNull). - The test suite should be well-organized and readable.
Notes
- Consider using
describeblocks to group related tests. - Think about edge cases, such as strings with leading/trailing spaces (although the problem description doesn't explicitly require handling these, it's good practice to consider them).
- Focus on writing tests that are easy to understand and maintain. Clear test descriptions are crucial.
- You'll need to implement the
formatStringfunction as well. It's a simple function, but it's necessary for the tests to run. - Remember to install Jest and TypeScript if you haven't already. A basic
tsconfig.jsonandjest.config.jswill be helpful.
// formatString.ts
export function formatString(input: string): string {
if (!input) {
return "";
}
return input.charAt(0).toUpperCase() + input.slice(1) + "!";
}
// formatString.test.ts
import { formatString } from './formatString';
describe('formatString', () => {
it('should return an empty string when given an empty string', () => {
expect(formatString('')).toBe('');
});
it('should capitalize the first letter and add an exclamation mark', () => {
expect(formatString('hello')).toBe('Hello!');
});
it('should handle strings with lowercase letters', () => {
expect(formatString('world')).toBe('World!');
});
it('should handle strings with uppercase letters', () => {
expect(formatString('TypeScript')).toBe('Typescript!');
});
it('should handle strings with mixed case letters', () => {
expect(formatString('tEsTiNg')).toBe('Testing!');
});
it('should not modify strings that already have an exclamation mark', () => {
expect(formatString('test!')).toBe('Test!');
});
it('should be a function', () => {
expect(typeof formatString).toBe('function');
});
it('should return a string', () => {
expect(formatString('test')).toBeTypeOf('string');
});
});