Back to Repositories

Testing Component Tree Shaking Implementation in dcloudio/uni-app

This test suite evaluates tree shaking functionality in uni-app components for both Android and iOS builds. It verifies the proper component optimization and manifest generation across different build configurations, ensuring efficient bundle size management.

Test Coverage Overview

The test suite provides comprehensive coverage of component tree shaking across different platform builds.

  • Tests both Android and iOS build configurations
  • Validates manifest.json generation and content
  • Verifies proper environment variable handling
  • Ensures correct build output directory structure

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities to execute build scripts and verify outputs. It implements dynamic test generation based on build types and platforms, using fs-extra for file system operations and execa for process execution.

  • Dynamic test generation per platform/build type
  • Async/await pattern for build process handling
  • Environment variable injection for build configuration

Technical Details

  • Jest test framework with extended timeout (50s)
  • fs-extra for enhanced file system operations
  • execa for process execution and npm script running
  • Snapshot testing for manifest validation
  • Dynamic environment variable configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices for build process validation.

  • Clean test environment setup with directory cleaning
  • Modular test configuration using object literals
  • Snapshot testing for consistent manifest validation
  • Proper error handling and process management
  • Clear test case isolation and setup

dcloudio/uni-app

packages/playground/__tests__/app-components-tree-shaking.spec.ts

            
import fs from 'fs-extra'
import path from 'path'
import execa from 'execa'

const projectDir = path.resolve(__dirname, '../app-components-tree-shaking')

describe('app-components-tree-shaking playground', () => {
  jest.setTimeout(50 * 1000)
  const types = {
    'uni-app-x': ['build:app-android', 'build:app-ios'],
  }
  const distDir = path.resolve(projectDir, 'dist')
  if (fs.existsSync(distDir)) {
    fs.emptyDirSync(distDir)
  }
  Object.keys(types).forEach((type) => {
    const scripts = types[type]
    scripts.forEach((script) => {
      const mode = script.split(':')[0]
      const platform = script.split(':')[1]
      test(`${type} ${script}`, async () => {
        const outDir = path.resolve(distDir, mode, type, platform)
        console.log(`${type} npm run ${script} start`)
        await execa('npm', ['run', script], {
          cwd: projectDir,
          env: {
            ...process.env,
            UNI_OUTPUT_DIR: outDir,
            UNI_APP_X: type === 'uni-app-x' ? 'true' : 'false',
          },
        })
        console.log(`${type} npm run ${script} end`)
        const manifest = JSON.parse(
          fs.readFileSync(path.resolve(outDir, 'manifest.json'), 'utf-8')
        )
        // 里边的版本号会变更,移除影响
        delete manifest['uni-app-x']
        expect(JSON.stringify(manifest, null, 2)).toMatchSnapshot()
      })
    })
  })
})