Skip to content

Commit 3a2e59e

Browse files
committed
Reset sendBeacon and fetch in tests
1 parent 2ca8abe commit 3a2e59e

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

glean/tests/unit/platform/browser/sendbeacon_fallback_uploader.spec.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,26 @@ const MOCK_ENDPOINT = "http://www.example.com";
1818

1919
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2020
// @ts-ignore
21-
global.navigator.sendBeacon = (url: string, content: string): boolean => {
22-
void fetch(url, {
23-
body: content,
24-
method: "POST",
25-
});
21+
// eslint-disable-next-line jsdoc/require-jsdoc
22+
function setGlobalSendBeacon() {
23+
global.navigator.sendBeacon = (url: string, content: string): boolean => {
24+
void fetch(url, {
25+
body: content,
26+
method: "POST",
27+
});
2628

27-
return true;
28-
};
29+
return true;
30+
};
31+
}
2932
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3033
// @ts-ignore
3134
global.fetch = fetch;
3235

3336
describe("Uploader/BrowserSendBeaconFallback", function () {
37+
beforeEach(function() {
38+
setGlobalSendBeacon();
39+
});
40+
3441
afterEach(function () {
3542
sandbox.restore();
3643
});
@@ -57,7 +64,14 @@ describe("Uploader/BrowserSendBeaconFallback", function () {
5764
const TEST_PING_CONTENT = {"my-test-value": 40721};
5865
nock(MOCK_ENDPOINT).post(/./i).reply(200);
5966

67+
// Reset `fetch` to a known state.
68+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
69+
// @ts-ignore
70+
global.fetch = fetch;
71+
72+
// Ensure `sendBeacon` fails.
6073
global.navigator.sendBeacon = () => false;
74+
6175
const response = BrowserSendBeaconFallbackUploader.post(MOCK_ENDPOINT, new PingRequest("abc", {}, JSON.stringify(TEST_PING_CONTENT), 1024));
6276
const expectedResponse = new UploadResult(UploadResultStatus.Success, 200);
6377
assert.deepStrictEqual(
@@ -72,7 +86,14 @@ describe("Uploader/BrowserSendBeaconFallback", function () {
7286
code: "AWFUL_ERROR",
7387
});
7488

89+
// Reset `fetch` to a known state.
90+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
91+
// @ts-ignore
92+
global.fetch = fetch;
93+
94+
// Ensure `sendBeacon` fails.
7595
global.navigator.sendBeacon = () => false;
96+
7697
const response = BrowserSendBeaconFallbackUploader.post(MOCK_ENDPOINT, new PingRequest("abc", {}, "{}", 1024));
7798
const expectedResponse = new UploadResult(UploadResultStatus.RecoverableFailure);
7899
assert.deepStrictEqual(

glean/tests/unit/platform/browser/sendbeacon_uploader.spec.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ const MOCK_ENDPOINT = "http://www.example.com";
1818

1919
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2020
// @ts-ignore
21-
global.navigator.sendBeacon = (url: string, content: string): boolean => {
22-
void fetch(url, {
23-
body: content,
24-
method: "POST",
25-
});
21+
// eslint-disable-next-line jsdoc/require-jsdoc
22+
function setGlobalSendBeacon() {
23+
global.navigator.sendBeacon = (url: string, content: string): boolean => {
24+
void fetch(url, {
25+
body: content,
26+
method: "POST",
27+
});
2628

27-
return true;
28-
};
29+
return true;
30+
};
31+
}
2932

3033
describe("Uploader/BrowserSendBeacon", function () {
34+
beforeEach(function() {
35+
setGlobalSendBeacon();
36+
});
37+
3138
afterEach(function () {
3239
sandbox.restore();
3340
});

0 commit comments

Comments
 (0)