Back to Repositories

Testing Logger Event Emission System in Parcel Bundler

This test suite validates the Logger module functionality in the Parcel bundler, focusing on log emission handling and level management. The tests verify different logging levels and message types using Jest and sinon for spy functionality.

Test Coverage Overview

The test suite provides comprehensive coverage of the Logger module’s core functionality:

  • Log level emission validation (info, warn, error)
  • Progress message handling
  • Diagnostic message structure verification
  • Event subscription and disposal

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern combined with sinon spies for event tracking. The implementation follows a before/after hook pattern for test isolation and proper cleanup of event listeners.

  • Spy-based event verification
  • Modular test structure
  • Isolated test cases with proper setup/teardown

Technical Details

Key technical components:

  • Jest test framework
  • Sinon for spy functionality
  • Flow strict-local type checking
  • Assert module for validations
  • Beforeach/afterEach hooks for test lifecycle management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation
  • Consistent assertion patterns
  • Resource cleanup after tests
  • Clear test case descriptions
  • Focused, single-responsibility test cases

parcel-bundler/parcel

packages/core/logger/test/Logger.test.js

            
// @flow strict-local

import assert from 'assert';
import sinon from 'sinon';
import Logger from '../src/Logger';

describe('Logger', () => {
  let onLog;
  let logDisposable;
  beforeEach(() => {
    onLog = sinon.spy();
    logDisposable = Logger.onLog(onLog);
  });

  afterEach(() => {
    logDisposable.dispose();
  });

  it('emits log diagnostics with info level', () => {
    let diagnostic = {
      message: 'hello',
      origin: 'logger',
    };

    Logger.log(diagnostic);

    assert(
      onLog.calledWith({
        level: 'info',
        diagnostics: [diagnostic],
        type: 'log',
      }),
    );
  });

  it('emits warn diagnostic with warn level', () => {
    let diagnostic = {
      message: 'zomg',
      origin: 'logger',
    };

    Logger.warn(diagnostic);

    assert(
      onLog.calledWith({level: 'warn', diagnostics: [diagnostic], type: 'log'}),
    );
  });

  it('emits error messages with error level', () => {
    let diagnostic = {
      message: 'oh noes',
      origin: 'logger',
    };

    Logger.error(diagnostic);

    assert(
      onLog.calledWith({
        level: 'error',
        diagnostics: [diagnostic],
        type: 'log',
      }),
    );
  });

  it('emits progress messages with progress level', () => {
    Logger.progress('update');
    assert(
      onLog.calledWith({level: 'progress', message: 'update', type: 'log'}),
    );
  });
});