From 59ebbe358bd2d01cfcaaf91a118ec10ca1257b0e Mon Sep 17 00:00:00 2001 From: Daniil Sitdikov Date: Fri, 19 Jan 2024 12:01:01 +0100 Subject: [PATCH] refactor(tests): moved from callback approach to sync one, add tests to adapter (#29) * refactor(tests): moved from callback approach to sync one, add tests for http adapter * chore(examples/ws): bump version to 8.16 --- examples/web-socket/package.json | 2 +- examples/web-socket/pnpm-lock.yaml | 8 +- package.json | 2 +- src/file-router.ts | 2 +- .../[...operations]/plain/[slug].ts | 2 +- tests/api-for-adapter/endpoint.[custom].ts | 3 + tests/api-for-adapter/exact.ts | 3 + tests/request-handler.test.ts | 340 ++++++++++-------- tests/test-utils.ts | 39 +- 9 files changed, 224 insertions(+), 177 deletions(-) create mode 100644 tests/api-for-adapter/endpoint.[custom].ts create mode 100644 tests/api-for-adapter/exact.ts diff --git a/examples/web-socket/package.json b/examples/web-socket/package.json index 524deeb..a00ef87 100644 --- a/examples/web-socket/package.json +++ b/examples/web-socket/package.json @@ -14,7 +14,7 @@ }, "dependencies": { "node-file-router": "workspace:../../dist", - "ws": "8.15.0" + "ws": "8.16.0" }, "devDependencies": { "@types/node": "20.1.4", diff --git a/examples/web-socket/pnpm-lock.yaml b/examples/web-socket/pnpm-lock.yaml index f04e912..ca3f78e 100644 --- a/examples/web-socket/pnpm-lock.yaml +++ b/examples/web-socket/pnpm-lock.yaml @@ -9,8 +9,8 @@ dependencies: specifier: workspace:../../dist version: link:../../dist ws: - specifier: 8.15.0 - version: 8.15.0 + specifier: 8.16.0 + version: 8.16.0 devDependencies: '@types/node': @@ -146,8 +146,8 @@ packages: resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==} dev: true - /ws@8.15.0: - resolution: {integrity: sha512-H/Z3H55mrcrgjFwI+5jKavgXvwQLtfPCUEp6pi35VhoB0pfcHnSoyuTzkBEZpzq49g1193CUEwIvmsjcotenYw==} + /ws@8.16.0: + resolution: {integrity: sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==} engines: {node: '>=10.0.0'} peerDependencies: bufferutil: ^4.0.1 diff --git a/package.json b/package.json index c5f4060..bf183d1 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build:types": "tsc --emitDeclarationOnly --declaration --outDir dist/types --rootDir src", "test": "vitest run", "test:coverage": "vitest run --coverage", - "lint": "eslint src", + "lint": "eslint src tests", "prepare": "husky install" }, "module": "./esm/index.mjs", diff --git a/src/file-router.ts b/src/file-router.ts index e0c03d4..3c47380 100644 --- a/src/file-router.ts +++ b/src/file-router.ts @@ -6,8 +6,8 @@ import { isFunction, isRecordWith } from './utils/object.utils'; import { resolveNotFoundHandler } from './components/not-found-resolver'; import { httpAdapter } from './adapters/http-adapter'; import { validateBaseDirExistence } from './validations/validations'; - import { executeWithMiddlewares } from './components/middleware-executer'; + import type { Adapter } from './types/adapter'; import type { RequestHandler } from './types/request-handlers'; diff --git a/tests/api-dynamic-segments/combination/[...operations]/plain/[slug].ts b/tests/api-dynamic-segments/combination/[...operations]/plain/[slug].ts index a18a979..8737aa0 100644 --- a/tests/api-dynamic-segments/combination/[...operations]/plain/[slug].ts +++ b/tests/api-dynamic-segments/combination/[...operations]/plain/[slug].ts @@ -1,3 +1,3 @@ import { createTestRequestHandler } from '../../../../test-utils'; -export default createTestRequestHandler(__filename); \ No newline at end of file +export default createTestRequestHandler(__filename); diff --git a/tests/api-for-adapter/endpoint.[custom].ts b/tests/api-for-adapter/endpoint.[custom].ts new file mode 100644 index 0000000..f0613e4 --- /dev/null +++ b/tests/api-for-adapter/endpoint.[custom].ts @@ -0,0 +1,3 @@ +import { createTestRequestHandler } from '../test-utils'; + +export default createTestRequestHandler(__filename); diff --git a/tests/api-for-adapter/exact.ts b/tests/api-for-adapter/exact.ts new file mode 100644 index 0000000..f0613e4 --- /dev/null +++ b/tests/api-for-adapter/exact.ts @@ -0,0 +1,3 @@ +import { createTestRequestHandler } from '../test-utils'; + +export default createTestRequestHandler(__filename); diff --git a/tests/request-handler.test.ts b/tests/request-handler.test.ts index 2432d83..d723d44 100644 --- a/tests/request-handler.test.ts +++ b/tests/request-handler.test.ts @@ -1,11 +1,11 @@ import { - createTestMethodsRequestRunner, createTestMiddlewareRequestRunner, createTestRequestRunner, expectAfterInit } from './test-utils'; -import { initFileRouter } from '../src/file-router'; +import { initFileRouter } from '../src'; import { describe, expect, it, beforeAll, vi, afterEach } from 'vitest'; +import type { IncomingMessage } from 'node:http'; import type { FileRouterRequestHandler } from 'node-file-router'; const currentCwd = process.cwd(); @@ -20,26 +20,54 @@ describe('RequestHandler', () => { describe('#Initialization', () => { it('should init file router with the default params', async () => { - expect.assertions(1); const requestHandler = await initFileRouter(); const run = createTestRequestRunner(requestHandler); + const { filePath } = await run('/'); - run('/', ({ filePath }) => { - expect(filePath).toBe('/api/index.ts'); - }); + expect(filePath).toBe('/api/index.ts'); }); it('should init file router with an absolute path', async () => { - expect.assertions(1); const baseDir = `${process.cwd()}/tests/api`; - const requestHandler = await initFileRouter({ baseDir }); const run = createTestRequestRunner(requestHandler); - run('/', ({ filePath }) => { - expect(filePath).toBe('/api/index.ts'); + const { filePath } = await run('/'); + + expect(filePath).toBe('/api/index.ts'); + }); + }); + + describe('#Adapter', () => { + const adapter = { + getPathname: (req: IncomingMessage) => req.url || '', + defaultNotFoundHandler: () => {}, + getMethod: undefined + }; + + it('should handle request even getMethod is not defined', async () => { + const requestHandler = await initFileRouter({ + baseDir: 'api-for-adapter', + adapter }); + const run = createTestRequestRunner(requestHandler); + + const { filePath } = await run('/exact'); + + expect(filePath).toBe('/api-for-adapter/exact.ts'); + }); + + it('should override the default method', async () => { + const requestHandler = await initFileRouter({ + baseDir: 'api-for-adapter', + adapter: { ...adapter, getMethod: () => 'custom' } + }); + const run = createTestRequestRunner(requestHandler); + + const { filePath } = await run('/endpoint'); + + expect(filePath).toBe('/api-for-adapter/endpoint.[custom].ts'); }); }); @@ -58,7 +86,7 @@ describe('RequestHandler', () => { }); }); - it('should invoke handler from mapped file', () => { + it('should invoke handler from mapped file', async () => { const mappingTestCases = [ { fromUrl: '', toFile: '/api-basics/index.ts' }, { fromUrl: '/example', toFile: '/api-basics/example.ts' }, @@ -73,98 +101,97 @@ describe('RequestHandler', () => { toFile: '/api-basics/one/[id]/two/three/[id].ts' } ]; - expect.assertions(mappingTestCases.length * 2); const run = createTestRequestRunner(basicCasesRequestHandler); - mappingTestCases.forEach(({ fromUrl, toFile }) => { - run(fromUrl, ({ filePath }) => expect(filePath).toBe(toFile)); - run(`${fromUrl}/`, ({ filePath }) => expect(filePath).toBe(toFile)); - }); - }); - - it('should parse a relative reference url', () => { - expect.assertions(1); - const run = createTestRequestRunner(basicCasesRequestHandler); + await Promise.all( + mappingTestCases.map(async ({ fromUrl, toFile }) => { + await run(fromUrl).then(({ filePath }) => + expect(filePath).toBe(toFile) + ); - run('//site.com/example', ({ filePath }) => - expect(filePath).toBe('/api-basics/example.ts') + await run(`${fromUrl}/`).then(({ filePath }) => + expect(filePath).toBe(toFile) + ); + }) ); }); - it('should parse http reference url', () => { - expect.assertions(1); + it('should parse a relative reference url', async () => { const run = createTestRequestRunner(basicCasesRequestHandler); - - run('http://site.com/example', ({ filePath }) => - expect(filePath).toBe('/api-basics/example.ts') - ); + const { filePath } = await run('//site.com/example'); + expect(filePath).toBe('/api-basics/example.ts'); }); - it('should parse https reference url', () => { - expect.assertions(1); + it('should parse http reference url', async () => { const run = createTestRequestRunner(basicCasesRequestHandler); + const { filePath } = await run('http://site.com/example'); + expect(filePath).toBe('/api-basics/example.ts'); + }); - run('https://site.com/example', ({ filePath }) => - expect(filePath).toBe('/api-basics/example.ts') - ); + it('should parse https reference url', async () => { + const run = createTestRequestRunner(basicCasesRequestHandler); + const { filePath } = await run('https://site.com/example'); + expect(filePath).toBe('/api-basics/example.ts'); }); - it('should skip ignoring files', () => { - expect.assertions(2); + it('should skip ignoring files', async () => { const run = createTestRequestRunner(basicCasesRequestHandler); - run('/one/123/index.some-spec', (res) => + await run('/one/123/index.some-spec').then((res) => + expect(res).toBe('404 Not Found') + ); + await run('/one/123/_private-file').then((res) => expect(res).toBe('404 Not Found') ); - run('/one/123/_private-file', (res) => expect(res).toBe('404 Not Found')); }); - it('should take default 404 fallback when no mapping is found', () => { + it('should take default 404 fallback when no mapping is found', async () => { const run = createTestRequestRunner(basicCasesRequestHandler); - run('/one/123/404', (res) => expect(res).toBe('404 Not Found')); + const res = await run('/one/123/404'); + expect(res).toBe('404 Not Found'); }); - it('should take custom 404 fallback from _404.js file when no mapping is found', () => { + it('should take custom 404 fallback from _404.js file when no mapping is found', async () => { const run = createTestRequestRunner(notFoundCasesRequestHandler); - run('/one/123/404', ({ filePath }) => - expect(filePath).toBe('/api-for-not-found/_404.ts') - ); + const { filePath } = await run('/one/123/404'); + expect(filePath).toBe('/api-for-not-found/_404.ts'); }); - it('should take params from [slug] handlers', () => { + it('should take params from [slug] handlers', async () => { const run = createTestRequestRunner(basicCasesRequestHandler); - run('/one/123', ({ routeParams }) => + + await run('/one/123').then(({ routeParams }) => expect(routeParams).toEqual({ id: '123' }) ); - run('/one/1/two/three/3', ({ routeParams }) => + await run('/one/1/two/three/3').then(({ routeParams }) => expect(routeParams).toEqual({ id: '3' }) ); - run('/one/1/two/2', ({ routeParams }) => - expect(routeParams).toEqual({ - id: '1', - subId: '2' - }) + await run('/one/1/two/2').then(({ routeParams }) => + expect(routeParams).toEqual({ id: '1', subId: '2' }) ); }); - it('should invoke http-method based handlers', () => { - const run = createTestMethodsRequestRunner(basicCasesRequestHandler); - run('/one/1/two/multi-methods/3', 'post', ({ req, routeParams }) => { - expect(routeParams).toEqual({ id: '3' }); - expect(req.method).toBe('post'); - }); + it('should invoke http-method based handlers', async () => { + const run = createTestRequestRunner(basicCasesRequestHandler); - run('/one/1/two/multi-methods/3', 'get', ({ req, routeParams }) => { - expect(routeParams).toEqual({ id: '3' }); - expect(req.method).toBe('get'); - }); + await run('/one/1/two/multi-methods/3', 'post').then( + ({ req, routeParams }) => { + expect(routeParams).toEqual({ id: '3' }); + expect(req?.method).toBe('post'); + } + ); + await run('/one/1/two/multi-methods/3', 'get').then( + ({ req, routeParams }) => { + expect(routeParams).toEqual({ id: '3' }); + expect(req?.method).toBe('get'); + } + ); }); - it('should returns 404 not found if method does not exists', () => { - const run = createTestMethodsRequestRunner(basicCasesRequestHandler); - run('/one/1/two/multi-methods/3', 'put', (res) => - expect(res).toBe('404 Not Found') - ); + it('should returns 404 not found if method does not exists', async () => { + const run = createTestRequestRunner(basicCasesRequestHandler); + const res = await run('/one/1/two/multi-methods/3', 'put'); + expect(res).toBe('404 Not Found'); }); }); @@ -177,69 +204,76 @@ describe('RequestHandler', () => { }); }); - it('should correctly map methods from filename for plain routes', () => { - const run = createTestMethodsRequestRunner(requestHandler); - run('/plain/some-route', 'get', ({ req }) => - expect(req.method).toBe('get') + it('should correctly map methods from filename for plain routes', async () => { + const run = createTestRequestRunner(requestHandler); + + await run('/plain/some-route', 'get').then(({ req }) => + expect(req?.method).toBe('get') ); - run('/plain/some-route', 'post', ({ req }) => - expect(req.method).toBe('post') + await run('/plain/some-route', 'post').then(({ req }) => + expect(req?.method).toBe('post') ); - run('/plain/some-route', 'put', (res) => + await run('/plain/some-route', 'put').then((res) => expect(res).toBe('404 Not Found') ); }); - it('should correctly map methods from filename for exact slug', () => { - const run = createTestMethodsRequestRunner(requestHandler); - run('/dynamic/exact/one', 'get', ({ req, routeParams }) => { + it('should correctly map methods from filename for exact slug', async () => { + const run = createTestRequestRunner(requestHandler); + + await run('/dynamic/exact/one', 'get').then(({ req, routeParams }) => { expect(req.method).toBe('get'); - expect(routeParams.slug).toBe('one'); + expect(routeParams?.slug).toBe('one'); }); - run('/dynamic/exact/one', 'put', (res) => + await run('/dynamic/exact/one', 'put').then((res) => expect(res).toBe('404 Not Found') ); }); - it('should correctly map methods from filename for catch all slugs', () => { - const run = createTestMethodsRequestRunner(requestHandler); - run('/dynamic/catch-all/a/b/c', 'get', ({ req, routeParams }) => { - expect(req.method).toBe('get'); - expect(routeParams.params).toEqual(['a', 'b', 'c']); - }); - run('/dynamic/catch-all/a/b/c', 'put', (res) => + it('should correctly map methods from filename for catch all slugs', async () => { + const run = createTestRequestRunner(requestHandler); + + await run('/dynamic/catch-all/a/b/c', 'get').then( + ({ req, routeParams }) => { + expect(req.method).toBe('get'); + expect(routeParams?.params).toEqual(['a', 'b', 'c']); + } + ); + await run('/dynamic/catch-all/a/b/c', 'put').then((res) => expect(res).toBe('404 Not Found') ); }); - it('should correctly map methods from filename for optional catch all slugs', () => { - const run = createTestMethodsRequestRunner(requestHandler); - run( - '/dynamic/optional-catch-all/a/b/c', - 'get', + it('should correctly map methods from filename for optional catch all slugs', async () => { + const run = createTestRequestRunner(requestHandler); + + await run('/dynamic/optional-catch-all/a/b/c', 'get').then( ({ req, routeParams }) => { expect(req.method).toBe('get'); - expect(routeParams.params).toEqual(['a', 'b', 'c']); + expect(routeParams?.params).toEqual(['a', 'b', 'c']); } ); - run('/dynamic/optional-catch-all', 'get', ({ req, routeParams }) => { - expect(req.method).toBe('get'); - expect(routeParams.params).toEqual(undefined); - }); - run('/dynamic/optional-catch-all', 'put', (res) => { + await run('/dynamic/optional-catch-all', 'get').then( + ({ req, routeParams }) => { + expect(req.method).toBe('get'); + expect(routeParams?.params).toEqual(undefined); + } + ); + await run('/dynamic/optional-catch-all', 'put').then((res) => { expect(res).toBe('404 Not Found'); }); }); - it('should correctly map methods from filename for index files', () => { - const run = createTestMethodsRequestRunner(requestHandler); - run('/root', 'get', ({ req }) => { + it('should correctly map methods from filename for index files', async () => { + const run = createTestRequestRunner(requestHandler); + + await run('/root', 'get').then(({ req }) => { expect(req.method).toBe('get'); }); - run('/root', 'post', ({ req }) => { + await run('/root', 'post').then(({ req }) => { expect(req.method).toBe('post'); }); - run('/root', 'put', (res) => { + await run('/root', 'put').then((res) => { expect(res).toBe('404 Not Found'); }); }); @@ -254,61 +288,64 @@ describe('RequestHandler', () => { }); }); - it('should get one route param from [...slug]', () => { + it('should get one route param from [...slug]', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/catch-all/1', ({ routeParams }) => + + await run('/catch-all/1').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1'] }) ); - run('/catch-all/1/', ({ routeParams }) => + await run('/catch-all/1/').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1'] }) ); }); - it('should get all route params from [...slug]', () => { + it('should get all route params from [...slug]', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/catch-all/1/2/3/4', ({ routeParams }) => + + await run('/catch-all/1/2/3/4').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1', '2', '3', '4'] }) ); - run('/catch-all/1/2/3/4/', ({ routeParams }) => + await run('/catch-all/1/2/3/4/').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1', '2', '3', '4'] }) ); }); - it('should take index file when it is defined', () => { + it('should take index file when it is defined', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/catch-all', ({ routeParams, filePath }) => { + + await run('/catch-all').then(({ routeParams, filePath }) => { expect(filePath).toEqual('/api-dynamic-segments/catch-all/index.ts'); expect(routeParams).toEqual({ slug: undefined }); }); - run('/catch-all/', ({ routeParams, filePath }) => { + await run('/catch-all/').then(({ routeParams, filePath }) => { expect(filePath).toEqual('/api-dynamic-segments/catch-all/index.ts'); expect(routeParams).toEqual({ slug: undefined }); }); }); - it('should catch index file in [[...slug]]', () => { + it('should catch index file in [[...slug]]', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/optional-catch-all', ({ routeParams }) => + await run('/optional-catch-all').then(({ routeParams }) => expect(routeParams).toEqual({ slug: undefined }) ); - run('/optional-catch-all/', ({ routeParams }) => + await run('/optional-catch-all/').then(({ routeParams }) => expect(routeParams).toEqual({ slug: undefined }) ); }); - it('should catch one param in [[...slug]]', () => { + it('should catch one param in [[...slug]]', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/optional-catch-all/1', ({ routeParams }) => + await run('/optional-catch-all/1').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1'] }) ); - run('/optional-catch-all/1/', ({ routeParams }) => + await run('/optional-catch-all/1/').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1'] }) ); }); - it('should catch files near slug [[...slug]] firstly', () => { + it('should catch files near slug [[...slug]] firstly', async () => { const mappingTestCases = [ { fromUrl: '/optional-catch-all/with-other-files/test', @@ -323,56 +360,63 @@ describe('RequestHandler', () => { ]; const run = createTestRequestRunner(dynamicSegmentsHandler); - mappingTestCases.forEach(({ fromUrl, toFile }) => { - run(fromUrl, ({ filePath }) => expect(filePath).toBe(toFile)); - run(`${fromUrl}/`, ({ filePath }) => expect(filePath).toBe(toFile)); - }); + await Promise.all( + mappingTestCases.map(async ({ fromUrl, toFile }) => { + await run(fromUrl).then(({ filePath }) => + expect(filePath).toBe(toFile) + ); + await run(`${fromUrl}/`).then(({ filePath }) => + expect(filePath).toBe(toFile) + ); + }) + ); }); - it('should catch several params in [[...slug]]', () => { + it('should catch several params in [[...slug]]', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/optional-catch-all/1/2/3', ({ routeParams }) => + await run('/optional-catch-all/1/2/3').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1', '2', '3'] }) ); - run('/optional-catch-all/1/2/3/', ({ routeParams }) => + await run('/optional-catch-all/1/2/3/').then(({ routeParams }) => expect(routeParams).toEqual({ slug: ['1', '2', '3'] }) ); }); - it('should catch all params during combination of dynamic segments', () => { + it('should catch all params during combination of dynamic segments', async () => { const run = createTestRequestRunner(dynamicSegmentsHandler); - run('/combination/1/sub/1/2', ({ routeParams, filePath }) => { + await run('/combination/1/sub/1/2').then(({ routeParams, filePath }) => { expect(filePath).toEqual( '/api-dynamic-segments/combination/[id]/sub/[...ids].ts' ); expect(routeParams).toEqual({ id: '1', ids: ['1', '2'] }); }); - run('/combination/1/sub/sub-sub/4/5/6', ({ routeParams, filePath }) => { - expect(filePath).toEqual( - '/api-dynamic-segments/combination/[id]/sub/sub-sub/[[...ids]].ts' - ); - expect(routeParams).toEqual({ id: '1', ids: ['4', '5', '6'] }); - }); - }); - - it('should process correctly for catch all segment before other types', () => { - const run = createTestRequestRunner(dynamicSegmentsHandler); - - run( - '/combination/one/two/three/plain/image.jpg', + await run('/combination/1/sub/sub-sub/4/5/6').then( ({ routeParams, filePath }) => { expect(filePath).toEqual( - '/api-dynamic-segments/combination/[...operations]/plain/[slug].ts' + '/api-dynamic-segments/combination/[id]/sub/sub-sub/[[...ids]].ts' ); - expect(routeParams).toEqual({ - operations: ['one', 'two', 'three'], - slug: 'image.jpg' - }); + expect(routeParams).toEqual({ id: '1', ids: ['4', '5', '6'] }); } ); }); + + it('should process correctly for catch all segment before other types', async () => { + const run = createTestRequestRunner(dynamicSegmentsHandler); + + const { routeParams, filePath } = await run( + '/combination/one/two/three/plain/image.jpg' + ); + + expect(filePath).toEqual( + '/api-dynamic-segments/combination/[...operations]/plain/[slug].ts' + ); + expect(routeParams).toEqual({ + operations: ['one', 'two', 'three'], + slug: 'image.jpg' + }); + }); }); describe('#Validations', () => { @@ -434,10 +478,8 @@ describe('RequestHandler', () => { const { marks } = await runForMiddleware('/nested/123'); expect(marks).toEqual(['before:m-root', '[id]', 'after:m-root']); - - runForRoute('/nested/123', ({ routeParams }) => - expect(routeParams).toEqual({ id: '123' }) - ); + const { routeParams } = await runForRoute('/nested/123'); + expect(routeParams).toEqual({ id: '123' }); }); it('should run the list middleware', async () => { diff --git a/tests/test-utils.ts b/tests/test-utils.ts index 3a796a2..11587bf 100644 --- a/tests/test-utils.ts +++ b/tests/test-utils.ts @@ -1,16 +1,16 @@ -import { initFileRouter } from '../src/file-router'; +import { initFileRouter } from '../src'; import { expect } from 'vitest'; -import type { FileRouterRequestHandler } from '../src/file-router'; +import type { FileRouterRequestHandler } from '../src'; import type { IncomingMessage } from 'node:http'; import type { OutgoingMessage } from 'http'; import type { Dictionary } from '../src/types/dictionary'; -type SuccessCallback = (props: { +interface RequestResult { req: IncomingMessage; filePath: string; - routeParams: Dictionary; -}) => void; + routeParams?: Dictionary; +} interface MiddlewareResult { req?: IncomingMessage; @@ -47,24 +47,23 @@ export function createTestRequestHandler( export function createTestRequestRunner( requestHandler: FileRouterRequestHandler ) { - return (url: string, onSuccess: SuccessCallback) => { - requestHandler( - { url, headers: { host: 'site' } }, - { end: onSuccess, registerCall: () => {} }, - [] - ); - }; -} - -export function createTestMethodsRequestRunner( - requestHandler: FileRouterRequestHandler -) { - return (url: string, method: string, onSuccess: SuccessCallback) => { - requestHandler( + return async (url: string, method?: string) => { + let result: R | undefined; + await requestHandler( { url, headers: { host: 'site' }, method }, - { end: onSuccess }, + { + end: (data: R) => { + result = data; + }, + registerCall: () => {} + }, [] ); + if (!result) { + throw new Error('Request handler did not return a result'); + } + + return result; }; }