Back to Repositories

Testing SFC Template Compilation Features in dcloudio/uni-app

This test suite evaluates template compilation functionality in uni-app UTS for Android, focusing on different template language formats. It verifies the proper handling of both HTML and Pug template syntaxes within Single File Components (SFC).

Test Coverage Overview

The test suite provides comprehensive coverage of template compilation scenarios:

  • HTML template syntax validation
  • Pug template syntax processing
  • SFC script setup integration
  • Inline template compilation verification

Implementation Analysis

The testing approach utilizes Jest’s describe/test structure for organizing template compilation tests. It implements custom utility functions (compileSFCScript, assertCode) to streamline the testing process and validate compiled output.

The tests specifically verify template processing with different lang attributes and proper integration with script setup syntax.

Technical Details

Testing Infrastructure:

  • Jest test runner
  • Custom compilation utilities
  • SFC parsing mechanisms
  • Template language processors for HTML and Pug
  • Assertion utilities for code validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for different template languages
  • Consistent test structure and naming
  • Reusable utility functions
  • Clear separation of test scenarios
  • Explicit test case descriptions

dcloudio/uni-app

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

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

describe('SFC compile template', () => {
  test('template with html', () => {
    const { content } = compile(
      `<script setup>const msg = ''</script><template lang='html'><view class="test"/></template>`,
      { inlineTemplate: true }
    )
    assertCode(content)
  })
  test('template with pug', () => {
    const { content } = compile(
      `<script setup>const msg = ''</script><template lang='pug'>view.test</template>`,
      { inlineTemplate: true }
    )
    assertCode(content)
  })
})