Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion spec/bugsplat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,32 @@ describe('BugSplat', function () {
bugsplat = new BugSplat(database, appName, appVersion);
// @ts-expect-error -- accessing private field for test mocking
bugsplat._formData = () => fakeFormData;
fetchSpy = vi.spyOn(globalThis, 'fetch') as unknown as Mock;
fetchSpy = vi.fn();
// @ts-expect-error -- accessing private field for test mocking
bugsplat._fetch = fetchSpy;
});

describe('post', () => {
it('should use injectable _fetch instead of globalThis.fetch', async () => {
const customFetch = vi.fn().mockResolvedValue(fakeSuccessResponseBody);
const globalFetchSpy = vi.fn().mockResolvedValue(fakeSuccessResponseBody);
vi.stubGlobal('fetch', globalFetchSpy);
// @ts-expect-error -- accessing private field for test
bugsplat._fetch = customFetch;

await bugsplat.post(new Error('BugSplat!'));

expect(customFetch).toHaveBeenCalledOnce();
Comment thread
bobbyg603 marked this conversation as resolved.
expect(customFetch).toHaveBeenCalledWith(
`https://${database}.bugsplat.com/post/js/`,
expect.objectContaining({
method: 'POST',
body: fakeFormData,
})
);
expect(globalFetchSpy).not.toHaveBeenCalled();
});

it('should call fetch url containing database', async () => {
fetchSpy.mockResolvedValue(fakeSuccessResponseBody);

Expand Down
5 changes: 3 additions & 2 deletions src/bugsplat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function appendAttachment(body: FormData, attachment: BugSplatAttachment)
*/
export class BugSplat {
private _formData = () => new FormData();
private _fetch: typeof globalThis.fetch = globalThis.fetch.bind(globalThis);

Comment thread
bobbyg603 marked this conversation as resolved.
private _appKey = '';
private _attributes: Record<string, string> = {};
Expand Down Expand Up @@ -129,7 +130,7 @@ export class BugSplat {
console.log('BugSplat Error:', errorToPost);
console.log('BugSplat Url:', url);

const response = await globalThis.fetch(url, { method: 'POST', body });
const response = await this._fetch(url, { method: 'POST', body });
const json = await tryParseResponseJson(response);

console.log('BugSplat POST status code:', response.status);
Expand Down Expand Up @@ -208,7 +209,7 @@ export class BugSplat {

console.log('BugSplat Feedback:', title);

const response = await globalThis.fetch(baseUrl, { method: 'POST', body });
const response = await this._fetch(baseUrl, { method: 'POST', body });
const json = await tryParseResponseJson(response);

if (!response.ok) {
Expand Down
Loading