|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('assert') |
| 4 | +const path = require('path') |
| 5 | +const { spawn } = require('child_process') |
| 6 | + |
| 7 | +const { BUN } = require('../helpers/bun') |
| 8 | +const { FakeAgent, useSandbox, sandboxCwd } = require('../helpers') |
| 9 | + |
| 10 | +/** |
| 11 | + * Spawn a file under the Bun runtime inside the test sandbox. |
| 12 | + * |
| 13 | + * @param {string} filename - Path relative to the sandbox root |
| 14 | + * @param {Record<string, string>} [env] |
| 15 | + * @returns {Promise<{ code: number | null, stdout: string, stderr: string }>} |
| 16 | + */ |
| 17 | +function runBun (filename, env = {}) { |
| 18 | + const cwd = sandboxCwd() |
| 19 | + |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + const proc = spawn(BUN, [path.join(cwd, filename)], { |
| 22 | + cwd, |
| 23 | + stdio: 'pipe', |
| 24 | + env: { |
| 25 | + ...process.env, |
| 26 | + DD_INSTRUMENTATION_TELEMETRY_ENABLED: 'false', |
| 27 | + ...env, |
| 28 | + }, |
| 29 | + }) |
| 30 | + |
| 31 | + let stdout = '' |
| 32 | + let stderr = '' |
| 33 | + |
| 34 | + proc.stdout.on('data', (data) => { stdout += data }) |
| 35 | + proc.stderr.on('data', (data) => { stderr += data }) |
| 36 | + proc.once('error', reject) |
| 37 | + proc.once('exit', (code) => resolve({ code, stdout, stderr })) |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @param {{ code: number | null, stdout: string, stderr: string }} result |
| 43 | + */ |
| 44 | +function assertBunSuccess (result) { |
| 45 | + const { code, stdout, stderr } = result |
| 46 | + |
| 47 | + assert.strictEqual(code, 0, `Process exited with code ${code}.\nstdout: ${stdout}\nstderr: ${stderr}`) |
| 48 | + assert.ok(stdout.includes('ok'), `Expected "ok" in stdout, got: ${stdout}`) |
| 49 | +} |
| 50 | + |
| 51 | +describe('Bun runtime smoke tests', function () { |
| 52 | + this.timeout(30_000) |
| 53 | + |
| 54 | + useSandbox() |
| 55 | + let agent |
| 56 | + |
| 57 | + before(async function () { |
| 58 | + agent = await new FakeAgent().start() |
| 59 | + }) |
| 60 | + |
| 61 | + after(async () => { |
| 62 | + await agent?.stop() |
| 63 | + }) |
| 64 | + |
| 65 | + describe('init order compatibility', () => { |
| 66 | + it('should init tracer via CJS require', async function () { |
| 67 | + assertBunSuccess(await runBun('bun/init-cjs.js')) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should init tracer after ESM JSON import (issue #7480)', async function () { |
| 71 | + assertBunSuccess(await runBun('bun/init-esm-json-import.mjs')) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should init tracer when ESM imports dd-trace before package.json', async function () { |
| 75 | + assertBunSuccess(await runBun('bun/init-esm-dd-trace-first.mjs')) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should init tracer when CJS requires package.json before init', async function () { |
| 79 | + assertBunSuccess(await runBun('bun/init-cjs-json-before-init.js')) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should init tracer when CJS requires package.json after init', async function () { |
| 83 | + assertBunSuccess(await runBun('bun/init-cjs-json-after-init.js')) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should send trace payload with Bun runtime header', async () => { |
| 88 | + const messagePromise = agent.assertMessageReceived(({ headers, payload }) => { |
| 89 | + assert.strictEqual(headers['datadog-meta-lang-interpreter'], 'JavaScriptCore') |
| 90 | + assert.ok(Array.isArray(payload), 'Expected trace payload array') |
| 91 | + assert.ok(payload.length > 0, 'Expected at least one trace') |
| 92 | + }, 20_000) |
| 93 | + |
| 94 | + const [bunResult] = await Promise.all([ |
| 95 | + runBun('bun/export-span.js', { |
| 96 | + DD_TRACE_AGENT_URL: `http://127.0.0.1:${agent.port}`, |
| 97 | + }), |
| 98 | + messagePromise, |
| 99 | + ]) |
| 100 | + |
| 101 | + assertBunSuccess(bunResult) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should not crash when agent is unavailable', async () => { |
| 105 | + assertBunSuccess(await runBun('bun/init-agent-unavailable.js')) |
| 106 | + }) |
| 107 | + |
| 108 | + it('should emit app-started telemetry under Bun', async () => { |
| 109 | + const telemetryPromise = agent.assertTelemetryReceived('app-started', 20_000, 1) |
| 110 | + |
| 111 | + const [bunResult] = await Promise.all([ |
| 112 | + runBun('bun/init-telemetry.js', { |
| 113 | + DD_TRACE_AGENT_URL: `http://127.0.0.1:${agent.port}`, |
| 114 | + DD_INSTRUMENTATION_TELEMETRY_ENABLED: 'true', |
| 115 | + }), |
| 116 | + telemetryPromise, |
| 117 | + ]) |
| 118 | + |
| 119 | + assertBunSuccess(bunResult) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should auto-instrument basic HTTP traffic', async () => { |
| 123 | + const messagePromise = agent.assertMessageReceived(({ payload }) => { |
| 124 | + assert.ok(Array.isArray(payload), 'Expected trace payload array') |
| 125 | + const spans = payload.flat() |
| 126 | + assert.ok( |
| 127 | + spans.some(span => span.name === 'web.request' || span.name === 'http.request'), |
| 128 | + 'Expected web.request or http.request span' |
| 129 | + ) |
| 130 | + }, 20_000) |
| 131 | + |
| 132 | + const [bunResult] = await Promise.all([ |
| 133 | + runBun('bun/http-instrumentation.js', { |
| 134 | + DD_TRACE_AGENT_URL: `http://127.0.0.1:${agent.port}`, |
| 135 | + }), |
| 136 | + messagePromise, |
| 137 | + ]) |
| 138 | + |
| 139 | + assertBunSuccess(bunResult) |
| 140 | + }) |
| 141 | +}) |
0 commit comments