-
Notifications
You must be signed in to change notification settings - Fork 81
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
Defer resolving globalThis.fetch until calling endpoint #692
Conversation
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.
Good call, but left a couple of comments. You have to run make bench
to update the code size benchmark too.
I ran |
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.
For manipulating globalThis.fetch in the tests, it looks like it would be useful to use a setup and teardown, so that a test failure doesn't affect all other tests:
let originFetch: typeof fetch;
beforeEach(() => (originFetch = globalThis.fetch));
afterEach(() => (globalThis.fetch = originFetch));
For the gRPC-web test, mocking the response will be difficult, because the transport uses the body as a stream.
Since we do not care which parts of the response are used in this test, you can reject from your fetch
instead of resolving:
const transport = createGrpcWebTransport({
baseUrl: "https://example.com",
});
// Patch globalThis.fetch to mimic a polyfill or patch
globalThis.fetch = () => Promise.reject("test-error-raised-from-patched-fetch");
await expectAsync(
transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
)
).toBeRejectedWithError(/test-error-raised-from-patched-fetch/);
@timostamm Yeah, that makes a lot of sense. I was seeing that weirdness and just started to mess with beforeEach/afterEach but was looking into the GRPC thing first. I was kinda struggling to mock it out so the error idea is super helpful, thanks! |
Also fixed grpc test
When using a polyfilled (or patched) fetch, we need to make sure the fetch is retrieved at call site, rather than being cached earlier. This allows tools like Sentry to properly detect fetch calls.