Back to Repositories

Validating Public Bundle Interface Consistency in Parcel Bundler

This test suite validates the public Bundle interfaces in Parcel’s core bundling system, focusing on bundle consistency and caching behavior. It ensures that internal bundle representations are correctly mapped to their public counterparts while maintaining object reference integrity.

Test Coverage Overview

The test suite provides comprehensive coverage of Parcel’s public Bundle API implementations.

Key areas tested include:
  • Bundle object caching and reference equality
  • NamedBundle instantiation consistency
  • PackagedBundle creation and caching behavior
  • Internal to public bundle conversion integrity

Implementation Analysis

The testing approach uses Jest’s describe/it pattern to validate bundle object caching and reference equality. The implementation leverages beforeEach hooks for test setup, creating controlled environments with mock bundle configurations and graph structures.

Technical patterns include:
  • Flow type checking for type safety
  • ContentGraph initialization for bundle relationships
  • Environment configuration mocking
  • Bundle reference comparison testing

Technical Details

Testing tools and configuration:
  • Jest test framework
  • Flow strict-local type checking
  • Assert library for equality validation
  • Mock ContentGraph implementation
  • Custom environment creation utilities
  • Project path conversion helpers

Best Practices Demonstrated

The test suite exemplifies several testing best practices in JavaScript module testing.

Notable practices include:
  • Isolated test setup using beforeEach
  • Consistent state management
  • Clear test case separation
  • Type-safe testing with Flow
  • Proper mock object initialization
  • Focused test assertions

parcel-bundler/parcel

packages/core/core/test/PublicBundle.test.js

            
// @flow strict-local
import type {Bundle as InternalBundle} from '../src/types';

import assert from 'assert';
import {ContentGraph} from '@parcel/graph';

import {Bundle, NamedBundle, PackagedBundle} from '../src/public/Bundle';
import BundleGraph from '../src/BundleGraph';
import {createEnvironment} from '../src/Environment';
import {DEFAULT_OPTIONS} from './test-utils';
import {toProjectPath} from '../src/projectPath';

describe('Public Bundle', () => {
  let internalBundle: InternalBundle;
  let bundleGraph;
  beforeEach(() => {
    let env = createEnvironment({});
    internalBundle = {
      id: '123',
      hashReference: '@@HASH_REFERENCE_123',
      entryAssetIds: [],
      mainEntryId: null,
      type: 'js',
      env,
      name: null,
      displayName: null,
      publicId: null,
      pipeline: null,
      needsStableName: null,
      bundleBehavior: null,
      isSplittable: true,
      target: {
        env,
        distDir: toProjectPath('/', '/'),
        name: '',
        publicUrl: '',
      },
    };

    bundleGraph = new BundleGraph({
      graph: new ContentGraph(),
      assetPublicIds: new Set(),
      publicIdByAssetId: new Map(),
      bundleContentHashes: new Map(),
    });
  });

  it('returns the same public Bundle given an internal bundle', () => {
    assert.equal(
      Bundle.get(internalBundle, bundleGraph, DEFAULT_OPTIONS),
      Bundle.get(internalBundle, bundleGraph, DEFAULT_OPTIONS),
    );
  });

  it('returns the same public NamedBundle given an internal bundle', () => {
    assert.equal(
      NamedBundle.get(internalBundle, bundleGraph, DEFAULT_OPTIONS),
      NamedBundle.get(internalBundle, bundleGraph, DEFAULT_OPTIONS),
    );
  });

  it('returns the same public PackagedBundle given an internal bundle', () => {
    assert.equal(
      PackagedBundle.get(internalBundle, bundleGraph, DEFAULT_OPTIONS),
      PackagedBundle.get(internalBundle, bundleGraph, DEFAULT_OPTIONS),
    );
  });
});