Skip to content

Commit

Permalink
separate middleware tests into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
ida613 committed Jan 16, 2025
1 parent 5364156 commit b9d1a76
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 207 deletions.
207 changes: 0 additions & 207 deletions packages/datadog-plugin-express/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,213 +1565,6 @@ describe('Plugin', () => {
})
})
})

// middlewareDisabledTest('plugin', [{ middleware: false }, { client: false }], {})
middlewareDisabledTest('tracer', [{}, { client: false }], { middleware: false })

function middlewareDisabledTest (description, pluginConfig, tracerConfig) {
describe.only(`with configuration for middleware disabled via ${description} config`, () => {
before(() => {
return agent.load(['express', 'http'], pluginConfig, tracerConfig)
})

after(() => {
return agent.close({ ritmReset: false })
})

beforeEach(() => {
express = require(`../../../versions/express@${version}`).get()
})

it('should not activate a scope per middleware', done => {
const app = express()

let span

app.use((req, res, next) => {
span = tracer.scope().active()
next()
})

app.get('/user', (req, res) => {
res.status(200).send()
try {
expect(tracer.scope().active()).to.equal(span).and.to.not.be.null
done()
} catch (e) {
done(e)
}
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios.get(`http://localhost:${port}/user`)
.catch(done)
})
})

it('should not do automatic instrumentation on middleware', done => {
const app = express()

app.use((req, res, next) => {
next()
})

app.get('/user', (req, res, next) => {
res.status(200).send()
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('resource', 'GET /user')
expect(traces.length).to.equal(1)
})
.then(done)
.catch(done)

axios.get(`http://localhost:${port}/user`)
.catch(done)
})
})

it('should handle error status codes', done => {
const app = express()

app.use((req, res, next) => {
next()
})

app.get('/user', (req, res) => {
res.status(500).send()
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('error', 1)
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('http.status_code', '500')
expect(spans[0].meta).to.have.property('component', 'express')

done()
})

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})

it('should only handle errors for configured status codes', done => {
const app = express()

app.use((req, res, next) => {
next()
})

app.get('/user', (req, res) => {
res.statusCode = 400
throw new Error('boom')
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('error', 0)
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('http.status_code', '400')
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 400
})
.catch(done)
})
})

it('should handle middleware errors', done => {
const app = express()
const error = new Error('boom')

app.use((req, res) => { throw error })
// eslint-disable-next-line n/handle-callback-err
app.use((error, req, res, next) => res.status(500).send())

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('error', 1)
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name)
expect(spans[0].meta).to.have.property(ERROR_MESSAGE, error.message)
expect(spans[0].meta).to.have.property(ERROR_STACK, error.stack)
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})

it('should handle request errors', done => {
const app = express()
const error = new Error('boom')

app.use(() => { throw error })

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('error', 1)
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name)
expect(spans[0].meta).to.have.property(ERROR_MESSAGE, error.message)
expect(spans[0].meta).to.have.property(ERROR_STACK, error.stack)
expect(spans[0].meta).to.have.property('http.status_code', '500')
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})
})
}
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

const test = require('./middleware-disabled-tests')

test('plugin', [{ middleware: false }, { client: false }], {})
Loading

0 comments on commit b9d1a76

Please sign in to comment.