Back to Repositories

Testing TypeScript Compilation Implementation in dcloudio/uni-app

This test suite validates the TypeScript compilation functionality for uni-app modules across Android and iOS platforms. It focuses on testing the compilation process of UTS (Uni TypeScript) files and ensures proper platform-specific code generation.

Test Coverage Overview

The test suite provides comprehensive coverage of TypeScript compilation for uni-app modules.

Key areas tested include:
  • Platform-specific compilation for Android (Kotlin) and iOS (Swift)
  • Plugin module compilation across different test cases
  • File system output verification
  • Environment configuration handling

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities to validate TypeScript compilation processes. The implementation employs platform-specific compilers (UniXKotlinCompiler and UniXSwiftCompiler) with snapshot testing to verify output consistency.

Key patterns include:
  • Dynamic test generation for multiple plugins and platforms
  • Environment state management with reset functionality
  • Snapshot-based output validation

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • fast-glob for file pattern matching
  • fs-extra for enhanced file system operations
  • Custom environment variables for build paths
  • Platform-specific compiler instantiation
  • Snapshot testing for output verification

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through systematic organization and robust validation approaches.

Notable practices include:
  • Isolated test environments with cleanup
  • Parametrized testing across platforms
  • Comprehensive environment configuration
  • Modular test structure with clear separation of concerns

dcloudio/uni-app

packages/uni-uts-v1/__tests__/tsc.spec.ts

            
import path from 'path'
import fs from 'fs-extra'
import { sync } from 'fast-glob'
import {
  compileUniModuleWithTsc,
  createUniXKotlinCompilerOnce,
  createUniXSwiftCompilerOnce,
} from '../src'

const inputDir = path.resolve(__dirname, 'examples/tsc/src')
const distDir = path.resolve(__dirname, 'examples/tsc/dist')
const plugins = ['test-a', 'test-b']
const platforms = ['app-android', 'app-ios'] as const
describe('uni_modules', () => {
  for (const plugin of plugins) {
    const pluginDir = path.resolve(inputDir, 'uni_modules', plugin)
    for (const platform of platforms) {
      jest.setTimeout(100000)
      test(`tsc ${plugin} ${platform}`, async () => {
        const reset = initEnv(platform)
        await compileUniModuleWithTsc(
          platform,
          pluginDir,
          platform === 'app-android'
            ? createUniXKotlinCompilerOnce()
            : createUniXSwiftCompilerOnce(),
          {
            rootFiles: [],
            preprocessor: async (content) => {
              return content
            },
          }
        )
        const outputUVuePluginDir = path.resolve(
          process.env.UNI_OUTPUT_DIR!,
          '../.uvue',
          platform,
          'uni_modules',
          plugin
        )
        sync('**/*.{uts,vue}', { cwd: outputUVuePluginDir }).forEach((file) => {
          expect(
            fs.readFileSync(path.resolve(outputUVuePluginDir, file), 'utf8')
          ).toMatchSnapshot(file)
        })
        reset()
      })
    }
  }
})

function initEnv(platform: 'app-android' | 'app-ios') {
  const oldEnv = JSON.parse(JSON.stringify(process.env))

  process.env.NODE_ENV = 'production'
  process.env.UNI_INPUT_DIR = inputDir
  process.env.UNI_OUTPUT_DIR = path.resolve(distDir, 'build', platform)
  process.env.UNI_UTS_PLATFORM = platform
  process.env.UNI_APP_X_CACHE_DIR = path.resolve(
    process.env.UNI_OUTPUT_DIR,
    '../cache/.' + platform
  )
  process.env.UNI_APP_X_TSC_DIR = path.resolve(
    process.env.UNI_OUTPUT_DIR,
    '../.tsc'
  )
  process.env.UNI_APP_X_UVUE_DIR = path.resolve(
    process.env.UNI_OUTPUT_DIR,
    '../.uvue'
  )
  process.env.UNI_APP_X_TSC_CACHE_DIR = path.resolve(
    process.env.UNI_APP_X_CACHE_DIR,
    `tsc`
  )

  return () => {
    process.env = oldEnv
  }
}