Skip to content

Commit 93025ab

Browse files
pabloerhardrochdev
authored andcommitted
added esm test for body-parser (#7304)
1 parent e0d1463 commit 93025ab

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const assert = require('node:assert/strict')
4+
const axios = require('axios')
5+
const {
6+
sandboxCwd, useSandbox, varySandbox,
7+
FakeAgent, spawnPluginIntegrationTestProc
8+
} = require('../../../../integration-tests/helpers')
9+
const { withVersions } = require('../../../dd-trace/test/setup/mocha')
10+
11+
withVersions('body-parser', 'body-parser', version => {
12+
describe('ESM', () => {
13+
let variants, proc, agent
14+
15+
useSandbox([`'body-parser@${version}'`, 'express'], false,
16+
['./packages/datadog-plugin-body-parser/test/integration-test/*'])
17+
18+
before(function () {
19+
variants = varySandbox('server.mjs', 'bodyParser', undefined, 'body-parser')
20+
})
21+
22+
beforeEach(async () => {
23+
agent = await new FakeAgent().start()
24+
})
25+
26+
afterEach(async () => {
27+
proc?.kill()
28+
await agent.stop()
29+
})
30+
31+
for (const variant of varySandbox.VARIANTS) {
32+
it(`is instrumented ${variant}`, async () => {
33+
proc = await spawnPluginIntegrationTestProc(sandboxCwd(), variants[variant], agent.port)
34+
const response = await axios.post(`${proc.url}/`, { key: 'value' })
35+
assert.equal(response.headers['x-counter'], '1')
36+
})
37+
}
38+
})
39+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'dd-trace/init.js'
2+
import express from 'express'
3+
import bodyParser from 'body-parser'
4+
import dc from 'dc-polyfill'
5+
const bodyParserReadCh = dc.channel('datadog:body-parser:read:finish')
6+
7+
let counter = 0
8+
bodyParserReadCh.subscribe(() => {
9+
counter += 1
10+
})
11+
const app = express()
12+
13+
app.use(bodyParser.json())
14+
app.post('/', (req, res) => {
15+
res.setHeader('X-Counter', counter)
16+
res.end('hello, world\n')
17+
})
18+
19+
const server = app.listen(0, () => {
20+
const port = server.address().port
21+
process.send({ port })
22+
})

packages/dd-trace/test/plugins/plugin-structure.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const missingPlugins = [
3737
'datadog-plugin-pug', // pug does not produce spans
3838
'datadog-plugin-vm', // vm does not produce spans
3939
'datadog-plugin-sequelize', // sequelize does not produce spans
40+
'datadog-plugin-body-parser', // body-parser does not produce spans
4041
]
4142

4243
// instrumentations that do not have a hook, but are still instrumented

0 commit comments

Comments
 (0)