Back to Repositories

Testing EasyCom Component Auto-Import System in uni-app

This test suite validates the EasyCom component functionality in uni-app, focusing on component auto-importing and tag parsing. It ensures proper handling of component paths, tag matching, and declaration generation for seamless component integration.

Test Coverage Overview

The test suite provides comprehensive coverage of EasyCom’s core functionality:
  • Component glob pattern parsing and path resolution
  • EasyCom tag parsing and component matching
  • Component search and declaration file generation
  • Edge cases in component path resolution and tag matching

Implementation Analysis

The testing approach utilizes Jest’s describe/test pattern for structured unit testing. It implements pattern matching validation and filesystem interactions through mock paths and component structures.
  • Uses Jest’s expect assertions for pattern matching
  • Implements path resolution testing
  • Validates component map generation

Technical Details

Testing infrastructure includes:
  • Jest test framework
  • Node.js path resolution utilities
  • Mock filesystem structure for component testing
  • EasyComContext class implementation
  • TypeScript type definitions generation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases for distinct functionality
  • Clear test case organization and naming
  • Comprehensive assertion coverage
  • Proper setup of test context and environment
  • Validation of both success paths and edge cases

dcloudio/uni-app

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

            
import { resolve } from 'node:path'
import { EasyComContext } from '../src'
import { parseEasyComTag, parseGlob } from '../src/easycom/options'
const inputDir = resolve(__dirname, 'examples/easycom')
const outputDir = resolve(__dirname, 'examples/easycom/unpackage/dist/dev/app')
describe('easycom', () => {
  test('parseGlob', () => {
    expect(
      parseGlob('@dcloudio/uni-ui/lib/uni-*/uni-*.vue', inputDir)
    ).toContain('/easycom/node_modules/@dcloudio/uni-ui/lib/uni-*/uni-*.vue')
  })
  test('parseEasyComTag', () => {
    const easyComTag = parseEasyComTag(
      '^uni-(.*)',
      '@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue',
      inputDir
    )
    expect(easyComTag).toMatchObject({
      tag: '^uni-(.*)',
      path: '@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue',
    })
    expect(easyComTag.glob).toContain(
      `/easycom/node_modules/@dcloudio/uni-ui/lib/uni-*/uni-*.vue`
    )
    expect(
      easyComTag.parseTag(
        `/easycom/node_modules/@dcloudio/uni-ui/lib/uni-badge/uni-badge.vue`
      )
    ).toBe('uni-badge')
  })
  test('searchComponents', () => {
    const ctx = new EasyComContext({
      inputDir,
      outputDir,
      dts: resolve(inputDir, 'easycom.d.ts'),
    })
    ctx.searchComponents()
    expect(ctx.componentMap['test']).not.toBeUndefined()
    expect(ctx.componentMap['uni-badge']).not.toBeUndefined()
    ctx.generateDeclaration()
  })
})