Back to Repositories

Testing DefaultMap Custom Collection Implementation in Parcel

This test suite validates the DefaultMap utility class implementation, which extends JavaScript’s Map functionality with default value generation. The tests ensure proper initialization, default value handling, and edge cases for map operations.

Test Coverage Overview

The test suite provides comprehensive coverage of DefaultMap functionality:

  • Map construction with initial entries
  • Default value generation for non-existent keys
  • Automatic entry creation with default values
  • Special value handling (undefined/null)
Integration points focus on JavaScript’s native Map interface compatibility.

Implementation Analysis

The testing approach uses Jest’s describe/it pattern for structured test organization. Each test case isolates specific DefaultMap behaviors, using assert statements to verify expectations. The implementation leverages Flow strict-local typing for enhanced type safety.

Key patterns include factory function testing, value equality verification, and array transformation validation.

Technical Details

Testing tools and configuration:

  • Jest test runner
  • Flow type checking
  • Node.js assert module
  • ES6+ features (Array.from, arrow functions)
  • Strict local type checking enabled

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear descriptions
  • Comprehensive edge case coverage
  • Type safety enforcement
  • Consistent assertion patterns
  • Clear test case organization
Code organization follows the AAA (Arrange-Act-Assert) pattern with clean separation of concerns.

parcel-bundler/parcel

packages/core/utils/test/DefaultMap.test.js

            
// @flow strict-local

import assert from 'assert';
import {DefaultMap} from '../src/DefaultMap';

describe('DefaultMap', () => {
  it('constructs with entries just like Map', () => {
    let map = new DefaultMap(
      k => k,
      [
        [1, 3],
        [2, 27],
      ],
    );
    assert.equal(map.get(1), 3);
    assert.deepEqual(Array.from(map.entries()), [
      [1, 3],
      [2, 27],
    ]);
  });

  it("returns a default value based on a key if it doesn't exist", () => {
    let map = new DefaultMap(k => k);
    assert.equal(map.get(3), 3);
  });

  it("sets a default value based on a key if it doesn't exist", () => {
    let map = new DefaultMap(k => k);
    map.get(3);
    assert.deepEqual(Array.from(map.entries()), [[3, 3]]);
  });

  it('respects undefined/null if it already existed in the map', () => {
    let map = new DefaultMap<number, number | void | null>(k => k);
    map.set(3, undefined);
    assert.equal(map.get(3), undefined);

    map.set(4, null);
    assert.equal(map.get(4), null);
  });
});