Back to Repositories

Testing MiniProgram JSON File Operations in uni-app Framework

This test suite validates the JSON file handling functionality for mini-program components in the uni-app framework. It focuses on testing component registration, path resolution, and configuration management for miniProgram JSON files.

Test Coverage Overview

The test suite provides comprehensive coverage of miniProgram JSON file operations.

Key areas tested include:
  • Component registration through usingComponents
  • Path resolution for component references
  • JSON file discovery and parsing
  • Error handling for missing components
Integration points cover plugin components and local component references.

Implementation Analysis

The testing approach employs Jest’s describe/test blocks to organize related test cases hierarchically. The implementation uses environment variable manipulation and path resolution to simulate the mini-program runtime environment.

Key patterns include:
  • Mock environment setup and cleanup
  • Path transformation validation
  • JSON structure comparison
  • Component resolution verification

Technical Details

Testing tools and setup:
  • Jest as the testing framework
  • Path module for filesystem operations
  • Environment variable management
  • JSON parsing and comparison utilities
  • Mock file system for component discovery

Best Practices Demonstrated

The test suite demonstrates strong testing practices through isolated test cases and proper setup/teardown patterns.

Notable practices include:
  • Proper test isolation using beforeAll/afterAll hooks
  • Comprehensive edge case coverage
  • Clear test case organization
  • Effective use of Jest’s expect assertions
  • Clean environment restoration after tests

dcloudio/uni-app

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

            
import path from 'path'
import {
  addMiniProgramPageJson,
  findChangedJsonFiles,
  findUsingComponentsJson,
} from '../src/json/mp/jsonFile'
import xrStart from './examples/usingComponents/wxcomponents/xr-start/xr-start.json'
import xrStartIndex from './examples/usingComponents/wxcomponents/xr-start-index/index.json'

describe('miniProgram:jsonFile', () => {
  const filename = 'pages/index/index'
  test(`usingComponents`, () => {
    const usingComponents = {
      subscribe: 'plugin://subscribeMsg/subscribe',
      demo: '/components/demo/demo',
    }
    addMiniProgramPageJson(filename, {
      usingComponents,
    })
    expect(JSON.parse(findChangedJsonFiles().get(filename)!)).toEqual({
      usingComponents: {
        subscribe: 'plugin://subscribeMsg/subscribe',
        demo: '../../components/demo/demo',
      },
    })
  })

  describe('miniProgram:jsonFile:findUsingComponentsJson', () => {
    let oldInput = process.env.UNI_INPUT_DIR
    let input = path.resolve(__dirname, './examples/usingComponents')
    beforeAll(() => {
      process.env.UNI_INPUT_DIR = input
    })
    afterAll(() => {
      process.env.UNI_INPUT_DIR = oldInput
    })
    test(`miniProgram:jsonFile:findMiniProgramUsingComponents`, () => {
      let json = findUsingComponentsJson(
        '../wxcomponents/xr-start/xr-start',
        'wxcomponents'
      )
      expect(json).toEqual(xrStart)
      json = findUsingComponentsJson(
        '/wxcomponents/xr-start-index',
        'wxcomponents'
      )
      expect(json).toEqual(xrStartIndex)
      json = findUsingComponentsJson(
        '../wxcomponents/xr-start-index',
        'wxcomponents'
      )
      expect(json).toEqual(xrStartIndex)
      json = findUsingComponentsJson(
        '../wxcomponents/xr-start-error',
        'wxcomponents'
      )
      expect(json).toEqual({})
      json = findUsingComponentsJson(
        '../error-components/xr-start-index',
        'wxcomponents'
      )
      expect(json).toEqual({})
    })
  })
})