Parameterised tests for Jest that enable running the same test multiple times with different data sets using arrays or tagged template literals
85
Build a test suite for an event handler system that validates callback functions are invoked correctly with the right arguments and frequencies.
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:
Your test suite must include the following test cases:
('data', 42, true), the listener receives those exact arguments @test@generates
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', () => { /* ... */ });
});Provides testing framework and mock function capabilities.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-jest-eachdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10