Back to Repositories

Validating Pages.json Configuration and Subpackages in uni-app

This test suite validates the pages.json configuration in uni-app, focusing on page routing and subpackage structure verification. The tests ensure proper JSON formatting and error handling for both main pages and subpackages configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of pages.json validation functionality.

Key areas tested include:
  • Main pages configuration validation
  • SubPackages structure verification
  • Alternative subpackages casing handling
  • Error generation and formatting for invalid configurations
Integration points cover JSON parsing, error handling, and code frame generation for error visualization.

Implementation Analysis

The testing approach utilizes Jest’s describe/test pattern for organized test cases. The implementation employs snapshot testing for error validation and includes specific checks for JSON structure compliance.

Technical patterns include:
  • JSON stringification with formatting
  • Try-catch error handling
  • Path resolution for test examples
  • Code frame generation for error visualization

Technical Details

Testing tools and configuration:
  • Jest as the testing framework
  • TypeScript for type-safe testing
  • Custom checkPagesJson utility
  • generateCodeFrame for error visualization
  • Path resolution for test file management
  • Snapshot testing for error validation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through structured test organization and comprehensive error checking.

Notable practices include:
  • Consistent test case organization
  • Proper error handling and validation
  • Snapshot testing for regression prevention
  • Clear test case separation
  • Meaningful test descriptions

dcloudio/uni-app

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

            
import path from 'path'
import { checkPagesJson } from '../src/json/uni-x'
import { generateCodeFrame } from '../src/vite/plugins/vitejs/utils'

describe('pages.json', () => {
  const inputDir = path.resolve(__dirname, './examples/check-pages-json')
  test(`pages check`, () => {
    const source = JSON.stringify(
      {
        pages: [
          {
            path: 'pages/index/index',
          },
          {
            path: 'pages/index/test',
          },
        ],
      },
      null,
      2
    )
    try {
      checkPagesJson(source, inputDir)
    } catch (error: any) {
      expect(error).toMatchSnapshot()
      expect(
        generateCodeFrame(
          source,
          error.offsetStart as number,
          error.offsetEnd as number
        ).replace(/\t/g, ' ')
      ).toMatchSnapshot()
    }
  })
  test(`subPackages check`, () => {
    const source = JSON.stringify(
      {
        subPackages: [
          {
            root: 'pages/API',
            pages: [
              {
                path: 'index/index',
              },
            ],
          },
        ],
      },
      null,
      2
    )
    try {
      checkPagesJson(source, inputDir)
    } catch (error: any) {
      expect(error).toMatchSnapshot()
      expect(
        generateCodeFrame(
          source,
          error.offsetStart as number,
          error.offsetEnd as number
        ).replace(/\t/g, ' ')
      ).toMatchSnapshot()
    }
  })
  test(`subpackages check`, () => {
    const source = JSON.stringify({
      subpackages: [
        {
          root: 'pages/API',
          pages: [
            {
              path: 'index/index',
            },
          ],
        },
      ],
    })
    try {
      checkPagesJson(source, inputDir)
    } catch (error: any) {
      expect(error).toMatchSnapshot()
      expect(
        generateCodeFrame(
          source,
          error.offsetStart as number,
          error.offsetEnd as number
        ).replace(/\t/g, ' ')
      ).toMatchSnapshot()
    }
  })
})