Skip to content

Commit 3aca43b

Browse files
authored
test: remove npm/sandbox operations from fastify esm test (#6622)
1 parent ab33cb0 commit 3aca43b

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

integration-tests/helpers/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,26 @@ function checkSpansForServiceName (spans, name) {
522522
return spans.some((span) => span.some((nestedSpan) => nestedSpan.name === name))
523523
}
524524

525+
/**
526+
* @overload
527+
* @param {string} cwd
528+
* @param {string} serverFile
529+
* @param {string|number} agentPort
530+
* @param {Record<string, string|undefined>} [additionalEnvArgs]
531+
*/
525532
/**
526533
* @param {string} cwd
527534
* @param {string} serverFile
528535
* @param {string|number} agentPort
529536
* @param {function} [stdioHandler]
530537
* @param {Record<string, string|undefined>} [additionalEnvArgs]
531538
*/
532-
async function spawnPluginIntegrationTestProc (cwd, serverFile, agentPort, stdioHandler, additionalEnvArgs = {}) {
539+
async function spawnPluginIntegrationTestProc (cwd, serverFile, agentPort, stdioHandler, additionalEnvArgs) {
540+
if (typeof stdioHandler !== 'function' && !additionalEnvArgs) {
541+
additionalEnvArgs = stdioHandler
542+
stdioHandler = undefined
543+
}
544+
additionalEnvArgs = additionalEnvArgs || {}
533545
let env = /** @type {Record<string, string|undefined>} */ ({
534546
NODE_OPTIONS: `--loader=${hookFile}`,
535547
DD_TRACE_AGENT_PORT: String(agentPort)

packages/datadog-plugin-fastify/test/integration-test/client.spec.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,24 @@
22

33
const {
44
FakeAgent,
5-
createSandbox,
65
curlAndAssertMessage,
76
checkSpansForServiceName,
87
spawnPluginIntegrationTestProc
98
} = require('../../../../integration-tests/helpers')
10-
const { withVersions } = require('../../../dd-trace/test/setup/mocha')
9+
const { withVersions, insertVersionDep } = require('../../../dd-trace/test/setup/mocha')
1110
const { assert } = require('chai')
11+
const { join } = require('path')
1212

1313
describe('esm', () => {
1414
let agent
1515
let proc
16-
let sandbox
16+
const env = {
17+
NODE_OPTIONS: `--loader=${join(__dirname, '..', '..', '..', '..', 'initialize.mjs')}`
18+
}
1719

1820
// skip older versions of fastify due to syntax differences
1921
withVersions('fastify', 'fastify', '>=3', (version, _, specificVersion) => {
20-
before(async function () {
21-
this.timeout(60000)
22-
sandbox = await createSandbox([`'fastify@${version}'`], false,
23-
['./packages/datadog-plugin-fastify/test/integration-test/*'])
24-
})
25-
26-
after(async () => {
27-
await sandbox.remove()
28-
})
22+
insertVersionDep(__dirname, 'fastify', version)
2923

3024
beforeEach(async () => {
3125
agent = await new FakeAgent().start()
@@ -37,7 +31,7 @@ describe('esm', () => {
3731
})
3832

3933
it('is instrumented', async () => {
40-
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port)
34+
proc = await spawnPluginIntegrationTestProc(__dirname, 'server.mjs', agent.port, env)
4135

4236
return curlAndAssertMessage(agent, proc, ({ headers, payload }) => {
4337
assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`)
@@ -47,7 +41,7 @@ describe('esm', () => {
4741
}).timeout(20000)
4842

4943
it('* import fastify is instrumented', async () => {
50-
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server1.mjs', agent.port)
44+
proc = await spawnPluginIntegrationTestProc(__dirname, 'server1.mjs', agent.port, env)
5145

5246
return curlAndAssertMessage(agent, proc, ({ headers, payload }) => {
5347
assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`)
@@ -57,7 +51,7 @@ describe('esm', () => {
5751
}).timeout(20000)
5852

5953
it('Fastify import fastify is instrumented', async () => {
60-
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server2.mjs', agent.port)
54+
proc = await spawnPluginIntegrationTestProc(__dirname, 'server2.mjs', agent.port, env)
6155

6256
return curlAndAssertMessage(agent, proc, ({ headers, payload }) => {
6357
assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`)

packages/datadog-plugin-fastify/test/integration-test/server.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dd-trace/init.js'
21
import fastify from 'fastify'
32
import { createAndStartServer } from './helper.mjs'
43

packages/datadog-plugin-fastify/test/integration-test/server1.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dd-trace/init.js'
21
import * as Fastify from 'fastify'
32
import { createAndStartServer } from './helper.mjs'
43

packages/datadog-plugin-fastify/test/integration-test/server2.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dd-trace/init.js'
21
import Fastify from 'fastify'
32
import { createAndStartServer } from './helper.mjs'
43

packages/dd-trace/test/setup/mocha.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('node:assert')
44
const util = require('node:util')
55
const { platform } = require('node:os')
66
const path = require('node:path')
7+
const fs = require('node:fs')
78

89
const { expect } = require('chai')
910
const { describe, it, beforeEach, afterEach, before, after } = require('mocha')
@@ -25,6 +26,7 @@ exports.withVersions = withVersions
2526
exports.withExports = withExports
2627
exports.withNamingSchema = withNamingSchema
2728
exports.withPeerService = withPeerService
29+
exports.insertVersionDep = insertVersionDep
2830

2931
const testedPlugins = agent.testedPlugins
3032

@@ -321,6 +323,30 @@ function getModulePath (moduleName, version) {
321323
return `../../../../versions/${moduleName}@${version}`
322324
}
323325

326+
/**
327+
* Sets up a symlink to the tested version of a package in the given directory,
328+
* and tears it down once tests are done.
329+
*
330+
* @param {string} dir - The directory to operate in. Typically __dirname.
331+
* @param {string} pkgName - The name of the package being tested.
332+
* @param {string} version - The "version" string, as used with `withVersions`
333+
*/
334+
function insertVersionDep (dir, pkgName, version) {
335+
const nmDir = path.join(dir, 'node_modules')
336+
const pkgDir = path.join(nmDir, pkgName)
337+
338+
before(() => {
339+
const pkgPath = path.dirname(require(getModulePath(pkgName, version)).pkgJsonPath())
340+
fs.mkdirSync(nmDir)
341+
fs.symlinkSync(pkgPath, pkgDir)
342+
})
343+
344+
after(() => {
345+
fs.unlinkSync(pkgDir)
346+
fs.rmdirSync(nmDir)
347+
})
348+
}
349+
324350
exports.mochaHooks = {
325351
afterEach () {
326352
agent.reset()

0 commit comments

Comments
 (0)