Skip to content

Commit

Permalink
#8312 Fix e2e error logging test for MV2 (#8332)
Browse files Browse the repository at this point in the history
* rewrite test to be reusable in mv2

* fix strict null errors

* extract expected request body logic

* add mv2 logic for test

* fix type error
  • Loading branch information
mnholtz authored Apr 24, 2024
1 parent 3570e60 commit 142a15e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
71 changes: 40 additions & 31 deletions end-to-end-tests/tests/telemetry/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { test, expect } from "../../fixtures/extensionBase";
import { type Request } from "playwright-core";
// @ts-expect-error -- https://youtrack.jetbrains.com/issue/AQUA-711/Provide-a-run-configuration-for-Playwright-tests-in-specs-with-fixture-imports-only
import { type Page, test as base } from "@playwright/test";
import { getBaseExtensionConsoleUrl } from "../../pageObjects/constants";
import { MV } from "../../env";

// TODO: Fix this test for MV2
test.skip(
MV === "2",
"Temporarily skipping this test due to inconsistencies with error display in manifest versions",
);
test("can report application error to telemetry service", async ({
page,
context,
Expand All @@ -17,7 +13,7 @@ test("can report application error to telemetry service", async ({
const errorServiceEndpoint = "https://browser-intake-datadoghq.com/api/v2/*";

await context.route(
"https://app.pixiebrix.com/api/registry/bricks/",
"https://app.pixiebrix.com/api/extensions/",
async (route) => {
await route.fulfill({
status: 200,
Expand All @@ -27,38 +23,51 @@ test("can report application error to telemetry service", async ({
},
);

await context.route(errorServiceEndpoint, async (route) => {
await route.fulfill({
await context.route(errorServiceEndpoint, async (route) =>
route.fulfill({
status: 202,
});
});
}),
);

await page.goto(getBaseExtensionConsoleUrl(extensionId));
await expect(page.getByText("An error occurred")).toBeVisible();
await expect(page.getByText("Something went wrong.")).toBeVisible();

// Due to service worker limitations with the Datadog SDK, we need to report errors via an offscreen document
// (see https://github.com/pixiebrix/pixiebrix-extension/issues/8268). The offscreen document is created when
// the first error is reported, so we need to wait for it to be created before we can interact with it.
let offscreenPage: Page;
await expect(async () => {
offscreenPage = context
.pages()
.find((value) =>
value
.url()
.startsWith(`chrome-extension://${extensionId}/offscreen.html`),
);
let waitForRequest: Promise<Request> | undefined;
if (MV === "3") {
// Due to service worker limitations with the Datadog SDK, we need to report errors via an offscreen document
// (see https://github.com/pixiebrix/pixiebrix-extension/issues/8268). The offscreen document is created when
// the first error is reported, so we need to wait for it to be created before we can interact with it.
let offscreenPage: Page | undefined;
await expect(async () => {
offscreenPage = context
.pages()
.find((value) =>
value
.url()
.startsWith(`chrome-extension://${extensionId}/offscreen.html`),
);

expect(offscreenPage?.url()).toBeDefined();
}).toPass({ timeout: 5000 });
expect(offscreenPage?.url()).toBeDefined();
}).toPass({ timeout: 5000 });
waitForRequest = offscreenPage?.waitForRequest(errorServiceEndpoint);
} else {
const backgroundPage = context.backgroundPages()[0];
waitForRequest = backgroundPage?.waitForRequest(errorServiceEndpoint);
}

// TODO: due to Datadog SDK implementation, it will take ~30 seconds for the
// request to be sent. We should figure out a way to induce the request to be sent sooner.
const request = await offscreenPage.waitForRequest(errorServiceEndpoint);
const request = await waitForRequest;
const errorLogsJson = request
?.postData()
?.split("\n")
.map((log) => JSON.parse(log));

expect(request.postDataJSON()).toMatchObject({
service: "pixiebrix-browser-extension",
manifestVersion: Number(MV),
error: expect.anything(),
});
expect(errorLogsJson).toContainEqual(
expect.objectContaining({
service: "pixiebrix-browser-extension",
manifestVersion: Number(MV),
error: expect.anything(),
}),
);
});
1 change: 1 addition & 0 deletions src/tsconfig.strictNullChecks.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"../end-to-end-tests/pageObjects/extensionConsole/workshopPage.ts",
"../end-to-end-tests/tests/extensionConsoleActivation.spec.ts",
"../end-to-end-tests/tests/smoke/modsPage.spec.ts",
"../end-to-end-tests/tests/telemetry/errors.spec.ts",
"./__mocks__/@/auth/featureFlagStorage.ts",
"./__mocks__/@/auth/useLinkState.ts",
"./__mocks__/@/background/messenger/strict/api.ts",
Expand Down

0 comments on commit 142a15e

Please sign in to comment.