Back to Repositories

Testing Cross-Platform Stack Trace Generation in dcloudio/uni-app

This test suite validates error handling and stack trace generation for UTS (Uni-app TypeScript) implementations across Android and iOS platforms. It focuses on proper error message formatting and code frame generation for different types of compilation errors.

Test Coverage Overview

The test suite provides comprehensive coverage for stack trace handling in both Android and iOS environments.

Key areas tested include:
  • Kotlin stack trace generation for Android uni_modules errors
  • Kotlin stack trace generation for Android UTSsdk errors
  • Swift stack trace generation for iOS compilation errors
  • Error message formatting and path resolution

Implementation Analysis

The testing approach utilizes Jest’s snapshot testing to verify the correct formatting of stack traces. It implements separate handlers for Kotlin and Swift stack traces, ensuring platform-specific error message processing.

Key patterns include:
  • Asynchronous stack trace generation
  • Platform-specific error message parsing
  • Dynamic path resolution for project directories

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • Mock file system paths for consistent testing
  • Snapshot testing for output verification
  • Custom stack trace generation utilities
  • Platform-specific error message templates

Best Practices Demonstrated

The test suite exemplifies several testing best practices for cross-platform development.

Notable practices include:
  • Separation of concerns between Android and iOS tests
  • Consistent error message formatting
  • Comprehensive error case coverage
  • Maintainable test structure with clear test cases
  • Effective use of Jest snapshots for output validation

dcloudio/uni-app

packages/uni-stacktracey/__tests__/uts.spec.ts

            
import path from 'path'

const {
  generateCodeFrameWithKotlinStacktrace,
  generateCodeFrameWithSwiftStacktrace,
} = require('../dist/uni-stacktracey.cjs.js')

const utsProjectDir = path.resolve(__dirname, '../test/uts')

describe('code-frame-uts', () => {
  test('android', async () => {
    expect(
      await generateCodeFrameWithKotlinStacktrace(androidUniModulesError, {
        name: 'uni_modules/test-uts1',
        inputDir: '/Users/fxy/DCloud/test-uts',
        outputDir: path.resolve(utsProjectDir, 'unpackage/dist/dev/app-plus'),
      })
    ).toMatchSnapshot()
    expect(
      await generateCodeFrameWithKotlinStacktrace(androidUTSsdkError, {
        name: 'utssdk/test2',
        inputDir: '/Users/fxy/DCloud/test-uts',
        outputDir: path.resolve(utsProjectDir, 'unpackage/dist/dev/app-plus'),
      })
    ).toMatchSnapshot()
  })
  test('ios', async () => {
    expect(
      await generateCodeFrameWithSwiftStacktrace(iosUniModulesError, {
        name: 'uni_modules/test-uts1',
        inputDir: '/Users/fxy/DCloud/test-uts',
        outputDir: path.resolve(utsProjectDir, 'unpackage/dist/dev/app-plus'),
      })
    ).toMatchSnapshot()
  })
})

const androidUniModulesError = `e: uni_modules/test-uts1/utssdk/app-android/index.kt: (8, 12): The integer literal does not conform to the expected type String

FAILURE: Build failed with an exception.`

const androidUTSsdkError = `e: utssdk/test2/app-android/index.kt: (8, 12): The integer literal does not conform to the expected type String

FAILURE: Build failed with an exception.`

const iosUniModulesError = `CompileSwift normal armv7 uni_modules/test-uts1/utssdk/app-ios/src/index.swift (in target 'uni_modules_test_uts1' from project 'UTS')
uni_modules/test-uts1/utssdk/app-ios/src/index.swift:3:12: error: cannot convert return expression of type 'Int' to return type 'String'

CompileSwift normal arm64 uni_modules/test-uts1/utssdk/app-ios/src/index.swift (in target 'uni_modules_test_uts1' from project 'UTS')
uni_modules/test-uts1/utssdk/app-ios/src/index.swift:3:12: error: cannot convert return expression of type 'Int' to return type 'String'`