Back to Repositories

Testing Path-to-Glob Pattern Conversion in uni-app

This test suite validates the pathToGlob utility function in the uni-app framework, which handles path-to-glob pattern conversion across different operating systems. The tests ensure reliable path manipulation for both Unix and Windows environments while maintaining proper glob pattern generation.

Test Coverage Overview

The test suite provides comprehensive coverage of path-to-glob pattern conversion scenarios.

Key areas tested include:
  • Unix path handling with basic and special characters
  • Windows path normalization and backslash handling
  • Special character escaping in both environments
  • Path concatenation with glob patterns

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for organized test cases. Tests are structured to validate path handling across different operating systems with various configuration options.

Implementation features:
  • Parameterized testing with options object
  • Platform-specific path handling verification
  • Special character escape sequence testing

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • TypeScript for type-safe testing
  • Expect assertions for validation
  • Configuration options for windows and escape flags

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for different environments
  • Clear test case descriptions
  • Comprehensive edge case coverage
  • Platform-specific configuration handling
  • Consistent assertion patterns

dcloudio/uni-app

packages/uni-cli-shared/__tests__/pathToGlob.spec.ts

            
import { pathToGlob } from '../src/utils'

describe('pathToGlob', () => {
  it('path in unix', () => {
    expect(pathToGlob('/test', '**/*.js')).toBe('/test/**/*.js')
    expect(pathToGlob('/test(1', '**/*.js')).toBe('/test[(]1/**/*.js')
    expect(
      pathToGlob('/test(1', '**/*.js', { escape: true, windows: false })
    ).toBe('/test\\(1/**/*.js')
  })
  it('path in windows', () => {
    expect(pathToGlob('C:\\\\test\\test', '**/*.js', { windows: true })).toBe(
      'C:/test/test/**/*.js'
    )
    expect(pathToGlob('C:\\\\test\\test(1', '**/*.js', { windows: true })).toBe(
      'C:/test/test[(]1/**/*.js'
    )
    expect(
      pathToGlob('C:\\\\test\\test(1', '**/*.js', {
        windows: true,
        escape: true,
      })
    ).toBe('C:/test/test[(]1/**/*.js')
  })
})