From 1e4c8848a2887210c381f92fe33c4e04ded03df8 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 10 Apr 2024 17:04:32 -0700 Subject: [PATCH] test: fix test not exiting --- test.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/test.js b/test.js index 5a7baef..e65a893 100644 --- a/test.js +++ b/test.js @@ -1,17 +1,16 @@ const express = require('express') const bodyParser = require('body-parser') -const test = require('node:test') +const { describe, test, before, after, afterEach } = require('node:test') const assert = require('node:assert/strict') const zlib = require('zlib') const fs = require('fs') const centra = require('./') -const qs = require('querystring') const app = express() app.use(bodyParser.raw()) app.use(bodyParser.json()) -app.use(bodyParser.urlencoded()) +app.use(bodyParser.urlencoded({extended: false})) app.get('/redirectRequireCookie', (req, res) => { if (req.header('Cookie') !== 'a=b') { @@ -121,7 +120,27 @@ app.get('/abort', (req, res) => { res.socket.destroy() }) -const runTests = () => { +let servers = [] + +let failed = false + +describe('Centra', () => { + before(() => { + servers.push(app.listen(8081, () => { + servers.push(app.listen(8082)) + })) + }) + + after(() => { + console.log('Closing servers...') + servers.forEach((s) => { + s.closeAllConnections() + s.close() + }) + console.log('Exit', process.exitCode) + process.exit() + }) + test('Simple GET', async (t) => { if (await (await centra('http://localhost:8081/simpleGet').send()).text() === 'Hey') { return @@ -196,17 +215,6 @@ const runTests = () => { throw new Error('fail') }) - test('Server-aborted request', (t) => new Promise((resolve, reject) => { - const p = centra('http://localhost:8081/abort').send() - p.then(() => { - t.diagnostic('Aborted request did not reject') - reject() - }) - p.catch(() => { - resolve() - }) - })) - test('Update request info on the fly', async (t) => { const res = await centra('http://localhost:8081/test').path('../updates').query('hey', 'hello').header('hey', 'hello').header({ 'test': 'testing' @@ -319,8 +327,4 @@ const runTests = () => { assert.equal(parsed, null) }) -} - -const server = app.listen(8081, () => { - app.listen(8082, runTests) })