Back to Repositories

Validating Uni-Stat Android Build Integration in dcloudio/uni-app

This test suite validates the uni-stat functionality in the uni-app framework, focusing on build processes and statistics integration for Android platform. It ensures proper configuration and implementation of statistical tracking features across different build modes.

Test Coverage Overview

The test suite provides comprehensive coverage of uni-stat implementation in Android builds.

Key areas tested include:
  • Build process validation for uni-app-x Android platform
  • Statistical tracking configuration verification
  • Output directory structure integrity
  • Environment variable handling for different build types

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities to validate build outputs and file contents. The implementation leverages fs-extra for file system operations and execa for command execution, ensuring reliable build process testing.

Notable patterns include:
  • Dynamic test generation based on build types
  • Environment variable injection for build configuration
  • File content validation using string matching

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • fs-extra for enhanced file system operations
  • execa for process execution
  • Custom timeout configuration (50s)
  • Dynamic path resolution for project directories
  • Environment variable manipulation for build control

Best Practices Demonstrated

The test suite exemplifies several testing best practices in build process validation.

Notable practices include:
  • Proper test isolation with directory cleaning
  • Dynamic test case generation
  • Explicit timeout management
  • Clear test case naming conventions
  • Comprehensive build output validation

dcloudio/uni-app

packages/playground/__tests__/uni-stat.spec.ts

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

const projectDir = path.resolve(__dirname, '../uni-stat')

describe('uni-stat playground', () => {
  jest.setTimeout(50 * 1000)
  const types = {
    'uni-app-x': ['build:app-android'],
  }
  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`)

        expect(
          fs
            .readFileSync(
              path.resolve(outDir, '.uniappx/android/src/index.kt'),
              'utf-8'
            )
            .includes('override var uniStatistics = object : UTSJSONObject()')
        ).toBe(true)
      })
    })
  })
})