From b38a8bc5a61cd874facc685a734fb1d98da0515f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 22 Dec 2024 13:44:23 +0800 Subject: [PATCH] test: add async await tests --- test/supertest.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/supertest.test.ts b/test/supertest.test.ts index d7222e8..446634b 100644 --- a/test/supertest.test.ts +++ b/test/supertest.test.ts @@ -4,6 +4,7 @@ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { AddressInfo } from 'node:net'; +import { once } from 'node:events'; import should from 'should'; import express, { Express } from 'express'; import bodyParser from 'body-parser'; @@ -39,6 +40,42 @@ describe('request(url)', function() { }); }); + it('should promise style', function(done) { + const app = express(); + + app.get('/', function(_req, res) { + res.send('hello'); + }); + + const server = app.listen(function() { + const url = 'http://localhost:' + (server.address() as AddressInfo).port; + request(url) + .get('/') + .expect('hello') + .then(() => done()) + .catch(done); + }); + }); + + it('should async await', async () => { + const app = express(); + + app.get('/', function(_req, res) { + res.send('hello async await'); + }); + + const server = app.listen(); + await once(server, 'listening'); + const url = 'http://localhost:' + (server.address() as AddressInfo).port; + await request(url) + .get('/') + .expect('hello async await'); + + await request.agent(url) + .get('/') + .expect('hello async await'); + }); + describe('.end(cb)', function() { it('should set `this` to the test object when calling cb', function(done) { const app = express();