Hone logo
Hone
Problems

Mastering Jest Assertions: Testing String Equality

Jest is a powerful JavaScript testing framework, and a core part of writing effective tests is using assertions to verify that your code behaves as expected. This challenge focuses on using Jest's expect assertion to test for string equality, a fundamental operation in many applications. You'll be writing Jest tests to ensure that functions return the correct strings under various conditions.

Problem Description

You are tasked with writing Jest tests for a series of functions that manipulate strings. Your goal is to use the expect assertion with appropriate matchers (like toBe, toEqual, or toMatch) to verify that the functions return the expected string values. This exercise will solidify your understanding of Jest's assertion capabilities and how to use them to ensure code correctness.

Key Requirements:

  • Write Jest tests using expect.
  • Use appropriate Jest matchers to compare strings. Consider toBe for strict equality and toEqual for deep equality (though in this case, toBe is generally sufficient for simple string comparisons).
  • Ensure your tests cover various scenarios, including positive and negative cases.
  • Tests should be readable and well-documented.

Expected Behavior:

Each test should assert that the actual output of the function matches the expected string value. Tests should fail if the actual output does not match the expected output.

Edge Cases to Consider:

  • Empty strings.
  • Strings with leading or trailing whitespace.
  • Case sensitivity (consider if case should be ignored or not).
  • Null or undefined inputs (if the functions are designed to handle them). Assume functions will not return null or undefined.

Examples

Example 1:

Input: Function: `function greet(name: string): string { return `Hello, ${name}!`; }`
Test: `expect(greet("World")).toBe("Hello, World!");`
Output: Test passes.
Explanation: The `greet` function returns "Hello, World!" when given "World" as input. The `toBe` matcher verifies that the actual output matches the expected string.

Example 2:

Input: Function: `function reverseString(str: string): string { return str.split("").reverse().join(""); }`
Test: `expect(reverseString("hello")).toBe("olleh");`
Output: Test passes.
Explanation: The `reverseString` function reverses the input string. The `toBe` matcher confirms that the reversed string is "olleh".

Example 3:

Input: Function: `function capitalize(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1); }`
Test: `expect(capitalize("world")).toBe("World");`
Output: Test passes.
Explanation: The `capitalize` function capitalizes the first letter of the input string. The `toBe` matcher verifies that the capitalized string is "World".

Constraints

  • You must use Jest's expect assertion.
  • The functions to be tested are provided below. You are not allowed to modify the functions themselves.
  • All tests must pass.
  • Tests should be concise and focused on testing a single aspect of each function.

Notes

  • Consider using describe blocks to group related tests.
  • Use clear and descriptive test names.
  • Think about the different ways a function could fail and write tests to cover those scenarios.
  • While toEqual can be used, toBe is generally preferred for simple string comparisons due to its stricter equality check.

Functions to Test (Do NOT modify these):

function greet(name: string): string {
  return `Hello, ${name}!`;
}

function reverseString(str: string): string {
  return str.split("").reverse().join("");
}

function capitalize(str: string): string {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

function stringLength(str: string): number {
  return str.length;
}

function isPalindrome(str: string): boolean {
  const reversed = reverseString(str);
  return str === reversed;
}
Loading editor...
typescript