Back to Repositories

Testing UTS Compiler Platform Transformations in dcloudio/uni-app

This test suite validates the UTS (Uni-App TypeScript) compiler’s ability to transform UTS files into native Kotlin and Swift code. It ensures proper file generation and handling for both Android and iOS platforms within the uni-app framework.

Test Coverage Overview

The test suite covers essential compiler transformation functionality for mobile platforms.

Key areas tested include:
  • Kotlin compilation for Android platform
  • Swift compilation for iOS platform
  • File system operations and path handling
  • Output file generation and verification

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities to validate compiler transformations. It implements a systematic pattern of compiling source files and verifying output existence.

Technical implementation features:
  • Async/await pattern for compilation operations
  • File system checks using existsSync
  • Directory path resolution handling
  • Compiler configuration options management

Technical Details

Testing infrastructure includes:
  • Jest test framework
  • Node.js fs module for file operations
  • Path module for cross-platform path handling
  • Custom toKotlin and toSwift compiler functions
  • Structured test directory setup with input/output paths

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through organized and maintainable code structure.

Notable practices include:
  • Isolated test cases for each platform
  • Proper cleanup of test artifacts
  • Consistent configuration object structure
  • Clear test case naming and organization
  • Effective use of Jest’s expect assertions

dcloudio/uni-app

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

            
import { existsSync, rmSync } from 'fs'
import { resolve } from 'path'
import { toKotlin, toSwift } from '../src/index'

const inputDir = resolve(__dirname, 'examples/demo')
const outputDir = resolve(
  __dirname,
  'examples/demo/unpackage/dist/dev/app-plus'
)

const pluginDir = resolve(inputDir, 'uni_modules/test-uts')

const outputPluginDir = resolve(outputDir, 'uni_modules/test-uts')

describe('compiler', () => {
  test('toKotlin', async () => {
    const relativeFile = 'utssdk/app-android/index'
    const kotlinFile = resolve(outputPluginDir, relativeFile + '.kt')
    if (existsSync(kotlinFile)) {
      rmSync(kotlinFile)
    }
    await toKotlin(resolve(pluginDir, relativeFile + '.uts'), {
      inputDir,
      outputDir,
      sourceMap: false,
      isX: false,
      isSingleThread: false,
      isPlugin: true,
      components: {},
      uniModules: [],
    })
    expect(existsSync(kotlinFile)).toBeTruthy()
  })
  test('toSwift', async () => {
    const relativeFile = 'utssdk/app-ios/index'
    const swiftFile = resolve(outputPluginDir, relativeFile + '.swift')
    if (existsSync(swiftFile)) {
      rmSync(swiftFile)
    }
    await toSwift(resolve(pluginDir, relativeFile + '.uts'), {
      inputDir,
      outputDir,
      sourceMap: false,
      isX: false,
      isSingleThread: false,
      isPlugin: true,
      components: {},
      uniModules: [],
    })
    expect(existsSync(swiftFile)).toBeTruthy()
  })
})