Back to Repositories

Validating CommonJS Module Compilation in uni-app Framework

This test suite validates CommonJS module functionality in the uni-app framework across multiple build platforms. It ensures proper module compilation and integration for app, H5, and WeChat Mini Program builds.

Test Coverage Overview

The test suite provides comprehensive coverage of CommonJS module compilation across different build targets.

Key areas tested include:
  • App service build verification
  • H5 build process
  • WeChat Mini Program compilation
  • Module constant declaration persistence
Integration points focus on build output verification and environment-specific configurations.

Implementation Analysis

The testing approach uses Jest’s asynchronous testing capabilities to execute build scripts and verify output files. The implementation leverages execa for build process execution and fs-extra for file system operations.

Key patterns include dynamic test generation based on build types, environment variable injection, and output file content validation.

Technical Details

Testing tools and configuration:
  • Jest as the test runner with 50-second timeout
  • execa for npm script execution
  • fs-extra for file system management
  • Dynamic output directory configuration
  • Platform-specific file path validation

Best Practices Demonstrated

The test suite exemplifies robust testing practices through systematic build verification across platforms. Notable practices include:
  • Dynamic test case generation
  • Environment isolation for builds
  • Explicit output validation
  • Clean build directory management
  • Platform-specific expectation handling

dcloudio/uni-app

packages/playground/__tests__/commonjs.spec.ts

            
import fs from 'fs-extra'
import path from 'path'
import execa from 'execa'
// import { sync } from 'fast-glob'
// import { normalizePath } from '@dcloudio/uni-cli-shared'

const projectDir = path.resolve(__dirname, '../commonjs')

describe('commonjs playground', () => {
  jest.setTimeout(50 * 1000)
  const types = {
    'uni-app': ['build:app', 'build:h5', 'build:mp-weixin'],
  }
  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`)
        let expectFile = ''
        if (platform === 'app') {
          expectFile = 'app-service.js'
        } else if (platform === 'mp-weixin') {
          expectFile = 'common/vendor.js'
        }
        if (expectFile) {
          expect(
            fs
              .readFileSync(path.resolve(outDir, expectFile), 'utf-8')
              .includes('const name = "test";')
          ).toBe(true)
        }
      })
    })
  })
})