|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('node:path') |
| 4 | +const assert = require('node:assert') |
| 5 | +const { execSync } = require('node:child_process') |
| 6 | + |
| 7 | +const axios = require('axios') |
| 8 | + |
| 9 | +const { FakeAgent, spawnProc, createSandbox } = require('../helpers') |
| 10 | + |
| 11 | +const esbuildVersions = ['latest', '0.16.12'] |
| 12 | + |
| 13 | +function findWebSpan (payload) { |
| 14 | + for (const trace of payload) { |
| 15 | + for (const span of trace) { |
| 16 | + if (span.type === 'web') { |
| 17 | + return span |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + throw new Error('web span not found') |
| 22 | +} |
| 23 | + |
| 24 | +esbuildVersions.forEach((version) => { |
| 25 | + describe('ESM is built and runs as expected in a sandbox', () => { |
| 26 | + let sandbox, agent, cwd |
| 27 | + |
| 28 | + before(async () => { |
| 29 | + sandbox = await createSandbox([`esbuild@${version}`, 'hono', '@hono/node-server'], false, [__dirname]) |
| 30 | + cwd = sandbox.folder |
| 31 | + }) |
| 32 | + |
| 33 | + beforeEach(async () => { |
| 34 | + agent = await new FakeAgent().start() |
| 35 | + }) |
| 36 | + |
| 37 | + after(() => { |
| 38 | + sandbox.remove() |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + agent.stop() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should build basic esm http server exporting esm and create web traces at runtime', async () => { |
| 46 | + const builder = path.join(cwd, 'esbuild', 'build.esm-http-output-esm.mjs') |
| 47 | + execSync(`node ${builder}`, { cwd }) |
| 48 | + |
| 49 | + const appFile = path.join(cwd, 'esbuild', 'esm-http-test-out.mjs') |
| 50 | + const proc = await spawnProc(appFile, { |
| 51 | + cwd, |
| 52 | + env: { |
| 53 | + DD_TRACE_AGENT_URL: `http://localhost:${agent.port}` |
| 54 | + }, |
| 55 | + stdio: 'pipe', |
| 56 | + }) |
| 57 | + |
| 58 | + await Promise.all([ |
| 59 | + agent.assertMessageReceived(({ payload }) => { |
| 60 | + // http web spans are created in bundled application |
| 61 | + const webSpan = findWebSpan(payload) |
| 62 | + assert.strictEqual(webSpan.name, 'web.request') |
| 63 | + }, 2_500), |
| 64 | + axios.get(proc.url) |
| 65 | + ]) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should build basic esm http server exporting cjs and create web traces at runtime', async () => { |
| 69 | + const builder = path.join(cwd, 'esbuild', 'build.esm-http-output-cjs.mjs') |
| 70 | + execSync(`node ${builder}`, { cwd }) |
| 71 | + |
| 72 | + const appFile = path.join(cwd, 'esbuild', 'esm-http-test-out.cjs') |
| 73 | + const proc = await spawnProc(appFile, { |
| 74 | + cwd, |
| 75 | + env: { |
| 76 | + DD_TRACE_AGENT_URL: `http://localhost:${agent.port}` |
| 77 | + }, |
| 78 | + stdio: 'pipe', |
| 79 | + }) |
| 80 | + |
| 81 | + await Promise.all([ |
| 82 | + agent.assertMessageReceived(({ payload }) => { |
| 83 | + // http web spans are created in bundled application |
| 84 | + const webSpan = findWebSpan(payload) |
| 85 | + assert.strictEqual(webSpan.name, 'web.request') |
| 86 | + }, 2_500), |
| 87 | + axios.get(proc.url) |
| 88 | + ]) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should build basic hono server exporting esm and create web traces at runtime', async () => { |
| 92 | + const builder = path.join(cwd, 'esbuild', 'build.esm-hono-output-esm.mjs') |
| 93 | + execSync(`node ${builder}`, { cwd }) |
| 94 | + |
| 95 | + const appFile = path.join(cwd, 'esbuild', 'hono-out.mjs') |
| 96 | + const proc = await spawnProc(appFile, { |
| 97 | + cwd, |
| 98 | + env: { |
| 99 | + DD_TRACE_AGENT_URL: `http://localhost:${agent.port}` |
| 100 | + }, |
| 101 | + stdio: 'pipe', |
| 102 | + }) |
| 103 | + |
| 104 | + await Promise.all([ |
| 105 | + agent.assertMessageReceived(({ payload }) => { |
| 106 | + // http web spans are created in bundled application |
| 107 | + const webSpan = findWebSpan(payload) |
| 108 | + assert.strictEqual(webSpan.name, 'hono.request') |
| 109 | + }, 2_500), |
| 110 | + axios.get(proc.url) |
| 111 | + ]) |
| 112 | + }) |
| 113 | + |
| 114 | + it('should build basic hono server exporting cjs and create web traces at runtime', async () => { |
| 115 | + const builder = path.join(cwd, 'esbuild', 'build.esm-hono-output-cjs.mjs') |
| 116 | + execSync(`node ${builder}`, { cwd }) |
| 117 | + |
| 118 | + const appFile = path.join(cwd, 'esbuild', 'hono-out.cjs') |
| 119 | + const proc = await spawnProc(appFile, { |
| 120 | + cwd, |
| 121 | + env: { |
| 122 | + DD_TRACE_AGENT_URL: `http://localhost:${agent.port}` |
| 123 | + }, |
| 124 | + stdio: 'pipe', |
| 125 | + }) |
| 126 | + |
| 127 | + await Promise.all([ |
| 128 | + agent.assertMessageReceived(({ payload }) => { |
| 129 | + // http web spans are created in bundled application |
| 130 | + const webSpan = findWebSpan(payload) |
| 131 | + assert.strictEqual(webSpan.name, 'hono.request') |
| 132 | + }, 2_500), |
| 133 | + axios.get(proc.url) |
| 134 | + ]) |
| 135 | + }) |
| 136 | + }) |
| 137 | +}) |
0 commit comments