Skip to content

Commit 1e08b2a

Browse files
authored
test: fix next test network error by removing get-port usage (#6051)
* fix appsec next test as well * fix standalone mode
1 parent 5f88def commit 1e08b2a

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

packages/datadog-plugin-next/test/index.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const path = require('node:path')
66
const axios = require('axios')
7-
const getPort = require('get-port')
87
const { execSync, spawn } = require('node:child_process')
98
const { withNamingSchema, withVersions } = require('../../dd-trace/test/setup/mocha')
109
const agent = require('../../dd-trace/test/plugins/agent')
@@ -25,8 +24,6 @@ describe('Plugin', function () {
2524

2625
const startServer = ({ withConfig, standalone }, schemaVersion = 'v0', defaultToGlobalService = false) => {
2726
before(async () => {
28-
port = await getPort()
29-
3027
return agent.load('next')
3128
})
3229

@@ -41,7 +38,7 @@ describe('Plugin', function () {
4138
env: {
4239
...process.env,
4340
VERSION: version,
44-
PORT: port,
41+
PORT: 0,
4542
DD_TRACE_AGENT_PORT: agent.server.address().port,
4643
WITH_CONFIG: withConfig,
4744
DD_TRACE_SPAN_ATTRIBUTE_SCHEMA: schemaVersion,
@@ -56,8 +53,12 @@ describe('Plugin', function () {
5653
server.once('error', done)
5754

5855
function waitUntilServerStarted (chunk) {
59-
const chunkString = chunk.toString()
60-
if (chunkString?.includes(port) || chunkString?.includes('Ready ')) {
56+
const chunkStr = chunk.toString()
57+
const match = chunkStr.match(/port:? (\d+)/) ||
58+
chunkStr.match(/http:\/\/127\.0\.0\.1:(\d+)/)
59+
60+
if (match) {
61+
port = Number(match[1])
6162
server.stdout.off('data', waitUntilServerStarted)
6263
done()
6364
}

packages/dd-trace/test/appsec/index.next.plugin.spec.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const { spawn, execSync } = require('child_process')
44
const { cpSync, mkdirSync, rmdirSync, unlinkSync } = require('fs')
5-
const getPort = require('get-port')
65
const axios = require('axios')
76
const { writeFileSync } = require('fs')
87
const { satisfies } = require('semver')
@@ -105,8 +104,6 @@ describe('test suite', () => {
105104
const appDir = path.join(__dirname, 'next', appName)
106105

107106
before(async () => {
108-
port = await getPort()
109-
110107
return agent.load('next')
111108
})
112109

@@ -119,7 +116,7 @@ describe('test suite', () => {
119116
env: {
120117
...process.env,
121118
VERSION: version,
122-
PORT: port,
119+
PORT: 0,
123120
DD_TRACE_AGENT_PORT: agent.server.address().port,
124121
DD_TRACE_SPAN_ATTRIBUTE_SCHEMA: schemaVersion,
125122
DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED: defaultToGlobalService,
@@ -129,9 +126,20 @@ describe('test suite', () => {
129126
})
130127

131128
server.once('error', done)
132-
server.stdout.once('data', () => {
133-
done()
134-
})
129+
130+
function waitUntilServerStarted (chunk) {
131+
const chunkStr = chunk.toString()
132+
const match = chunkStr.match(/port:? (\d+)/) ||
133+
chunkStr.match(/http:\/\/127\.0\.0\.1:(\d+)/)
134+
135+
if (match) {
136+
port = Number(match[1])
137+
server.stdout.off('data', waitUntilServerStarted)
138+
done()
139+
}
140+
}
141+
server.stdout.on('data', waitUntilServerStarted)
142+
135143
server.stderr.on('data', chunk => process.stderr.write(chunk))
136144
server.stdout.on('data', chunk => process.stdout.write(chunk))
137145
})

packages/dd-trace/test/appsec/next/pages-dir/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { createServer } = require('http')
77
const { parse } = require('url')
88
const next = require('next')
99

10-
const app = next({ dir: __dirname, dev: false, quiet: true, hostname: HOSTNAME })
10+
const app = next({ dir: __dirname, dev: false, quiet: true, hostname: HOSTNAME, port: PORT })
1111
const handle = app.getRequestHandler()
1212

1313
app.prepare().then(() => {

0 commit comments

Comments
 (0)