Back to Repositories

Testing Path Normalization and Vue File Detection in uni-app

This test suite validates core utility functions in the uni-app CLI shared package, focusing on identifier normalization and Vue application file detection. The tests ensure robust path handling and consistent identifier generation across the framework.

Test Coverage Overview

The test suite provides comprehensive coverage of two critical utility functions:
  • normalizeIdentifier: Tests path-to-variable conversion with various special characters and edge cases
  • isAppVue: Validates Vue application file detection across different path formats and operating systems
Key edge cases include handling of hyphens, spaces, numeric prefixes, and Windows-style paths.

Implementation Analysis

The testing approach utilizes Jest’s describe/test pattern for organized test grouping. Each function is tested with multiple test cases using expect assertions to verify output accuracy.

The implementation leverages Jest’s toBe matcher for strict equality comparisons, demonstrating both positive and edge case scenarios for path normalization and file detection.

Technical Details

Testing Framework: Jest
File Type: TypeScript
Key Components:
  • describe blocks for test organization
  • Individual test cases with expect assertions
  • Import of utility functions from shared package
  • Cross-platform path testing support

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Descriptive test case naming
  • Comprehensive edge case coverage
  • Consistent test structure and organization
  • Platform-agnostic path handling
  • Clear input/output validation patterns

dcloudio/uni-app

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

            
import { isAppVue, normalizeIdentifier } from '../src/utils'

describe('test: packages/uni-cli-shared/src/utils.ts', () => {
  test('test:normalizeIdentifier', () => {
    // 根据 path 返回合法 js 变量
    expect(normalizeIdentifier('pages/index/index')).toBe('PagesIndexIndex')
    expect(normalizeIdentifier('pages/index0.0/index')).toBe(
      'PagesIndex00Index'
    )
    // note: filter `-`
    expect(normalizeIdentifier('pages/index1-0/index')).toBe(
      'PagesIndex10Index'
    )
    expect(normalizeIdentifier('pages/index2-0///index')).toBe(
      'PagesIndex20Index'
    )
    expect(normalizeIdentifier('pages/index3--0/index')).toBe(
      'PagesIndex30Index'
    )
    expect(normalizeIdentifier('pages/index4---0/index')).toBe(
      'PagesIndex40Index'
    )

    expect(normalizeIdentifier('pages/index5 0/index')).toBe(
      'PagesIndex50Index'
    )
    expect(normalizeIdentifier('2pages/index6/index')).toBe(
      '_2pagesIndex6Index'
    )
  })

  test('test: isAppVue', () => {
    expect(isAppVue('/xx/xx/app.vue')).toBe(true)
    expect(isAppVue('/xx/xx/app.uvue')).toBe(true)
    expect(isAppVue('/xx/xx/App.uvue')).toBe(true)
    expect(isAppVue('/xx/xx/App.vue')).toBe(true)
    expect(isAppVue('/xx/xx/App.vue')).toBe(true)
    expect(isAppVue('./xx/xx/App.vue')).toBe(true)
    expect(isAppVue('./xx/xx/App.vue')).toBe(true)
    const filePath = 'D:\\user\\file\\app.vue'
    expect(isAppVue(filePath)).toBe(true)
  })
})