Hone logo
Hone
Problems

Implementing test.skip in Jest for Selective Test Execution

Jest provides the test.skip function to temporarily disable a test without removing it from the test suite. This is incredibly useful for debugging, focusing on specific areas of functionality, or excluding tests that rely on external resources that might be unavailable. Your task is to implement a simplified version of test.skip that mimics its core functionality within a Jest environment.

Problem Description

You need to create a function called skipTest that accepts a description string and a test function as arguments. When skipTest is called, it should prevent the provided test function from being executed during the Jest test run, but still report the test as skipped in the Jest output. The function should essentially wrap the provided test function in a way that causes it to be skipped.

Key Requirements:

  • The skipTest function must accept a description string (e.g., "My test description") and a test function.
  • The test function should not be executed when skipTest is called.
  • Jest's output should clearly indicate that the test was skipped, similar to how test.skip behaves.
  • The function should not modify the original test function.

Expected Behavior:

When skipTest is called with a description and a test function, the test function should be effectively ignored by Jest. The Jest output should show the test as "skipped" and not include any results (pass, fail, pending) for that test.

Edge Cases to Consider:

  • What happens if the provided test function is empty?
  • What happens if the description string is empty or null? (Should still skip the test)
  • Ensure the implementation doesn't introduce any unexpected side effects.

Examples

Example 1:

Input: skipTest("My Skipped Test", () => { console.log("This should not run"); });
Output: Jest output should show "My Skipped Test" as skipped. No console output from the test function.
Explanation: The test function is never executed, and Jest reports it as skipped.

Example 2:

Input: skipTest("", () => { console.log("Another test"); });
Output: Jest output should show an empty description as skipped. No console output from the test function.
Explanation: An empty description should still result in the test being skipped.

Example 3: (Edge Case)

Input: skipTest("Empty Test", () => {});
Output: Jest output should show "Empty Test" as skipped. No console output from the test function.
Explanation: An empty test function should still be skipped.

Constraints

  • The implementation must be compatible with a standard Jest environment.
  • The skipTest function should be lightweight and efficient. Avoid unnecessary overhead.
  • The solution should be written in TypeScript.
  • The solution should not rely on external libraries beyond Jest itself.

Notes

Think about how Jest internally handles skipped tests. You don't need to replicate the entire Jest architecture, but understanding the concept of marking a test as skipped is key. Consider how you can prevent the test function from being called while still allowing Jest to report its status. You can use a simple function wrapper to achieve this.

Loading editor...
typescript