From e5e05480bd133f01217cb6276aa995859704e2ff Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Mon, 12 Feb 2024 12:01:36 +0100 Subject: [PATCH] chore: transform tests from tap to native test runner (#2719) --- test/client-head-reset-override.js | 92 +++++++++++++------------ test/client-post.js | 98 +++++++++++++++------------ test/client-reconnect.js | 57 ++++++++-------- test/client-unref.js | 32 +++++---- test/client-write-max-listeners.js | 31 +++++---- test/connect-errconnect.js | 13 ++-- test/fetch/issue-2294-patch-method.js | 9 +-- test/issue-1670.js | 6 +- test/issue-2065.js | 13 ++-- 9 files changed, 194 insertions(+), 157 deletions(-) diff --git a/test/client-head-reset-override.js b/test/client-head-reset-override.js index 9639e94860d..4582aa0a79c 100644 --- a/test/client-head-reset-override.js +++ b/test/client-head-reset-override.js @@ -1,62 +1,68 @@ 'use strict' +const { tspl } = require('@matteo.collina/tspl') +const { once } = require('node:events') const { createServer } = require('node:http') -const { test } = require('tap') +const { test, after } = require('node:test') const { Client } = require('..') -test('override HEAD reset', (t) => { +test('override HEAD reset', async (t) => { + t = tspl(t, { plan: 4 }) + const expected = 'testing123' const server = createServer((req, res) => { if (req.method === 'GET') { res.write(expected) } res.end() - }) - t.teardown(server.close.bind(server)) + }).listen(0) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => server.close()) - let done - client.on('disconnect', () => { - if (!done) { - t.fail() - } - }) + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.close()) - client.request({ - path: '/', - method: 'HEAD', - reset: false - }, (err, res) => { - t.error(err) - res.body.resume() - }) + let done + client.on('disconnect', () => { + if (!done) { + t.fail() + } + }) - client.request({ - path: '/', - method: 'HEAD', - reset: false - }, (err, res) => { - t.error(err) - res.body.resume() - }) + client.request({ + path: '/', + method: 'HEAD', + reset: false + }, (err, res) => { + t.ifError(err) + res.body.resume() + }) - client.request({ - path: '/', - method: 'GET', - reset: false - }, (err, res) => { - t.error(err) - let str = '' - res.body.on('data', (data) => { - str += data - }).on('end', () => { - t.same(str, expected) - done = true - t.end() - }) + client.request({ + path: '/', + method: 'HEAD', + reset: false + }, (err, res) => { + t.ifError(err) + res.body.resume() + }) + + client.request({ + path: '/', + method: 'GET', + reset: false + }, (err, res) => { + t.ifError(err) + let str = '' + res.body.on('data', (data) => { + str += data + }).on('end', () => { + t.strictEqual(str, expected) + done = true + t.end() }) }) + + await t.completed }) diff --git a/test/client-post.js b/test/client-post.js index 82203ad36c7..e666faf0714 100644 --- a/test/client-post.js +++ b/test/client-post.js @@ -1,73 +1,83 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') +const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const { Blob } = require('node:buffer') -test('request post blob', { skip: !Blob }, (t) => { - t.plan(4) +test('request post blob', { skip: !Blob }, async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer(async (req, res) => { - t.equal(req.headers['content-type'], 'application/json') + t.strictEqual(req.headers['content-type'], 'application/json') let str = '' for await (const chunk of req) { str += chunk } - t.equal(str, 'asd') + t.strictEqual(str, 'asd') res.end() }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) - - client.request({ - path: '/', - method: 'GET', - body: new Blob(['asd'], { - type: 'application/json' - }) - }, (err, data) => { - t.error(err) - data.body.resume().on('end', () => { - t.ok(true, 'pass') - }) + after(server.close.bind(server)) + + server.listen(0) + + await once(server, 'listening') + + const client = new Client(`http://localhost:${server.address().port}`) + after(client.destroy.bind(client)) + + client.request({ + path: '/', + method: 'GET', + body: new Blob(['asd'], { + type: 'application/json' + }) + }, (err, data) => { + t.ifError(err) + data.body.resume().on('end', () => { + t.end() }) }) + await t.completed }) -test('request post arrayBuffer', { skip: !Blob }, (t) => { - t.plan(3) +test('request post arrayBuffer', { skip: !Blob }, async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer(async (req, res) => { let str = '' for await (const chunk of req) { str += chunk } - t.equal(str, 'asd') + t.strictEqual(str, 'asd') res.end() }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) - - const buf = Buffer.from('asd') - const dst = new ArrayBuffer(buf.byteLength) - buf.copy(new Uint8Array(dst)) - - client.request({ - path: '/', - method: 'GET', - body: dst - }, (err, data) => { - t.error(err) - data.body.resume().on('end', () => { - t.ok(true, 'pass') - }) + + after(() => server.close()) + + server.listen(0) + + await once(server, 'listening') + + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.destroy()) + + const buf = Buffer.from('asd') + const dst = new ArrayBuffer(buf.byteLength) + buf.copy(new Uint8Array(dst)) + + client.request({ + path: '/', + method: 'GET', + body: dst + }, (err, data) => { + t.ifError(err) + data.body.resume().on('end', () => { + t.ok(true, 'pass') }) }) + + await t.completed }) diff --git a/test/client-reconnect.js b/test/client-reconnect.js index d8ed2ce4ddd..34355ce40ac 100644 --- a/test/client-reconnect.js +++ b/test/client-reconnect.js @@ -1,54 +1,57 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') +const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const FakeTimers = require('@sinonjs/fake-timers') const timers = require('../lib/timers') -test('multiple reconnect', (t) => { - t.plan(5) +test('multiple reconnect', async (t) => { + t = tspl(t, { plan: 5 }) let n = 0 const clock = FakeTimers.install() - t.teardown(clock.uninstall.bind(clock)) + after(() => clock.uninstall()) const orgTimers = { ...timers } Object.assign(timers, { setTimeout, clearTimeout }) - t.teardown(() => { + after(() => { Object.assign(timers, orgTimers) }) const server = createServer((req, res) => { n === 0 ? res.destroy() : res.end('ok') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + server.listen(0) + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`) + after(client.destroy.bind(client)) - client.request({ path: '/', method: 'GET' }, (err, data) => { - t.ok(err) - t.equal(err.code, 'UND_ERR_SOCKET') - }) - - client.request({ path: '/', method: 'GET' }, (err, data) => { - t.error(err) - data.body - .resume() - .on('end', () => { - t.ok(true, 'pass') - }) - }) + client.request({ path: '/', method: 'GET' }, (err, data) => { + t.ok(err) + t.strictEqual(err.code, 'UND_ERR_SOCKET') + }) - client.on('disconnect', () => { - if (++n === 1) { + client.request({ path: '/', method: 'GET' }, (err, data) => { + t.ifError(err) + data.body + .resume() + .on('end', () => { t.ok(true, 'pass') - } - process.nextTick(() => { - clock.tick(1000) }) + }) + + client.on('disconnect', () => { + if (++n === 1) { + t.ok(true, 'pass') + } + process.nextTick(() => { + clock.tick(1000) }) }) + await t.completed }) diff --git a/test/client-unref.js b/test/client-unref.js index f46376f9c46..49df4244e8e 100644 --- a/test/client-unref.js +++ b/test/client-unref.js @@ -3,35 +3,41 @@ const { Worker, isMainThread, workerData } = require('node:worker_threads') if (isMainThread) { - const tap = require('tap') + const { tspl } = require('@matteo.collina/tspl') + const { test, after } = require('node:test') + const { once } = require('node:events') const { createServer } = require('node:http') - tap.test('client automatically closes itself when idle', t => { - t.plan(1) + test('client automatically closes itself when idle', async t => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.end() }) - t.teardown(server.close.bind(server)) + after(server.close.bind(server)) server.keepAliveTimeout = 9999 - server.listen(0, () => { - const url = `http://localhost:${server.address().port}` - const worker = new Worker(__filename, { workerData: { url } }) - worker.on('exit', code => { - t.equal(code, 0) - }) + server.listen(0) + + await once(server, 'listening') + const url = `http://localhost:${server.address().port}` + const worker = new Worker(__filename, { workerData: { url } }) + worker.on('exit', code => { + t.strictEqual(code, 0) }) + await t.completed }) - tap.test('client automatically closes itself if the server is not there', t => { - t.plan(1) + test('client automatically closes itself if the server is not there', async t => { + t = tspl(t, { plan: 1 }) const url = 'http://localhost:4242' // hopefully empty port const worker = new Worker(__filename, { workerData: { url } }) worker.on('exit', code => { - t.equal(code, 0) + t.strictEqual(code, 0) }) + + await t.completed }) } else { const { Client } = require('..') diff --git a/test/client-write-max-listeners.js b/test/client-write-max-listeners.js index 237bb527f10..c76ce60b0ab 100644 --- a/test/client-write-max-listeners.js +++ b/test/client-write-max-listeners.js @@ -1,19 +1,21 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') +const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') -test('socket close listener does not leak', (t) => { - t.plan(32) +test('socket close listener does not leak', async (t) => { + t = tspl(t, { plan: 32 }) const server = createServer() server.on('request', (req, res) => { res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) const makeBody = () => { return new Readable({ @@ -26,7 +28,7 @@ test('socket close listener does not leak', (t) => { } const onRequest = (err, data) => { - t.error(err) + t.ifError(err) data.body.on('end', () => t.ok(true, 'pass')).resume() } @@ -36,16 +38,19 @@ test('socket close listener does not leak', (t) => { } } process.on('warning', onWarning) - t.teardown(() => { + after(() => { process.removeListener('warning', onWarning) }) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + server.listen(0) - for (let n = 0; n < 16; ++n) { - client.request({ path: '/', method: 'GET', body: makeBody() }, onRequest) - } - }) + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.destroy()) + + for (let n = 0; n < 16; ++n) { + client.request({ path: '/', method: 'GET', body: makeBody() }, onRequest) + } + + await t.completed }) diff --git a/test/connect-errconnect.js b/test/connect-errconnect.js index 85b11f83847..7c241c1453e 100644 --- a/test/connect-errconnect.js +++ b/test/connect-errconnect.js @@ -1,14 +1,15 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Client } = require('..') const net = require('node:net') -test('connect-connectionError', t => { - t.plan(2) +test('connect-connectionError', async t => { + t = tspl(t, { plan: 2 }) const client = new Client('http://localhost:9000') - t.teardown(client.close.bind(client)) + after(() => client.close()) client.once('connectionError', () => { t.ok(true, 'pass') @@ -27,6 +28,8 @@ test('connect-connectionError', t => { path: '/', method: 'GET' }, (err) => { - t.equal(err, _err) + t.strictEqual(err, _err) }) + + await t.completed }) diff --git a/test/fetch/issue-2294-patch-method.js b/test/fetch/issue-2294-patch-method.js index bac7ef51aa9..6223989c07c 100644 --- a/test/fetch/issue-2294-patch-method.js +++ b/test/fetch/issue-2294-patch-method.js @@ -1,19 +1,20 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Request } = require('../..') test('Using `patch` method emits a warning.', (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const { emitWarning } = process - t.teardown(() => { + after(() => { process.emitWarning = emitWarning }) process.emitWarning = (warning, options) => { - t.equal(options.code, 'UNDICI-FETCH-patch') + t.strictEqual(options.code, 'UNDICI-FETCH-patch') } // eslint-disable-next-line no-new diff --git a/test/issue-1670.js b/test/issue-1670.js index c27bdb272dc..7a0cda32669 100644 --- a/test/issue-1670.js +++ b/test/issue-1670.js @@ -1,12 +1,10 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') const { request } = require('..') -test('https://github.com/mcollina/undici/issues/810', async (t) => { +test('https://github.com/mcollina/undici/issues/810', async () => { const { body } = await request('https://api.github.com/user/emails') await body.text() - - t.end() }) diff --git a/test/issue-2065.js b/test/issue-2065.js index 5080353a584..14bca22d311 100644 --- a/test/issue-2065.js +++ b/test/issue-2065.js @@ -1,18 +1,21 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { createServer } = require('node:http') const { once } = require('node:events') const { createReadStream } = require('node:fs') const { File, FormData, request } = require('..') test('undici.request with a FormData body should set content-length header', async (t) => { + t = tspl(t, { plan: 1 }) + const server = createServer((req, res) => { t.ok(req.headers['content-length']) res.end() }).listen(0) - t.teardown(server.close.bind(server)) + after(() => server.close()) await once(server, 'listening') const body = new FormData() @@ -25,12 +28,14 @@ test('undici.request with a FormData body should set content-length header', asy }) test('undici.request with a FormData stream value should set transfer-encoding header', async (t) => { + t = tspl(t, { plan: 1 }) + const server = createServer((req, res) => { - t.equal(req.headers['transfer-encoding'], 'chunked') + t.strictEqual(req.headers['transfer-encoding'], 'chunked') res.end() }).listen(0) - t.teardown(server.close.bind(server)) + after(() => server.close()) await once(server, 'listening') class BlobFromStream {