Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Migrated test/versioned/express to node:test #2609

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@
*/

'use strict'

const test = require('tap').test
const test = require('node:test')
const helper = require('../../lib/agent_helper')
const http = require('http')
const { isExpress5 } = require('./utils')
const tsplan = require('@matteo.collina/tspl')
const promiseResolvers = require('../../lib/promise-resolvers')

// This test is no longer applicable in express 5 as mounting a child router does not emit the same
// mount event
test('app should be at top of stack when mounted', { skip: isExpress5 }, function (t) {
test('app should be at top of stack when mounted', { skip: isExpress5() }, function (t, end) {
const agent = helper.instrumentMockedAgent()
const express = require('express')

t.teardown(() => {
t.after(() => {
helper.unloadAgent(agent)
})

t.plan(1)
const plan = tsplan(t, { plan: 1 })

const main = express()
const child = express()

child.on('mount', function () {
t.equal(main._router.stack.length, 3, '3 middleware functions: query parser, Express, child')
plan.equal(main._router.stack.length, 3, '3 middleware functions: query parser, Express, child')
end()
})

main.use(child)
})

test('app should be at top of stack when mounted', function (t) {
test('app should be at top of stack when mounted', async function (t) {
const { promise, resolve } = promiseResolvers()
const agent = helper.instrumentMockedAgent()

const express = require('express')
Expand All @@ -42,10 +45,19 @@ test('app should be at top of stack when mounted', function (t) {
const router = new express.Router()
const router2 = new express.Router()
const server = http.createServer(main)
// track how many requests and wait for all to be done
let tests = 0

const int = setInterval(() => {
if (tests === 5) {
resolve()
}
}, 10)

t.teardown(function () {
t.after(function () {
helper.unloadAgent(agent)
server.close()
clearInterval(int)
})

main.use('/:app', app)
Expand All @@ -58,7 +70,7 @@ test('app should be at top of stack when mounted', function (t) {
router2.get('/', respond)
main.get('/:foo/:bar', respond)

t.plan(10)
const plan = tsplan(t, { plan: 10 })

// store finished transactions
const finishedTransactions = {}
Expand All @@ -70,59 +82,67 @@ test('app should be at top of stack when mounted', function (t) {
server.listen(port, function () {
const host = 'http://localhost:' + port
helper.makeGetRequest(host + '/myApp/myChild/app', function (err, res, body) {
t.notOk(err)
t.equal(
plan.ok(!err)
plan.equal(
finishedTransactions[body].nameState.getName(),
'Expressjs/GET//:app/:child/app',
'should set partialName correctly for nested apps'
)
++tests
})

helper.makeGetRequest(host + '/myApp/nestedApp ', function (err, res, body) {
t.notOk(err)
t.equal(
plan.ok(!err)
plan.equal(
finishedTransactions[body].nameState.getName(),
'Expressjs/GET//:app/nestedApp',
'should set partialName correctly for deeply nested apps'
)
++tests
})

helper.makeGetRequest(host + '/myApp/myChild/router', function (err, res, body) {
t.notOk(err)
t.equal(
plan.ok(!err)
plan.equal(
finishedTransactions[body].nameState.getName(),
'Expressjs/GET//:router/:child/router',
'should set partialName correctly for nested routers'
)
++tests
})

helper.makeGetRequest(host + '/myApp/nestedRouter', function (err, res, body) {
t.notOk(err)
t.equal(
plan.ok(!err)
plan.equal(
finishedTransactions[body].nameState.getName(),
'Expressjs/GET//:router/nestedRouter',
'should set partialName correctly for deeply nested routers'
)
++tests
})

helper.makeGetRequest(host + '/foo/bar', function (err, res, body) {
t.notOk(err)
t.equal(
plan.ok(!err)
plan.equal(
finishedTransactions[body].nameState.getName(),
'Expressjs/GET//:foo/:bar',
'should reset partialName after a router without a matching route'
)
++tests
})
})
})

function respond(req, res) {
res.send(agent.getTransaction().id)
const tx = agent.getTransaction()
res.send(tx.id)
}

await promise
})

test('should not pass wrong args when transaction is not present', function (t) {
t.plan(5)
test('should not pass wrong args when transaction is not present', function (t, end) {
const plan = tsplan(t, { plan: 5 })

const agent = helper.instrumentMockedAgent()

Expand All @@ -136,7 +156,7 @@ test('should not pass wrong args when transaction is not present', function (t)
main.use('/', router)
main.use('/', router2)

t.teardown(function () {
t.after(function () {
helper.unloadAgent(agent)
server.close()
})
Expand All @@ -148,18 +168,18 @@ test('should not pass wrong args when transaction is not present', function (t)
})

router2.get('/', function (req, res, next) {
t.equal(req, args[0])
t.equal(res, args[1])
t.equal(typeof next, 'function')
plan.equal(req, args[0])
plan.equal(res, args[1])
plan.equal(typeof next, 'function')
res.send('ok')
})

helper.randomPort(function (port) {
server.listen(port, function () {
helper.makeGetRequest('http://localhost:' + port + '/', function (err, res, body) {
t.notOk(err)
t.equal(body, 'ok')
t.end()
plan.ok(!err)
plan.equal(body, 'ok')
end()
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/

'use strict'

const assert = require('node:assert')
const path = require('path')
const test = require('tap').test
const test = require('node:test')
const fork = require('child_process').fork
const { isExpress5 } = require('./utils')

Expand All @@ -17,26 +17,26 @@ const { isExpress5 } = require('./utils')
*/
const COMPLETION = 27

test('Express async throw', { skip: isExpress5() }, function (t) {
test('Express async throw', { skip: isExpress5() }, function (t, end) {
const erk = fork(path.join(__dirname, 'erk.js'))
let timer

erk.on('error', function (error) {
t.fail(error)
t.end()
assert.ok(!error)
end()
})

erk.on('exit', function (code) {
clearTimeout(timer)
t.notEqual(code, COMPLETION, "request didn't complete")
t.end()
assert.notEqual(code, COMPLETION, "request didn't complete")
end()
})

// wait for the child vm to boot
erk.on('message', function (message) {
if (message === 'ready') {
timer = setTimeout(function () {
t.fail('hung waiting for exit')
end(new Error('hung waiting for exit'))
erk.kill()
}, 1000)
timer.unref()
Expand Down
86 changes: 0 additions & 86 deletions test/versioned/express/async-handlers.tap.js

This file was deleted.

Loading
Loading