Back to Repositories

Testing InternalAsset Dependency Management in Parcel Bundler

This test suite validates the functionality of InternalAsset class in Parcel bundler, focusing on file dependency management and asset invalidation. It ensures proper handling of connected files and dependency tracking in the bundling process.

Test Coverage Overview

The test suite provides comprehensive coverage of InternalAsset’s core functionality, particularly focusing on file path management and dependency handling.

  • Tests file path deduplication in connected files
  • Verifies dependency uniqueness based on specifier IDs
  • Validates different environment configurations for dependencies
  • Covers edge cases in dependency management

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern with modular test cases that isolate specific behaviors. The implementation leverages Flow type checking and custom asset creation utilities.

  • Uses UncommittedAsset class for test scenarios
  • Implements asset creation helper functions
  • Employs assertion-based verification
  • Utilizes environment simulation

Technical Details

  • Testing Framework: Jest
  • Type System: Flow strict-local
  • Custom Utilities: createAsset, createEnvironment
  • Test Configuration: DEFAULT_OPTIONS
  • Path Management: toProjectPath utility

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and comprehensive assertion coverage.

  • Clear test case isolation and naming
  • Consistent setup patterns
  • Thorough edge case coverage
  • Proper use of assertion methods
  • Modular test organization

parcel-bundler/parcel

packages/core/core/test/InternalAsset.test.js

            
// @flow strict-local

import assert from 'assert';
import UncommittedAsset from '../src/UncommittedAsset';
import {createAsset as _createAsset} from '../src/assetUtils';
import {createEnvironment} from '../src/Environment';
import {DEFAULT_OPTIONS} from './test-utils';
import {toProjectPath} from '../src/projectPath';

function createAsset(opts) {
  return _createAsset('/', opts);
}

const stats = {time: 0, size: 0};

describe('InternalAsset', () => {
  it('only includes connected files once per filePath', () => {
    let asset = new UncommittedAsset({
      value: createAsset({
        filePath: toProjectPath('/', '/foo/asset.js'),
        env: createEnvironment(),
        stats,
        type: 'js',
        isSource: true,
      }),
      options: DEFAULT_OPTIONS,
    });
    asset.invalidateOnFileChange(toProjectPath('/', '/foo/file'));
    asset.invalidateOnFileChange(toProjectPath('/', '/foo/file'));
    assert.deepEqual(
      asset.invalidations.invalidateOnFileChange,
      new Set(['foo/file']),
    );
  });

  it('only includes dependencies once per id', () => {
    let asset = new UncommittedAsset({
      value: createAsset({
        filePath: toProjectPath('/', '/foo/asset.js'),
        env: createEnvironment(),
        stats,
        type: 'js',
        isSource: true,
      }),
      options: DEFAULT_OPTIONS,
    });

    asset.addDependency({specifier: './foo', specifierType: 'esm'});
    asset.addDependency({specifier: './foo', specifierType: 'esm'});
    let dependencies = asset.getDependencies();
    assert(dependencies.length === 1);
    assert(dependencies[0].specifier === './foo');
  });

  it('includes different dependencies if their id differs', () => {
    let asset = new UncommittedAsset({
      value: createAsset({
        filePath: toProjectPath('/', '/foo/asset.js'),
        env: createEnvironment(),
        stats,
        type: 'js',
        isSource: true,
      }),
      options: DEFAULT_OPTIONS,
    });

    asset.addDependency({specifier: './foo', specifierType: 'esm'});
    asset.addDependency({
      specifier: './foo',
      specifierType: 'esm',
      env: {context: 'web-worker', engines: {}},
    });
    let dependencies = asset.getDependencies();
    assert(dependencies.length === 2);
  });
});