Skip to content

Conversation

mcansh
Copy link
Owner

@mcansh mcansh commented Jul 31, 2025

Signed-off-by: GitHub [email protected]

Copy link

changeset-bot bot commented Jul 31, 2025

⚠️ No Changeset found

Latest commit: 91f9ea9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @mcansh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the vitest-response-matchers library by introducing shared utility functions to streamline common validation and data extraction tasks. These changes lead to a more robust and type-safe codebase, while also refining the behavior of specific matchers to be more accurate and flexible in their assertions.

Highlights

  • Refactoring & Utility Functions: Introduced two new utility functions, getHeaders and verifyResponse, to centralize common logic such as extracting headers from response objects and validating if an input is an instance of Response. These utilities are now integrated across various matchers, significantly reducing code duplication and improving maintainability.
  • Type Definition Enhancements: Updated the type definition for the toThrowResponse matcher in client.ts to be more precise, now accepting Response | { status: number; statusText: string }. This provides clearer expectations for the structure of thrown response errors.
  • Matcher Behavior Refinements: Adjusted the toHaveBody matcher to consider null as a valid body, changing its internal logic from response.body !== null to typeof response.body !== "undefined". The toHaveCookies matcher also received enhancements for more robust handling of missing Set-Cookie headers and improved logic for cases where no cookies are expected.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
packages 91f9ea9 Commit Preview URL

Branch Preview URL
Jul 31 2025, 06:52 PM

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances response matchers with utility functions and type definitions. However, the changes introduce potential regressions, particularly in the verifyResponse utility function and its usage, as well as logic errors in toHaveBody and toThrowResponse. Addressing these issues is crucial to ensure the matchers function correctly.


return {
message: () => `Expected response to have body`,
pass: response.body !== null,
pass: typeof response.body !== "undefined",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The check typeof response.body !== "undefined" is not the correct way to determine if a Response has a body. The body property on a Response object is either a ReadableStream or null, but it's never undefined. This means the current check will always evaluate to true.

The correct way to check for the presence of a body stream is to check if it's not null.

Suggested change
pass: typeof response.body !== "undefined",
pass: response.body !== null,

pass: false,
};
}
verifyResponse(response);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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 verifyResponse function returns a MatcherResult on failure and undefined on success, it should be used like this to properly handle the validation:

const verification = verifyResponse(response);
if (verification) {
  return verification;
}

This pattern should be applied to all other matchers where this refactoring was performed.

Comment on lines 29 to 31
pass:
error instanceof Response &&
error.status === expectedResponse.status &&
error.statusText === expectedResponse.statusText,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Removing the error instanceof Response check can lead to runtime errors. It introduces two potential problems:

  1. If the received() function does not throw an error, error will be undefined. Accessing error.status will then cause a TypeError.
  2. If a non-Response error is thrown, the checks will be performed on an object that doesn't have status or statusText properties, leading to incorrect results.

The instanceof check is essential for ensuring the thrown object is of the correct type before accessing its properties.

Suggested change
pass:
error instanceof Response &&
error.status === expectedResponse.status &&
error.statusText === expectedResponse.statusText,
pass:
error instanceof Response &&
error.status === expectedResponse.status &&
error.statusText === expectedResponse.statusText,

Comment on lines +6 to +11
export function verifyResponse(response: unknown) {
return {
message: () => `Expected a Response, but received ${typeof response}`,
pass: !(response instanceof Response),
};
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The verifyResponse utility function has a potential flaw:

The pass property is set to !(response instanceof Response). This means if a non-Response object is passed (a failure case), pass becomes true. For Vitest matchers, pass: true indicates a successful assertion, which contradicts the failure message.

A better approach would be for this function to return a MatcherResult object on failure and undefined on success. The calling matcher can then check the return value and fail early if needed.

Suggested change
export function verifyResponse(response: unknown) {
return {
message: () => `Expected a Response, but received ${typeof response}`,
pass: !(response instanceof Response),
};
}
export function verifyResponse(response: unknown): { pass: false; message: () => string } | undefined {
if (!(response instanceof Response)) {
return {
message: () => `Expected a Response, but received ${typeof response}`,
pass: false,
};
}
}

@mcansh mcansh changed the base branch from main to dev August 5, 2025 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant