Back to Repositories

Validating Component Exposure Mechanisms in dcloudio/uni-app

A comprehensive test suite for validating defineExpose functionality and script compilation behavior in uni-app UTS components. These tests ensure proper handling of component exposure mechanisms and script content compilation across different script tag configurations.

Test Coverage Overview

The test suite covers two primary aspects of UTS component compilation:
  • defineExpose functionality validation for component property exposure
  • Script tag ordering and content compilation verification
  • Edge cases for script content termination and multiple script tag handling

Implementation Analysis

The testing approach utilizes Jest’s assertion framework with custom compile utilities for SFC (Single File Component) processing. The implementation focuses on script content transformation and exposure mechanism verification, employing pattern matching for compiled output validation.

Key patterns include compile utility usage, content assertion checks, and regex-based output verification.

Technical Details

Testing infrastructure includes:
  • Custom compileSFCScript utility for component compilation
  • Jest assertion framework for test execution
  • Custom assertCode helper for validation
  • Regular expression matching for output verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases with clear scope definition
  • Comprehensive input/output validation
  • Custom utility usage for repeated operations
  • Clear test case naming and organization

dcloudio/uni-app

packages/uni-app-uts/__tests__/android/sfc/compileScript/defineExpose.spec.ts

            
import { assertCode, compileSFCScript as compile } from '../utils'

test('defineExpose()', () => {
  const { content } = compile(`
<script setup>
defineExpose({ foo: 123 })
</script>
`)
  assertCode(content)
  // should remove defineOptions import and call
  expect(content).not.toMatch('defineExpose')
  // should generate correct setup signature
  // expect(content).toMatch(`setup(__props, { expose: __expose }) {`)
  // should replace callee
  expect(content).toMatch(/\b__expose\(\{ foo: 123 \}\)/)
})

test('<script> after <script setup> the script content not end with `\\n`', () => {
  const { content } = compile(`
  <script setup>
  import { x } from './x'
  </script>
  <script>const n = 1</script>
  `)
  assertCode(content)
})