Back to Repositories

Testing Console Log Normalization in uni-app

This test suite validates the console logging functionality in uni-app, specifically focusing on the log normalization process. It ensures proper formatting and handling of different data types in console output messages, which is crucial for debugging and development workflows.

Test Coverage Overview

The test coverage focuses on the normalizeLog utility function’s capability to handle mixed data types in console logging.

Key areas tested include:
  • String concatenation and formatting
  • JSON object serialization
  • File reference handling
  • Message delimiter implementation

Implementation Analysis

The testing approach utilizes Jest’s expect assertions to verify the correct formatting of console log messages. The implementation demonstrates a pattern of transforming various input types into a standardized string format, using custom delimiters (—COMMA—, —BEGIN:JSON—, —END:JSON—) to maintain data integrity.

The test leverages Jest’s describe and test blocks for clear organization and isolated testing of the log normalization functionality.

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • TypeScript for type-safe testing
  • normalizeLog utility from the uni-app service API
  • Custom string delimiters for data separation
  • Filename reference handling for stack trace simulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in its implementation.

Notable practices include:
  • Isolated function testing
  • Clear input/output expectations
  • Complex data type handling
  • Meaningful test descriptions
  • Single responsibility principle in test cases

dcloudio/uni-app

packages/uni-app-plus/__tests__/hbx/console.spec.ts

            
import { normalizeLog } from '../../src/service/api/plugin/log'
const filename = 'foo.vue'
describe('console', () => {
  test('console.log format', () => {
    expect(
      normalizeLog('log', 'at ' + filename + ':1', ['a', 'b', { a: 1 }])
    ).toBe(
      `a---COMMA---b---COMMA------BEGIN:JSON---{"a":1}---END:JSON--- at foo.vue:1`
    )
  })
})