Back to Repositories

Testing Public ID Generation and Collision Handling in Parcel Bundler

This test suite validates the getPublicId utility function in Parcel’s core package, focusing on input validation, collision handling, and ID generation. The tests ensure proper hexadecimal string handling and base62 character conversion for public identifier generation.

Test Coverage Overview

The test suite provides comprehensive coverage of the getPublicId utility function, examining both valid and invalid input scenarios. Key functionality tested includes:

  • Input validation for 32-character hexadecimal strings
  • Base62 character conversion and length handling
  • Collision detection and resolution
  • Edge case handling for maximum collision scenarios

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern with Node’s assert module for validation. The implementation demonstrates modular test organization with focused test cases that verify specific behaviors. The tests leverage closure functions to simulate collision scenarios and validate the progressive length increase behavior.

  • Modular test structure using describe blocks
  • Assertion-based validation
  • Collision simulation using callback functions

Technical Details

Testing infrastructure includes:

  • Jest test runner
  • Node.js assert module
  • Flow type checking with strict-local mode
  • Modular test file organization
  • Custom collision callback implementations

Best Practices Demonstrated

The test suite exemplifies several testing best practices in JavaScript unit testing. Each test case is focused and isolated, with clear assertions and error conditions tested. The code demonstrates:

  • Clear test case isolation
  • Comprehensive error handling validation
  • Progressive complexity in test scenarios
  • Effective use of test doubles for collision simulation

parcel-bundler/parcel

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

            
// @flow strict-local

import assert from 'assert';
import {getPublicId} from '../src/utils';

const id = '0123456789abcdef0123456789abcdef';
const fullPublicId = '296TIIbB3u904riPYGPJJ';

describe('getPublicId', () => {
  it('only accepts 32-character hexadecimal strings', () => {
    assert.throws(() => {
      getPublicId('abc', () => false);
    });

    let notHexadecimal = 'abcdefghiklmnopqrstuvwxyz1234567';
    assert.equal(notHexadecimal.length, 32);
    assert.throws(() => {
      getPublicId(notHexadecimal, () => false);
    });
  });

  it('if no collisions, returns the first 5 base62 characters of value represented by the input', () => {
    assert.equal(
      getPublicId(id, () => false),
      fullPublicId.slice(0, 5),
    );
  });

  it('uses more characters if there is a collision', () => {
    assert.equal(
      getPublicId(id, publicId =>
        [fullPublicId.slice(0, 5), fullPublicId.slice(0, 6)].includes(publicId),
      ),
      fullPublicId.slice(0, 7),
    );
  });

  it('fails if all characters collide', () => {
    assert.throws(() => {
      getPublicId(id, () => true);
    });
  });
});