Back to Repositories

Testing Configuration Loading Mechanisms in Parcel Bundler

This test suite validates the configuration loading functionality in Parcel bundler, focusing on various file formats and error handling scenarios. The tests ensure robust config parsing across JSON, TOML, JS, and CJS file types while maintaining proper error management.

Test Coverage Overview

The test suite provides comprehensive coverage of configuration loading scenarios across multiple file formats.

  • Tests JSON config loading with valid and empty files
  • Validates TOML empty configuration handling
  • Verifies JS and CJS module config loading
  • Tests extensionless config files (.testrc)
  • Ensures proper error handling for malformed configs

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern with async/await for handling asynchronous config loading operations.

Key implementation patterns include:
  • Consistent use of assert.deepEqual for config validation
  • Path resolution using path.join for cross-platform compatibility
  • Mock filesystem integration via @parcel/test-utils
  • Promise-based error assertion using assert.rejects

Technical Details

Testing infrastructure includes:
  • Flow type checking with strict-local mode
  • Jest test runner configuration
  • Custom filesystem abstraction (@parcel/test-utils)
  • Node.js assert module for validations
  • Path manipulation utilities

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through structured organization and thorough validation.

  • Isolated test cases with clear purpose
  • Consistent assertion patterns
  • Error case coverage
  • Cross-platform path handling
  • Type-safe testing with Flow

parcel-bundler/parcel

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

            
// @flow strict-local

import assert from 'assert';
import {loadConfig} from '../src/config';
import {inputFS as fs} from '@parcel/test-utils';
import path from 'path';

describe('loadConfig', () => {
  it('load config with json', async () => {
    assert.deepEqual(
      (
        await loadConfig(
          fs,
          path.join(__dirname, './input/config/config.json'),
          ['config.json'],
          path.join(__dirname, './input/config/'),
        )
      )?.config,
      {
        hoge: 'fuga',
      },
    );
  });

  it('should throw error with empty string json', async () => {
    // $FlowFixMe[prop-missing]
    await assert.rejects(async () => {
      await loadConfig(
        fs,
        path.join(__dirname, './input/config/empty.json'),
        ['empty.json'],
        path.join(__dirname, './input/config/'),
      );
    });
  });

  it('should load with empty string config toml', async () => {
    assert.deepEqual(
      (
        await loadConfig(
          fs,
          path.join(__dirname, './input/config/empty.toml'),
          ['empty.toml'],
          path.join(__dirname, './input/config/'),
        )
      )?.config,
      {},
    );
  });

  it('should load with js', async () => {
    assert.deepEqual(
      (
        await loadConfig(
          fs,
          path.join(__dirname, './input/config/config.js'),
          ['config.js'],
          path.join(__dirname, './input/config/'),
        )
      )?.config,
      {
        hoge: 'fuga',
      },
    );
  });

  it('should load with cjs', async () => {
    assert.deepEqual(
      (
        await loadConfig(
          fs,
          path.join(__dirname, './input/config/config.cjs'),
          ['config.cjs'],
          path.join(__dirname, './input/config/'),
        )
      )?.config,
      {
        hoge: 'fuga',
      },
    );
  });

  it('should load without an extension as json', async () => {
    assert.deepEqual(
      (
        await loadConfig(
          fs,
          path.join(__dirname, './input/config/.testrc'),
          ['.testrc'],
          path.join(__dirname, './input/config/'),
        )
      )?.config,
      {
        hoge: 'fuga',
      },
    );
  });
});