Back to Repositories

Testing WXS Filter Name Parsing in uni-app

This test suite validates the filter name parsing functionality in uni-app, specifically focusing on WXS script module handling. The tests ensure proper extraction of module names from various script tag configurations and contexts.

Test Coverage Overview

The test suite provides comprehensive coverage of filter name parsing functionality.

Key areas tested include:
  • Basic WXS script tag parsing
  • Self-closing script tag handling
  • Multiple filter extraction
  • Complex template scenarios with mixed script types
Edge cases covered include different script tag formats and multiple module declarations within the same template.

Implementation Analysis

The testing approach utilizes Jest’s describe/test pattern for organizing related test cases. Each test validates the parseFilterNames function with different input scenarios, using precise expect assertions to verify output.

The implementation leverages Jest’s toMatchObject and toBe matchers for array and string comparisons, ensuring accurate module name extraction.

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • TypeScript for type-safe test implementation
  • parseFilterNames utility function from filter module
  • String-based template parsing verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices for component parsing validation.

Notable practices include:
  • Isolated test cases for specific scenarios
  • Clear test descriptions and naming
  • Progressive complexity in test cases
  • Comprehensive coverage of edge cases
  • Consistent assertion patterns

dcloudio/uni-app

packages/uni-cli-shared/__tests__/filter.spec.ts

            
import { parseFilterNames } from '../src/filter'

describe('filter', () => {
  test(`basic`, () => {
    expect(
      parseFilterNames(
        'wxs',
        `<script src="./wx.wxs" module="swipe" lang="wxs"></script>`
      )[0]
    ).toBe('swipe')
  })
  test(`self close`, () => {
    expect(
      parseFilterNames(
        'wxs',
        `<script src="./wx.wxs" module="swipe" lang="wxs"/>`
      )[0]
    ).toBe('swipe')
  })
  test(`multi filter`, () => {
    expect(
      parseFilterNames(
        'wxs',
        `<script module="swipe1" lang="wxs">
module.exports = { message:'hello' }
</script>
<script src="./wx.wxs" module="swipe2" lang="wxs"/>
`
      )
    ).toMatchObject(['swipe1', 'swipe2'])
  })
  test(`complex`, () => {
    expect(
      parseFilterNames(
        'wxs',
        `<template><view/></template>
<script module="swipe1" lang="wxs">
module.exports = { message:'hello' }
</script>
<script src="./wx.wxs" module="swipe2" lang="wxs"/>
<script src="./renderjs.js" module="swipe3" lang="renderjs"/>
<script setup>
const title = '123'
</script>
`
      )
    ).toMatchObject(['swipe1', 'swipe2'])
  })
})