Skip to content

Commit e8dbcd2

Browse files
committed
test: refactor PreparedRegExpRouter test
1 parent befcce4 commit e8dbcd2

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/router/reg-exp-router/router.test.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { writeFile } from 'fs/promises'
2+
import { tmpdir } from 'os'
13
import { UnsupportedPathError } from '../../router'
24
import { PreparedRegExpRouter, buildInitParams, serializeInitParams } from './prepared-router'
35
import { RegExpRouter } from './router'
@@ -658,26 +660,29 @@ describe('Capture Group', () => {
658660
})
659661
})
660662

661-
describe('PreparedRegExpRouter', () => {
662-
const serialized = serializeInitParams(buildInitParams({
663-
routes: [
664-
{method: 'ALL', path: '*'},
665-
{method: 'ALL', path: '/posts/:id/*'},
666-
{method: 'GET', path: '*'},
667-
{method: 'GET', path: '/'},
668-
{method: 'GET', path: '/static'},
669-
{method: 'GET', path: '/posts/:id/*'},
670-
{method: 'GET', path: '/posts/:id'},
671-
{method: 'GET', path: '/posts/:id/comments'},
672-
{method: 'POST', path: '/posts'},
673-
{method: 'PUT', path: '/posts/:id'},
674-
],
675-
}))
676-
677-
let params: ConstructorParameters<typeof PreparedRegExpRouter<string>>
678-
// eslint-disable-next-line prefer-const
679-
params = [{}, {}]
680-
eval(`params = ${serialized}`)
663+
describe('PreparedRegExpRouter', async () => {
664+
const serialized = serializeInitParams(
665+
buildInitParams({
666+
routes: [
667+
{ method: 'ALL', path: '*' },
668+
{ method: 'ALL', path: '/posts/:id/*' },
669+
{ method: 'GET', path: '*' },
670+
{ method: 'GET', path: '/' },
671+
{ method: 'GET', path: '/static' },
672+
{ method: 'GET', path: '/posts/:id/*' },
673+
{ method: 'GET', path: '/posts/:id' },
674+
{ method: 'GET', path: '/posts/:id/comments' },
675+
{ method: 'POST', path: '/posts' },
676+
{ method: 'PUT', path: '/posts/:id' },
677+
],
678+
})
679+
)
680+
681+
const path = `${tmpdir()}/params.ts`
682+
await writeFile(path, `const params = ${serialized}; export default params`)
683+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
684+
// @ts-ignore
685+
const params: ConstructorParameters<typeof PreparedRegExpRouter<string>> = (await import(path)).default
681686
const router = new PreparedRegExpRouter<string>(...params)
682687

683688
router.add('ALL', '*', 'wildcard')

0 commit comments

Comments
 (0)