Skip to content

Commit

Permalink
test: add async await tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 22, 2024
1 parent cd7c6d8 commit b38a8bc
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/supertest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b38a8bc

Please sign in to comment.