-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: enhance response matchers with utility functions and type definitions #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,16 +1,12 @@ | ||||||
| import type { MatcherResult } from "./matcher"; | ||||||
| import { verifyResponse } from "#src/utils.ts"; | ||||||
| import type { MatcherResult } from "./types"; | ||||||
|
|
||||||
| export function toHaveBody(response: Response): MatcherResult { | ||||||
| if (!(response instanceof Response)) { | ||||||
| return { | ||||||
| message: () => `Expected a Response, but received ${typeof response}`, | ||||||
| pass: false, | ||||||
| }; | ||||||
| } | ||||||
| verifyResponse(response); | ||||||
|
|
||||||
| return { | ||||||
| message: () => `Expected response to have body`, | ||||||
| pass: response.body !== null, | ||||||
| pass: typeof response.body !== "undefined", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check The correct way to check for the presence of a body stream is to check if it's not
Suggested change
|
||||||
| }; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -24,7 +20,7 @@ if (import.meta.vitest) { | |||||
| expect(response).toHaveBody(); | ||||||
| }); | ||||||
|
|
||||||
| it.fails("fails when body is null", () => { | ||||||
| it("allows body to be null", () => { | ||||||
| let response = new Response(null); | ||||||
| expect(response).toHaveBody(); | ||||||
| }); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||
| import type { MatcherResult } from "./matcher"; | ||||||||||||||||||
| import type { MatcherResult } from "./types"; | ||||||||||||||||||
|
|
||||||||||||||||||
| export function toThrowResponse( | ||||||||||||||||||
| received: () => Response, | ||||||||||||||||||
|
|
@@ -8,7 +8,7 @@ export function toThrowResponse( | |||||||||||||||||
|
|
||||||||||||||||||
| try { | ||||||||||||||||||
| received(); | ||||||||||||||||||
| } catch (e: unknown) { | ||||||||||||||||||
| } catch (e) { | ||||||||||||||||||
| error = e; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -27,7 +27,6 @@ export function toThrowResponse( | |||||||||||||||||
| return { | ||||||||||||||||||
| message: () => `Expected to throw a Response`, | ||||||||||||||||||
| pass: | ||||||||||||||||||
| error instanceof Response && | ||||||||||||||||||
| error.status === expectedResponse.status && | ||||||||||||||||||
| error.statusText === expectedResponse.statusText, | ||||||||||||||||||
|
Comment on lines
29
to
31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the
The
Suggested change
|
||||||||||||||||||
| actual: { status: error.status, statusText: error.statusText }, | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,11 @@ | ||||||||||||||||||||||||||||||
| export function getHeaders(response: Response | { headers?: HeadersInit }) { | ||||||||||||||||||||||||||||||
| if (response.headers instanceof Headers) return response.headers; | ||||||||||||||||||||||||||||||
| return new Headers(response.headers); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export function verifyResponse(response: unknown) { | ||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||
| message: () => `Expected a Response, but received ${typeof response}`, | ||||||||||||||||||||||||||||||
| pass: !(response instanceof Response), | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The A better approach would be for this function to return a
Suggested change
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to
verifyResponse(response)is incorrect because its return value is ignored. This effectively removes the runtime type check that was present in the previous version of the code.Assuming
verifyResponsefunction returns aMatcherResulton failure andundefinedon success, it should be used like this to properly handle the validation:This pattern should be applied to all other matchers where this refactoring was performed.