CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-each

Parameterised tests for Jest that enable running the same test multiple times with different data sets using arrays or tagged template literals

85

1.10x
Overview
Eval results
Files

task.mdevals/scenario-8/

Event Handler Test Suite

Build a test suite for an event handler system that validates callback functions are invoked correctly with the right arguments and frequencies.

Requirements

You need to test an EventEmitter class that has the following interface:

class EventEmitter {
  on(eventName, callback) { /* ... */ }
  emit(eventName, ...args) { /* ... */ }
  removeListener(eventName, callback) { /* ... */ }
}

Your task is to create a test file that validates the behavior of this event emitter using mock functions to track:

  1. Whether callbacks are invoked when events are emitted
  2. The exact arguments passed to callbacks
  3. The number of times callbacks are called
  4. That removed listeners are not invoked

Test Cases

Your test suite must include the following test cases:

  • When a listener is registered and an event is emitted, the callback is invoked exactly once @test
  • When an event is emitted with arguments ('data', 42, true), the listener receives those exact arguments @test
  • When the same event is emitted three times, the listener is called exactly three times @test
  • After removing a listener, emitting the event does not invoke that callback @test

Implementation

@generates

API

The test file should define a test suite that validates the EventEmitter behavior. The EventEmitter implementation will be provided at runtime.

// Test suite structure (using Jest)
describe('EventEmitter', () => {
  // Test case implementations go here
  // Each test should create mock functions to track callback behavior
  test('invokes callback when event is emitted', () => { /* ... */ });
  test('passes correct arguments to callback', () => { /* ... */ });
  test('tracks multiple invocations', () => { /* ... */ });
  test('does not invoke removed listeners', () => { /* ... */ });
});

Dependencies { .dependencies }

jest { .dependency }

Provides testing framework and mock function capabilities.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-jest-each

tile.json