Back to Repositories

Testing Tracer Event Measurement and Plugin Integration in Parcel

This test suite validates the functionality of the Tracer module in Parcel’s core profiler, focusing on trace event measurement and emission. The tests verify both basic and complex tracing scenarios, along with plugin-specific tracing capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of the Tracer functionality, including both enabled and disabled states.

Key areas tested include:
  • Basic trace event creation and emission
  • Complex trace events with categories and arguments
  • Plugin-specific tracing with origin and category handling
  • Edge cases like double-ending measurements

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern combined with sinon for spy functionality and assertions. The implementation demonstrates clean test isolation through beforeEach/afterEach hooks for setup and teardown.

Notable patterns include:
  • Spy-based verification of trace event emissions
  • Sinon matcher usage for flexible assertions
  • Modular test organization with nested describe blocks

Technical Details

Testing tools and configuration:
  • Jest as the primary test runner
  • Sinon.js for spies and test doubles
  • Node.js assert module for basic assertions
  • Beforeach/afterEach hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through proper test isolation and comprehensive coverage.

Notable practices include:
  • Clean setup/teardown patterns
  • Explicit test case naming
  • Granular test scenarios
  • Proper mock cleanup
  • Structured test organization

parcel-bundler/parcel

packages/core/profiler/test/Tracer.test.js

            
import {tracer, PluginTracer} from '../src/Tracer';
import sinon from 'sinon';
import assert from 'assert';

describe('Tracer', () => {
  let onTrace;
  let traceDisposable;
  beforeEach(() => {
    onTrace = sinon.spy();
    traceDisposable = tracer.onTrace(onTrace);
    tracer.enable();
  });
  afterEach(() => {
    traceDisposable.dispose();
  });

  it('returns no measurement when disabled', () => {
    tracer.disable();
    const measurement = tracer.createMeasurement('test');
    assert(measurement == null);
    assert(onTrace.notCalled);
  });
  it('emits a basic trace event', () => {
    const measurement = tracer.createMeasurement('test');
    measurement.end();
    sinon.assert.calledWith(
      onTrace,
      sinon.match({
        type: 'trace',
        name: 'test',
        args: undefined,
        duration: sinon.match.number,
      }),
    );
  });
  it('emits a complex trace event', () => {
    const measurement = tracer.createMeasurement('test', 'myPlugin', 'aaargh', {
      extra: 'data',
    });
    measurement.end();
    sinon.assert.calledWith(
      onTrace,
      sinon.match({
        type: 'trace',
        name: 'test',
        categories: ['myPlugin'],
        args: {extra: 'data', name: 'aaargh'},
        duration: sinon.match.number,
      }),
    );
  });
  it('calling end twice on measurment should be a no-op', () => {
    const measurement = tracer.createMeasurement('test');
    measurement.end();
    measurement.end();
    sinon.assert.calledOnce(onTrace);
  });

  describe('PluginTracer', () => {
    it('emits events with proper origin/category', () => {
      const pluginTracer = new PluginTracer({
        origin: 'origin',
        category: 'cat',
      });
      const measurement = pluginTracer.createMeasurement('test', 'customCat');
      measurement.end();
      sinon.assert.calledWith(
        onTrace,
        sinon.match({
          type: 'trace',
          name: 'test',
          categories: ['cat:origin:customCat'],
          duration: sinon.match.number,
        }),
      );
    });
  });
});