Hone logo
Hone
Problems

Jest Assertion Counter

This challenge focuses on understanding and leveraging Jest's built-in capabilities to track the number of assertions executed within a test suite. Accurately counting assertions is crucial for ensuring thorough test coverage and for identifying tests that might be unintentionally skipping assertions.

Problem Description

Your task is to write a Jest test file that accurately counts the total number of assertions executed across all tests within that file. Jest provides mechanisms to track this, and your goal is to demonstrate proficiency in using these.

Requirements:

  • Create at least two distinct test cases.
  • Each test case should contain at least one assertion.
  • You need to implement a way to programmatically count the total number of assertions executed across all test cases within the file.
  • The final assertion in your test file should verify that the calculated total assertion count matches the expected count based on the assertions you've written.

Expected Behavior:

When the test file is run using Jest, it should pass. The final assertion should confirm that the tracked assertion count is correct.

Edge Cases to Consider:

  • Tests with no assertions (while not explicitly forbidden by the problem, your solution should still correctly report the count, which would not increase from such a test).
  • Tests that throw errors before assertions are made.

Examples

Example 1:

Let's say your test file contains two it blocks, each with one expect call.

// test_file.ts
import { expect } from '@jest/globals';

describe('Assertion Counting Example', () => {
  it('should have one assertion', () => {
    expect(1 + 1).toBe(2);
  });

  it('should have another assertion', () => {
    expect('hello').toContain('ell');
  });

  // Here you would implement the logic to count and assert the total
});

Expected Outcome (conceptual, not literal code):

The test run should report passing all tests. The final assertion in test_file.ts should verify that the total assertion count is 2.

Example 2:

Consider a scenario with multiple assertions within a single test.

// test_file.ts
import { expect } from '@jest/globals';

describe('Multiple Assertions', () => {
  it('should have multiple assertions', () => {
    const data = { name: 'Alice', age: 30 };
    expect(data.name).toBe('Alice');
    expect(data.age).toBeGreaterThan(25);
    expect(data).toHaveProperty('name');
  });

  it('should have one more assertion', () => {
    expect(true).toBe(true);
  });

  // Here you would implement the logic to count and assert the total
});

Expected Outcome (conceptual):

The test run should report passing all tests. The final assertion in test_file.ts should verify that the total assertion count is 4 (3 from the first it block + 1 from the second).

Constraints

  • Your solution must be implemented in TypeScript.
  • You must use Jest as the testing framework.
  • The solution should be self-contained within a single Jest test file.
  • Avoid hardcoding assertion counts for individual tests if possible; aim for a dynamic count.

Notes

Jest's global expect function and its associated matchers are the primary tools for making assertions. Consider how Jest reports test results and what information it makes available during test execution. There might be global objects or hooks that can help you achieve this. Think about when assertions are actually executed and how you can tap into that lifecycle.

Loading editor...
typescript