Skip to content

Commit 9772c2e

Browse files
committed
Readability improvements for events tests
- Additional clarifying comments - More descriptive variable names for mock events array values.
1 parent 7c06436 commit 9772c2e

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

glean/tests/unit/core/metrics/events_database.spec.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,20 @@ describe("EventsDatabase", function() {
394394
prevTime = e.timestamp;
395395
}
396396

397-
// Make sure the found events are the expected events.
397+
// Make sure the found events are the expected events. This array consists of
398+
// a user created event followed by a restarted event and repeats.
398399
for (let i = 0; i < 10; i++) {
399-
assert.strictEqual("test", (snapshot[i * 2] as JSONObject)["category"]);
400-
assert.strictEqual(`stichting_test_${i}`, (snapshot[i * 2] as JSONObject)["name"]);
400+
const userEvent = snapshot[i * 2] as JSONObject;
401+
const restartedEvent = snapshot[(i * 2) + 1] as JSONObject;
402+
403+
assert.strictEqual("test", userEvent["category"]);
404+
assert.strictEqual(`stichting_test_${i}`, userEvent["name"]);
401405

402406
// We no longer keep trailing restarted events, so in this scenario, we need to ignore
403407
// the final element of the snapshot since it previously had a restarted event.
404-
if (snapshot[(i * 2) + 1]) {
405-
assert.strictEqual("glean", (snapshot[(i * 2) + 1] as JSONObject)["category"]);
406-
assert.strictEqual("restarted", (snapshot[(i * 2) + 1] as JSONObject)["name"]);
408+
if (restartedEvent) {
409+
assert.strictEqual("glean", restartedEvent["category"]);
410+
assert.strictEqual("restarted", restartedEvent["name"]);
407411
}
408412

409413
// Check that no errors were recorded for the `glean.restarted` metric.
@@ -454,16 +458,20 @@ describe("EventsDatabase", function() {
454458
prevTime = e.timestamp;
455459
}
456460

457-
// Make sure the found events are the expected events.
461+
// Make sure the found events are the expected events. This array consists of
462+
// a user created event followed by a restarted event and repeats.
458463
for (let i = 0; i < 10; i++) {
459-
assert.strictEqual("test", (snapshot[i * 2] as JSONObject)["category"]);
460-
assert.strictEqual(`time_travel_${i}`, (snapshot[i * 2] as JSONObject)["name"]);
464+
const userEvent = snapshot[i * 2] as JSONObject;
465+
const restartedEvent = snapshot[(i * 2) + 1] as JSONObject;
466+
467+
assert.strictEqual("test", userEvent["category"]);
468+
assert.strictEqual(`time_travel_${i}`, userEvent["name"]);
461469

462470
// We no longer keep trailing restarted events, so in this scenario, we need to ignore
463471
// the final element of the snapshot since it previously had a restarted event.
464-
if (snapshot[(i * 2) + 1]) {
465-
assert.strictEqual("glean", (snapshot[(i * 2) + 1] as JSONObject)["category"]);
466-
assert.strictEqual("restarted", (snapshot[(i * 2) + 1] as JSONObject)["name"]);
472+
if (restartedEvent) {
473+
assert.strictEqual("glean", restartedEvent["category"]);
474+
assert.strictEqual("restarted", restartedEvent["name"]);
467475
}
468476
}
469477

@@ -513,16 +521,20 @@ describe("EventsDatabase", function() {
513521
prevTime = e.timestamp;
514522
}
515523

516-
// Make sure the found events are the expected events.
524+
// Make sure the found events are the expected events. This array consists of
525+
// a user created event followed by a restarted event and repeats.
517526
for (let i = 0; i < 10; i++) {
518-
assert.strictEqual("test", (snapshot[i * 2] as JSONObject)["category"]);
519-
assert.strictEqual(`time_travel_${i}`, (snapshot[i * 2] as JSONObject)["name"]);
527+
const userEvent = snapshot[i * 2] as JSONObject;
528+
const restartedEvent = snapshot[(i * 2) + 1] as JSONObject;
529+
530+
assert.strictEqual("test", userEvent["category"]);
531+
assert.strictEqual(`time_travel_${i}`, userEvent["name"]);
520532

521533
// We no longer keep trailing restarted events, so in this scenario, we need to ignore
522534
// the final element of the snapshot since it previously had a restarted event.
523-
if (snapshot[(i * 2) + 1]) {
524-
assert.strictEqual("glean", (snapshot[(i * 2) + 1] as JSONObject)["category"]);
525-
assert.strictEqual("restarted", (snapshot[(i * 2) + 1] as JSONObject)["name"]);
535+
if (restartedEvent) {
536+
assert.strictEqual("glean", restartedEvent["category"]);
537+
assert.strictEqual("restarted", restartedEvent["name"]);
526538
}
527539
}
528540

glean/tests/unit/core/pings/maker.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,12 @@ describe("PingMaker", function() {
291291

292292
const triggerCustomEvent = async (event: EventMetricType<ExtraMap>) => {
293293
event.record();
294+
// Wait for recording action to complete
294295
await event.testGetValue();
295296
};
296297

297298
const triggerRestartedEvent = async () => {
299+
// Easiest way to simulate a restart and create a `glean.restarted` event
298300
await testResetGlean(testAppId, true, undefined, false);
299301
};
300302

0 commit comments

Comments
 (0)