Skip to content
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

feat: allow more types of overloads #26

Merged
merged 2 commits into from
Oct 28, 2024
Merged

feat: allow more types of overloads #26

merged 2 commits into from
Oct 28, 2024

Conversation

dirkluijk
Copy link
Collaborator

@dirkluijk dirkluijk commented Oct 26, 2024

This PR makes it possible to use more types of overloaded functions:

fetch.mockResponseOnce('x');
fetch.mockResponseOnce({ body: 'x' }); // previously not allowed
fetch.mockResponseOnce(new Response('x'));  // previously not allowed

fetch.mockResponseOnce(() => 'x');
fetch.mockResponseOnce(() => ({ body: 'x' }));
fetch.mockResponseOnce(() => new Response('x'));

fetch.mockResponseOnce(() => Promise.resolve('x'));
fetch.mockResponseOnce(() => Promise.resolve({ body: 'x' }));
fetch.mockResponseOnce(() => Promise.resolve(new Response('x')));

This could make certain cases less awkward, like #25 (comment):

fetch.mockResponseOnce({ status: 204 });
fetch.mockResponseOnce(new Response(null, { status: 204 }));

"eslint": "^8.56.0",
"jsdom": "^24.1.0",
"prettier": "^3.3.2",
"typescript": "~5.5.0",
"vitest": "2.0.2"
"vitest": "^2.1.3"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

bumped the devDependency in order to allow checking coverage

@dirkluijk dirkluijk requested a review from IanVS October 28, 2024 09:03
@IanVS
Copy link
Owner

IanVS commented Oct 28, 2024

Hmmmm, I'm not sure if I love the idea of even more ways to accomplish the same thing, I think this project already has too many overloads already. I might even suggest that we move towards a future where a Response needs to be used, either on its own, or in a function that returns a Response or the promise of a Response. It's a bit more annoying to use that way, but it's easier to document and learn, and there's no ambiguity of what's going on behind the scenes.

But I see that today, the only time a Response can be provided is when returned in a function. So this PR would be a necessary step anyway. I think this makes sense to do for now, and hopefully we can find a way to deprecate simple strings and objects being passed to mockResponse-style methods.

Do you think there are any docs that should be changed if we merge this feature?

@dirkluijk
Copy link
Collaborator Author

dirkluijk commented Oct 28, 2024

Hmmmm, I'm not sure if I love the idea of even more ways to accomplish the same thing, I think this project already has too many overloads already. I might even suggest that we move towards a future where a Response needs to be used, either on its own, or in a function that returns a Response or the promise of a Response. It's a bit more annoying to use that way, but it's easier to document and learn, and there's no ambiguity of what's going on behind the scenes.

Agreed. I'd say we could narrow it down by limiting the current definition:

type ResponseProvider = ResponseLike | ((request: Request) => ResponseLike | Promise<ResponseLike>);
type ResponseLike = MockResponse | ResponseBody | Response;
type ResponseBody = string;

down to:

type ResponseProvider = ResponseLike | ((request: Request) => ResponseLike | Promise<ResponseLike>);
type ResponseLike = ResponseBody | Response;
type ResponseBody = string;

or even

type ResponseProvider = Response | ((request: Request) => Response | Promise<Response>);

although I see the added value of keeping the response body overload.

Do you think there are any docs that should be changed if we merge this feature?

It's a superset of the current API, so I wouldn't mind just keeping it as-is. But I am also in favor of rewriting the whole docs for the 1.0 version, when we introduce breaking changes. My ideas for 1.0 would be:

  • deprecate (or remove) aliases
  • simplify RequestProvider
  • remove global fetchMock instance
  • rename enable / disable to install / uninstall
  • add option to autoEnable

@IanVS IanVS merged commit 46bbb06 into main Oct 28, 2024
3 checks passed
@IanVS IanVS deleted the feat-flexible-overloads branch October 28, 2024 13:27
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.

2 participants