Back to Repositories

Testing Console Method Transformation in uni-app

This test suite validates the console expression rewriting functionality in uni-app’s logging system. It ensures proper transformation of various console methods while maintaining code integrity and logging functionality across different usage patterns.

Test Coverage Overview

The test suite provides comprehensive coverage of console method rewriting functionality.

Key areas tested include:
  • Basic console.log transformation
  • Multi-line console statements
  • Different console methods (log, info, debug, warn, error)
  • Variable argument handling
  • JSON stringify integration

Implementation Analysis

The testing approach utilizes Jest’s snapshot testing pattern to verify console expression transformations. Each test case focuses on specific console method implementations, utilizing the rewriteConsoleExpr function with consistent parameters (METHOD, filename) across different console method variations.

The implementation leverages TypeScript’s type system and Jest’s expect assertions to validate the transformed code output.

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • TypeScript for type-safe testing
  • Snapshot testing for output validation
  • rewriteConsoleExpr utility function
  • Standard test file organization with describe/test blocks

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for each console method
  • Consistent test structure and naming
  • Snapshot testing for complex output validation
  • Clear test case organization
  • Comprehensive coverage of edge cases

dcloudio/uni-app

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

            
import { rewriteConsoleExpr } from '../src/logs/console'
const filename = 'foo.vue'
const METHOD = '__f__'
describe('console', () => {
  test('console.log', () => {
    expect(
      rewriteConsoleExpr(
        METHOD,
        filename,
        filename,
        `const a = 1;console.log(a, JSON.stringify(1));`
      ).code
    ).toMatchSnapshot()
  })
  test('console.log multiline', () => {
    expect(
      rewriteConsoleExpr(
        METHOD,
        filename,
        filename,
        `const a = 1;

console.log(a);
const b = 2
console.log(a,b);
console.log(a,b,c);
`
      ).code
    ).toMatchSnapshot()
  })
  test('console.info', () => {
    expect(
      rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
        .code
    ).toMatchSnapshot()
  })
  test('console.debug', () => {
    expect(
      rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
        .code
    ).toMatchSnapshot()
  })
  test('console.warn', () => {
    expect(
      rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
        .code
    ).toMatchSnapshot()
  })
  test('console.error', () => {
    expect(
      rewriteConsoleExpr(METHOD, filename, filename, `console.info(a,b,c);`)
        .code
    ).toMatchSnapshot()
  })
})