Skip to content

Commit

Permalink
test(instrumentation-pino): add test for ESM (#2176)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDanielson committed May 7, 2024
1 parent ba0de30 commit 66e6ce4
Showing 1 changed file with 74 additions and 0 deletions.
Expand Up @@ -33,6 +33,10 @@ import {
testInjection,
testNoInjection,
} from './common';
import {
runTestFixture,
TestCollector,
} from '@opentelemetry/contrib-test-utils';

import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
Expand Down Expand Up @@ -235,4 +239,74 @@ describe('PinoInstrumentation', () => {
});
});
});
describe('ESM usage', () => {
beforeEach(() => {
testContext = setupInstrumentationAndInitTestContext();
});
it('should work with ESM default import', async function () {
testContext = setupInstrumentationAndInitTestContext();
let logRecords: any[];
await runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pino-default-import.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, _stderr) => {
assert.ifError(err);
logRecords = stdout
.trim()
.split('\n')
.map(ln => JSON.parse(ln));
assert.strictEqual(logRecords.length, 1);
},
checkCollector: (collector: TestCollector) => {
// Check that both log records had the trace-context of the span injected.
const spans = collector.sortedSpans;
assert.strictEqual(spans.length, 1);
logRecords.forEach(rec => {
assert.strictEqual(rec.trace_id, spans[0].traceId);
assert.strictEqual(rec.span_id, spans[0].spanId);
});
},
});
});

it('should work with ESM named import', async function () {
if (semver.lt(testContext.pino.version, '6.8.0')) {
// Pino 6.8.0 added named ESM exports (https://github.com/pinojs/pino/pull/936).
this.skip();
} else {
let logRecords: any[];
await runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pino-named-import.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, _stderr) => {
assert.ifError(err);
logRecords = stdout
.trim()
.split('\n')
.map(ln => JSON.parse(ln));
assert.strictEqual(logRecords.length, 1);
},
checkCollector: (collector: TestCollector) => {
// Check that both log records had the trace-context of the span injected.
const spans = collector.sortedSpans;
assert.strictEqual(spans.length, 1);
logRecords.forEach(rec => {
assert.strictEqual(rec.trace_id, spans[0].traceId);
assert.strictEqual(rec.span_id, spans[0].spanId);
});
},
});
}
});
});
});

0 comments on commit 66e6ce4

Please sign in to comment.