Skip to content

Commit eba8ed8

Browse files
committed
support regex in body
1 parent 458f0be commit eba8ed8

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

deno.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export const makeFetch = (h: HandlerOrListener): FetchFunction => {
133133
assertEquals(
134134
header,
135135
b.join(','),
136-
`expected header ${a} to match regexp ${b}, got ${header}`,
136+
`expected header ${a} to match ${b.join(', ')}, got ${header}`,
137137
)
138138
} else {
139139
assertEquals(
@@ -152,7 +152,13 @@ export const makeFetch = (h: HandlerOrListener): FetchFunction => {
152152
}
153153
}
154154
const expectBody = (a: unknown) => {
155-
assertEquals(data, a, `Expected to have body ${a}, got ${data}`)
155+
if (a instanceof RegExp) {
156+
assertMatch(
157+
data as string,
158+
a,
159+
`Expected body to match regexp ${a}, got ${data}`,
160+
)
161+
} else assertEquals(data, a, `Expected to have body ${a}, got ${data}`)
156162
}
157163

158164
// deno-lint-ignore no-explicit-any

mod_test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ describe('expectHeader', () => {
184184
try {
185185
res.expectHeader('Content-Type', /image/)
186186
} catch (e) {
187-
expect((e as Error).message).toMatch(
188-
new RegExp(
189-
'Expected actual: "text/html" to match: "/image/": expected header Content-Type to match regexp /image/, got text/html',
190-
),
187+
expect((e as Error).message).toEqual(
188+
'Expected actual: "text/html" to match: "/image/": expected header Content-Type to match regexp /image/, got text/html',
191189
)
192190
}
193191
})
@@ -258,6 +256,12 @@ describe('expectBody', () => {
258256
expect(e).toBeDefined()
259257
}
260258
})
259+
it('supports regex', async () => {
260+
const handler: Handler = () => new Response('Hello World')
261+
const fetch = makeFetch(handler)
262+
const res = await fetch('/')
263+
res.expectBody(new RegExp('Hello'))
264+
})
261265
})
262266

263267
describe('expect', () => {

0 commit comments

Comments
 (0)