From 0f43456a7bdf9a353d5685e0e59fdab21f55488b Mon Sep 17 00:00:00 2001 From: Myrotvorets Date: Fri, 20 Dec 2024 06:06:42 +0200 Subject: [PATCH] Update code --- test/date.test.mts | 3 ++- test/helpers/setup.mts | 4 +++- test/logger.test.mts | 9 +++++---- test/mode.test.mts | 3 ++- test/remote-user.test.mts | 3 ++- test/req.test.mts | 11 ++++++----- test/res.test.mts | 3 ++- test/settokenhandler.test.mts | 3 ++- test/total-time.test.mts | 13 ++++++++----- test/url.test.mts | 3 ++- 10 files changed, 34 insertions(+), 21 deletions(-) diff --git a/test/date.test.mts b/test/date.test.mts index 0802429..6b48cd6 100644 --- a/test/date.test.mts +++ b/test/date.test.mts @@ -1,5 +1,6 @@ import { afterEach, before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; import type { Express } from 'express'; @@ -18,7 +19,7 @@ await describe(':date', async () => { afterEach(unmockDate); const checker = (app: Express, stream: WritableBufferStream, expected: string): Promise => - request(app) + request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), expected)); diff --git a/test/helpers/setup.mts b/test/helpers/setup.mts index e271e28..afe5017 100644 --- a/test/helpers/setup.mts +++ b/test/helpers/setup.mts @@ -4,7 +4,9 @@ import express, { type Express, type RequestHandler } from 'express'; export let stream: WritableBufferStream; // NOSONAR export let app: Express; // NOSONAR -export const genericHandler: RequestHandler = (_req, res) => res.json({ hello: 'world' }); +export const genericHandler: RequestHandler = (_req, res) => { + res.json({ hello: 'world' }); +}; export function beforeSuite(): void { stream = new WritableBufferStream(); diff --git a/test/logger.test.mts b/test/logger.test.mts index 6cd1651..92584cc 100644 --- a/test/logger.test.mts +++ b/test/logger.test.mts @@ -1,5 +1,6 @@ import { afterEach, before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import { app, beforeSuite, beforeTest, genericHandler, stream } from './helpers/setup.mjs'; import { requestLogger } from '../src/index.mjs'; @@ -24,7 +25,7 @@ await describe('RequestLogger', async () => { const expectedURL = '/'; const expectedUA = 'WeirdBot/1.2.4'; - await request(app) + await request(app as RequestListener) .get(expectedURL) .set('X-Forwarded-For', expectedIP) .set('User-Agent', expectedUA) @@ -48,7 +49,7 @@ await describe('RequestLogger', async () => { const expectedIP = '192.168.1.1'; const expectedURL = '/'; const expectedUA = 'WeirdBot/1.2.4'; - await request(app) + await request(app as RequestListener) .get(expectedURL) .set('X-Forwarded-For', expectedIP) .set('User-Agent', expectedUA) @@ -72,7 +73,7 @@ await describe('RequestLogger', async () => { genericHandler, ); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), 'LOGGER says: 200')); }); @@ -87,7 +88,7 @@ await describe('RequestLogger', async () => { genericHandler, ); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => equal(stream.toString(), '')); }); diff --git a/test/mode.test.mts b/test/mode.test.mts index dca34ab..b841086 100644 --- a/test/mode.test.mts +++ b/test/mode.test.mts @@ -1,5 +1,6 @@ import { before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; import type { Express } from 'express'; @@ -17,7 +18,7 @@ await describe('Operation Mode', async () => { expectedURL: string, expected: string, ): Promise => - request(app) + request(app as RequestListener) .get(expectedURL) .expect(() => equal(stream.toString().trimEnd(), expected)); diff --git a/test/remote-user.test.mts b/test/remote-user.test.mts index 689a67f..4810547 100644 --- a/test/remote-user.test.mts +++ b/test/remote-user.test.mts @@ -1,5 +1,6 @@ import { before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; import type { Express } from 'express'; @@ -12,7 +13,7 @@ await describe(':remote-user', async () => { beforeEach(beforeTest); const checker = (app: Express, stream: WritableBufferStream, expected: string): Promise => - request(app) + request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), expected)); diff --git a/test/req.test.mts b/test/req.test.mts index 512c654..afcbf19 100644 --- a/test/req.test.mts +++ b/test/req.test.mts @@ -1,5 +1,6 @@ import { before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; import { app, beforeSuite, beforeTest, genericHandler, stream } from './helpers/setup.mjs'; @@ -16,7 +17,7 @@ await describe(':req', async () => { await it('should handle the case when header is not available', async () => { app.use(requestLogger({ format: ':req[x-ping]', stream }), genericHandler); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => checker(stream, '-')); }); @@ -24,7 +25,7 @@ await describe(':req', async () => { await it('should handle the case with no parameters', async () => { app.use(requestLogger({ format: ':req', stream }), genericHandler); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => checker(stream, '-')); }); @@ -33,7 +34,7 @@ await describe(':req', async () => { app.use(requestLogger({ format: ':req[x-ping]', stream }), genericHandler); const expected = 'pong'; - await request(app) + await request(app as RequestListener) .get('/') .set('x-ping', expected) .expect(() => checker(stream, expected)); @@ -42,7 +43,7 @@ await describe(':req', async () => { await it('should handle the case when header is available, but empty', async () => { app.use(requestLogger({ format: ':req[x-ping]', stream }), genericHandler); - await request(app) + await request(app as RequestListener) .get('/') .set('x-ping', '') .expect(() => checker(stream, '-')); @@ -58,7 +59,7 @@ await describe(':req', async () => { genericHandler, ); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => checker(stream, 'pong, pong')); }); diff --git a/test/res.test.mts b/test/res.test.mts index f962907..302a557 100644 --- a/test/res.test.mts +++ b/test/res.test.mts @@ -1,4 +1,5 @@ import { before, beforeEach, describe, it } from 'node:test'; +import type { RequestListener } from 'node:http'; import { equal } from 'node:assert/strict'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; @@ -12,7 +13,7 @@ await describe(':res', async () => { beforeEach(beforeTest); const checker = (app: Express, stream: WritableBufferStream, expected: string): Promise => - request(app) + request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), expected)); diff --git a/test/settokenhandler.test.mts b/test/settokenhandler.test.mts index dc8af55..b5922d9 100644 --- a/test/settokenhandler.test.mts +++ b/test/settokenhandler.test.mts @@ -1,5 +1,6 @@ import { afterEach, before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; import type { Express } from 'express'; @@ -14,7 +15,7 @@ await describe('setTokenHandler', async () => { afterEach(() => setTokenHandler('test', undefined)); const checker = (app: Express, stream: WritableBufferStream, expected: string): Promise => - request(app) + request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), expected)); diff --git a/test/total-time.test.mts b/test/total-time.test.mts index 83939a9..2aec85a 100644 --- a/test/total-time.test.mts +++ b/test/total-time.test.mts @@ -1,5 +1,6 @@ import { before, beforeEach, describe, it } from 'node:test'; import { equal, match } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import { hrtime } from 'node:process'; import request from 'supertest'; import type { Response } from 'express'; @@ -18,7 +19,7 @@ await describe(':total-time', async () => { await it('should log the total time', async () => { app.use(requestLogger({ format: ':total-time', stream }), genericHandler); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => { const line = stream.toString().trimEnd(); @@ -35,12 +36,14 @@ await describe(':total-time', async () => { format: ':total-time', stream, }), - (_req, _res, next) => setTimeout(next, timeout), + (_req, _res, next) => { + setTimeout(next, timeout); + }, genericHandler, ); const now = hrtime.bigint(); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => { const duration = +Number((hrtime.bigint() - now) / BigInt(1e6)).toFixed(3); @@ -62,7 +65,7 @@ await describe(':total-time', async () => { genericHandler, ); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), '-')); }); @@ -80,7 +83,7 @@ await describe(':total-time', async () => { genericHandler, ); - await request(app) + await request(app as RequestListener) .get('/') .expect(() => equal(stream.toString().trimEnd(), '-')); }); diff --git a/test/url.test.mts b/test/url.test.mts index da0442b..1bc02c8 100644 --- a/test/url.test.mts +++ b/test/url.test.mts @@ -1,5 +1,6 @@ import { before, beforeEach, describe, it } from 'node:test'; import { equal } from 'node:assert/strict'; +import type { RequestListener } from 'node:http'; import request from 'supertest'; import type { WritableBufferStream } from '@myrotvorets/buffer-stream'; import type { Express } from 'express'; @@ -12,7 +13,7 @@ await describe(':url', async () => { beforeEach(beforeTest); const checker = (app: Express, stream: WritableBufferStream, expectedURL: string): Promise => - request(app) + request(app as RequestListener) .get(expectedURL) .expect(() => equal(stream.toString().trimEnd(), expectedURL));