Back to Repositories

Testing URL Path Joining Utilities in Parcel Bundler

This test suite validates the URL joining functionality in Parcel’s core utilities, ensuring proper path concatenation and normalization across different formats and operating systems. The tests verify the urlJoin utility’s ability to handle various path combinations while maintaining correct URL structure.

Test Coverage Overview

The test suite provides comprehensive coverage of URL path joining scenarios.

Key areas tested include:
  • Basic path joining with root directory
  • Handling of query parameters
  • Static path prefixing
  • Windows path normalization
  • Special character handling (colons in paths)

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for organized test cases. Each test case focuses on a specific URL joining scenario, employing Node’s assert module for equality verification. The implementation demonstrates clear separation of concerns with isolated test cases for different path combinations and formats.

Technical Details

Testing infrastructure includes:
  • Flow strict-local type checking
  • Node.js assert module for assertions
  • Jest test runner configuration
  • Custom urlJoin utility from core package

Best Practices Demonstrated

The test suite exemplifies several testing best practices including descriptive test case naming, isolated test scenarios, and comprehensive edge case coverage. The code organization follows a clear pattern with consistent formatting and appropriate use of assertion methods for validation.

parcel-bundler/parcel

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

            
// @flow strict-local

import assert from 'assert';
import urlJoin from '../src/urlJoin';

describe('urlJoin', () => {
  it('Should join two paths', () => {
    let joinedUrl = urlJoin('/', './image.jpeg?test=test');
    assert.equal(joinedUrl, '/image.jpeg?test=test');
  });

  it('Should join two paths with longer publicUrl', () => {
    let joinedUrl = urlJoin('/static', './image.jpeg?test=test');
    assert.equal(joinedUrl, '/static/image.jpeg?test=test');
  });

  it('Should join two paths with longer publicUrl', () => {
    let joinedUrl = urlJoin('/static', 'image.jpeg?test=test');
    assert.equal(joinedUrl, '/static/image.jpeg?test=test');
  });

  it('Should turn windows path into posix', () => {
    let joinedUrl = urlJoin('/static', '.\\image.jpeg?test=test');
    assert.equal(joinedUrl, '/static/image.jpeg?test=test');
  });

  it('should support paths with colons', () => {
    let joinedUrl = urlJoin('/static', 'a:b:c.html');
    assert.equal(joinedUrl, '/static/a:b:c.html');

    joinedUrl = urlJoin('/static', '/a:b:c.html');
    assert.equal(joinedUrl, '/static/a:b:c.html');

    joinedUrl = urlJoin('/static', './a:b:c.html');
    assert.equal(joinedUrl, '/static/a:b:c.html');
  });
});