Skip to content

Replace console spying in tests with in-memory exporters #478

Description

@sjvans

Tests should read telemetry from in-memory exporters (like MyInMemorySpanExporter, introduced in #465) rather than spying on console.dir / cds.test.log() and parsing formatted output. Console spying is brittle (couples tests to exporter formatting), order-sensitive, and — as the jest→vitest migration (#474) showed — sensitive to log-timing windows.

The console-based tests fall into three groups; handle each accordingly.

1. Trace-span tests still spying on console.dir → use MyInMemorySpanExporter

These use the sdk-trace-node console exporter (tracing-console / formerly tracing-attributes profile) and read spans out of the console.dir spy. They should switch to the tracing-in-memory profile and read captured from MyInMemorySpanExporter (as tracing.test.js, tracing-mt.test.js, tracing-scheduled.test.js etc. already do post-#465):

  • test/tracing-remote-cloudsdk.test.js (getSpans/getCapSpans off console.dircaptured)
  • test/tracing-remote-native.test.js (same)
  • test/tracing-span-names.test.js (same)

Once these move off it, the tracing-console profile (@opentelemetry/sdk-trace-node console exporter) in test/bookshop/.cdsrc.json may become unused — remove it if so. (Note: there's a separate deferred task to rename that profile; this supersedes it — it just goes away.)

2. Metric tests spying on console.dir → need an in-memory metric reader

#465 only added an in-memory span exporter; there is no metric equivalent yet. These read metric datapoints (descriptor.name, dataPoints) out of the ConsoleMetricExporter console.dir output:

  • test/metrics-outbox.test.js
  • test/metrics-outbox-multitenant.test.js
  • test/metrics-outbox-disabled.test.js

Add a MyInMemoryMetricReader (companion to MyInMemorySpanExporter, wired via a .cdsrc.json profile) that captures the exported ResourceMetrics in a module-level array, and convert these three tests to assert against it. forceFlush() on the reader replaces the current sleep+scrape pattern.

3. Genuine console-output tests → LEAVE AS-IS

These legitimately assert on what is printed to the console, which is the behavior under test — not a backdoor to spans/metrics. Do NOT convert:

  • test/metrics.test.js — asserts the ConsoleMetricExporter's host-metrics text output (process/network/nodejs.eventloop.* lines). This is testing the console formatter itself.
  • test/logging.test.js — asserts the LogRecord objects the logging path emits (body, exception.* attributes). (If a MyInMemoryLogRecordExporter is ever added, revisit; not required here.)

Definition of done

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions