Hone logo
Hone
Problems

Mastering Jest's .only Modifier for Targeted Testing

Jest provides powerful tools to help developers focus their testing efforts. One such tool is the .only modifier, which allows you to run only a specific test or describe block. This is incredibly useful during development for quickly verifying changes in a particular area without running your entire test suite. This challenge will test your understanding and practical application of Jest's .only modifier.

Problem Description

Your task is to create a simple Jest test file in TypeScript that demonstrates the use of the .only modifier. You should have multiple test cases within a describe block, and then apply .only to a specific test case to show that only that test runs.

What needs to be achieved:

  1. Create a Jest test file using TypeScript.
  2. Define a describe block containing at least three individual test (or it) cases.
  3. Use the .only modifier on one of these test cases.
  4. Configure your Jest environment to run these tests and observe the output.

Key requirements:

  • The solution must be written in TypeScript.
  • The test file should contain a describe block.
  • Inside the describe block, there should be at least three distinct test (or it) functions.
  • One of the test functions must be modified with .only.
  • The output when running Jest should clearly indicate that only the .only test has executed.

Expected behavior:

When Jest is executed against the created test file, only the test case marked with .only should pass (or fail, depending on its implementation), while the other tests within the same describe block should be skipped.

Any important edge cases to consider:

  • What happens if .only is applied to a describe block instead of a test? (While not explicitly required for this challenge, it's good to be aware of).
  • Ensuring the test setup correctly identifies and executes only the intended test.

Examples

Example 1:

  • Conceptual Input: A Jest test file (my-tests.test.ts) containing the following structure:

    describe('My Feature Tests', () => {
      test('should perform action A', () => {
        expect(true).toBe(true); // Placeholder
      });
    
      test.only('should perform action B', () => {
        expect(1 + 1).toBe(2); // This test should run
      });
    
      test('should perform action C', () => {
        expect(false).toBe(false); // This test should be skipped
      });
    });
    
  • Conceptual Output (when running jest command):

    PASS  ./my-tests.test.ts
      My Feature Tests
        ✓ should perform action B (XXms)
    
    ------------------------------------------------------------------
    Ran 1 of 3 tests in XXXms (XX skipped)
    
  • Explanation: The output shows that only one test ("should perform action B") was executed, and the other two were skipped.

Constraints

  • You must use TypeScript for your test file.
  • Your solution should be a single .ts file.
  • You are expected to have Jest installed and configured in your project (or use a simple npx jest --init if starting from scratch).

Notes

  • The goal is to demonstrate your ability to create the test file that uses .only, not necessarily to write complex application logic within the tests. Simple expect statements asserting basic truths are sufficient.
  • Pay close attention to the output of Jest when you run it. This is the primary indicator of success.
  • Consider how you might remove .only once your development is complete.
Loading editor...
typescript