Back to Repositories

Testing Code Frame Generation with Source Maps in uni-app

This test suite validates code frame generation functionality in uni-app’s stacktracey module, focusing on source map consumption and warning message handling. It ensures accurate code frame generation for various warning scenarios in Kotlin files while maintaining source map integration.

Test Coverage Overview

The test suite provides comprehensive coverage of code frame generation functionality.

Key areas tested include:
  • Source map consumer initialization and handling
  • Warning message processing across multiple scenarios
  • Code frame generation with source map integration
  • External path resolution and source root handling
Edge cases covered include deprecated Java methods, redundant initializers, and non-null assertions.

Implementation Analysis

The testing approach utilizes Jest’s describe/test structure with async/await patterns for source map operations.

Technical implementation features:
  • Asynchronous source map consumer initialization
  • Snapshot testing for output validation
  • Dynamic warning message processing
  • Source root path configuration handling

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • SourceMapConsumer for source map processing
  • File system (fs) operations for map file reading
  • Path resolution utilities
  • TypeScript type definitions for source map consumers

Best Practices Demonstrated

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

Notable practices include:
  • Proper test isolation and setup
  • Efficient resource handling with consumer initialization
  • Comprehensive snapshot testing
  • Strong typing with TypeScript interfaces
  • Clear test case organization and naming

dcloudio/uni-app

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

            
import fs from 'fs'
import path from 'path'

const {
  SourceMapConsumer,
  generateCodeFrameSourceMapConsumer,
  generateCodeFrameWithSourceMapPath,
} = require('../dist/uni-stacktracey.cjs.js')
import type {
  BasicSourceMapConsumer,
  IndexedSourceMapConsumer,
} from '../lib/source-map'

const warnings = [
  {
    type: 'warning',
    message: "'DATA: String' is deprecated. Deprecated in Java",
    file: 'index.kt',
    line: 27,
    column: 71,
  },
  {
    type: 'warning',
    message:
      'Unnecessary non-null assertion (!!) on a non-null receiver of type onImageCatchOptions',
    file: 'index.kt',
    line: 41,
    column: 25,
  },
  {
    type: 'warning',
    message: "Condition 'cursor == null' is always 'false'",
    file: 'index.kt',
    line: 61,
    column: 13,
  },
  {
    type: 'warning',
    message: "'DATA: String' is deprecated. Deprecated in Java",
    file: 'index.kt',
    line: 63,
    column: 78,
  },
  {
    type: 'warning',
    message: "Variable 'width' initializer is redundant",
    file: 'index.kt',
    line: 69,
    column: 21,
  },
  {
    type: 'warning',
    message: "Variable 'height' initializer is redundant",
    file: 'index.kt',
    line: 70,
    column: 22,
  },
  {
    type: 'warning',
    message:
      'Unnecessary non-null assertion (!!) on a non-null receiver of type onImageCatchOptions',
    file: 'index.kt',
    line: 97,
    column: 47,
  },
  {
    type: 'warning',
    message:
      "'getter for defaultDisplay: Display!' is deprecated. Deprecated in Java",
    file: 'index.kt',
    line: 122,
    column: 44,
  },
  {
    type: 'warning',
    message: "'getRealSize(Point!): Unit' is deprecated. Deprecated in Java",
    file: 'index.kt',
    line: 123,
    column: 24,
  },
  {
    type: 'warning',
    message:
      "'getExternalStorageDirectory(): File!' is deprecated. Deprecated in Java",
    file: 'index.kt',
    line: 152,
    column: 51,
  },
  {
    type: 'warning',
    message:
      "'getExternalStorageDirectory(): File!' is deprecated. Deprecated in Java",
    file: 'index.kt',
    line: 153,
    column: 47,
  },
]

const filename = path.resolve(__dirname, 'index.kt.map')

describe('code-frame', () => {
  let consumer: BasicSourceMapConsumer | IndexedSourceMapConsumer
  async function initConsumer() {
    if (!consumer) {
      consumer = await new SourceMapConsumer(fs.readFileSync(filename, 'utf8'))
    }
    return consumer
  }

  test('generateCodeFrame', async () => {
    const consumer = await initConsumer()
    warnings.forEach((w) => {
      expect(
        generateCodeFrameSourceMapConsumer(consumer, w, {
          sourceRoot: '/Users/fxy/Projects/Gitcode/hello-uts',
        })
      ).toMatchSnapshot()
    })
  })

  test('generateCodeFrameWithSourceMapPath', async () => {
    ;(
      await generateCodeFrameWithSourceMapPath(
        filename,
        JSON.stringify(warnings),
        { sourceRoot: '/Users/fxy/Projects/Gitcode/hello-uts' }
      )
    ).forEach((m: unknown) => {
      expect(m).toMatchSnapshot()
    })
  })
})