Skip to content

Test: Fastify #5555 - move to node test runner #404

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

Merged
merged 11 commits into from
Feb 12, 2025
Merged
2 changes: 2 additions & 0 deletions .borp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- 'test/**/*.js'
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"lint:typescript": "npm run lint:fix - --parser @typescript-eslint/parser --plugin typescript \"test/types/*.ts\"",
"test": "npm run lint && tap \"test/*.js\" && npm run typescript",
"test": "npm run lint && borp -C --check-coverage --reporter=@jsumners/line-reporter && npm run typescript",
"typescript": "tsd"
},
"repository": {
Expand Down Expand Up @@ -59,8 +59,10 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"@fastify/websocket": "^11.0.1",
"@jsumners/line-reporter": "^1.0.1",
"@types/node": "^22.0.0",
"@types/ws": "^8.5.10",
"borp": "^0.19.0",
"eslint": "^9.17.0",
"express": "^4.19.2",
"express-http-proxy": "^2.0.0",
Expand All @@ -73,7 +75,6 @@
"simple-get": "^4.0.1",
"socket.io": "^4.7.5",
"socket.io-client": "^4.7.5",
"tap": "^18.7.2",
"tsd": "^0.31.0",
"typescript": "~5.7.2",
"why-is-node-running": "^3.0.0"
Expand Down
14 changes: 7 additions & 7 deletions test/export.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict'

const t = require('tap')
const test = require('node:test')

const fastifyHttpProxy = require('..')
const defaultExport = require('..').default
const { fastifyHttpProxy: namedExport } = require('..')

t.test('module export', function (t) {
test('module export', function (t) {
t.plan(1)
t.equal(typeof fastifyHttpProxy, 'function')
t.assert.strictEqual(typeof fastifyHttpProxy, 'function')
})

t.test('default export', function (t) {
test('default export', function (t) {
t.plan(1)
t.equal(typeof defaultExport, 'function')
t.assert.strictEqual(typeof defaultExport, 'function')
})

t.test('named export', function (t) {
test('named export', function (t) {
t.plan(1)
t.equal(typeof namedExport, 'function')
t.assert.strictEqual(typeof namedExport, 'function')
})
41 changes: 24 additions & 17 deletions test/socket.io.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const proxy = require('../')
const ioServer = require('socket.io')
Expand All @@ -9,44 +9,51 @@ const { createServer } = require('node:http')
const { promisify } = require('node:util')
const { once } = require('node:events')

test('proxy socket.io', async t => {
test('proxy socket.io', async (t) => {
t.plan(2)

const srvUpstream = createServer()
t.teardown(srvUpstream.close.bind(srvUpstream))
t.after(() => {
srvUpstream.close()
})

const srvSocket = new ioServer.Server(srvUpstream)
t.teardown(srvSocket.close.bind(srvSocket))
t.after(() => {
srvSocket.close()
})

await promisify(srvUpstream.listen.bind(srvUpstream))(0)

const srvProxy = Fastify()
t.teardown(srvProxy.close.bind(srvProxy))
t.after(() => {
srvProxy.close()
})

srvProxy.register(proxy, {
upstream: `http://127.0.0.1:${srvUpstream.address().port}`,
websocket: true
websocket: true,
})

await srvProxy.listen({ port: 0, host: '127.0.0.1' })

srvSocket.on('connection', socket => {
socket.on('hello', data => {
t.equal(data, 'world')
srvSocket.on('connection', (socket) => {
socket.on('hello', (data) => {
t.assert.strictEqual(data, 'world')
socket.emit('hi', 'socket')
})
})

const cliSocket = ioClient(`http://127.0.0.1:${srvProxy.server.address().port}`)
t.teardown(cliSocket.close.bind(cliSocket))
const cliSocket = ioClient(
`http://127.0.0.1:${srvProxy.server.address().port}`
)
t.after(() => {
cliSocket.close()
})

cliSocket.emit('hello', 'world')

const out = await once(cliSocket, 'hi')
t.equal(out[0], 'socket')
const [out] = await once(cliSocket, 'hi')
t.assert.strictEqual(out, 'socket')

await Promise.all([
once(cliSocket, 'disconnect'),
srvProxy.close()
])
await Promise.all([once(cliSocket, 'disconnect'), srvProxy.close()])
})
Loading
Loading