Back to all reviewers

Behavior-focused test design

nodejs/node
Based on 5 comments
JavaScript

Tests should focus on verifying behavior rather than implementation details to ensure they remain robust during refactoring. Avoid dependencies on timers or internal code structure that can lead to flaky and brittle tests.

Testing JavaScript

Reviewer Prompt

Tests should focus on verifying behavior rather than implementation details to ensure they remain robust during refactoring. Avoid dependencies on timers or internal code structure that can lead to flaky and brittle tests.

Always use appropriate assertion utilities to verify expected behavior:

  • Use deepStrictEqual for comprehensive object comparison rather than checking individual properties
  • Verify callback execution with utilities like common.mustCall()
  • Consider snapshot testing for output validation rather than parsing implementation details
// โŒ Fragile: Tests implementation details
assert(tapReporterSource.includes('return result; // Early return...'));

// โœ… Robust: Tests actual behavior with proper utilities
const result = await reader.read(new Uint8Array(16));
assert.deepStrictEqual(result, { 
  value: expectedBytes,
  done: false
});

// โœ… Verify callbacks execute
reader.read(receive).then(common.mustCall((result) => {
  assert.strictEqual(result.done, false);
}));

Avoid using timers in tests as they create non-deterministic behavior, leading to flakiness and slowness. Instead, use event hooks or synchronous assertions that verify the expected outcome directly.

5
Comments Analyzed
JavaScript
Primary Language
Testing
Category

Source Discussions