Skip to content

Commit

Permalink
Add attempt to test id context (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjduffy authored Jan 2, 2024
1 parent 23d3783 commit 025420d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
11 changes: 10 additions & 1 deletion packages/jest/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
removeAnsiCodes,
getMetadataFilePath as getMetadataFilePathBase,
initMetadataFile,
TestIdContext,
} from "@replayio/test-utils";
import type Runtime from "jest-runtime";
import path from "path";
Expand Down Expand Up @@ -62,6 +63,14 @@ const ReplayRunner = async (
};
}

function getTestIdContext(test: Circus.TestEntry): TestIdContext {
const source = getSource(test);
return {
...source,
attempt: test.invocations,
};
}

function getWorkerIndex() {
return +(process.env.JEST_WORKER_ID || 0);
}
Expand All @@ -73,7 +82,7 @@ const ReplayRunner = async (
}

function handleTestStart(test: Circus.TestEntry) {
reporter.onTestBegin(getSource(test), getMetadataFilePath(getWorkerIndex()));
reporter.onTestBegin(getTestIdContext(test), getMetadataFilePath(getWorkerIndex()));
}

function getErrorMessage(errors: any[]) {
Expand Down
15 changes: 13 additions & 2 deletions packages/playwright/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
removeAnsiCodes,
TestMetadataV2,
getMetadataFilePath as getMetadataFilePathBase,
TestIdContext,
} from "@replayio/test-utils";

type UserActionEvent = TestMetadataV2.UserActionEvent;
Expand Down Expand Up @@ -108,6 +109,13 @@ class ReplayPlaywrightReporter implements Reporter {
};
}

getTestIdContext(test: TestCase, testResult: TestResult): TestIdContext {
return {
...this.getSource(test),
attempt: testResult.retry + 1,
};
}

onBegin(config: FullConfig) {
const cfg = this.parseConfig(config);
this.reporter = new ReplayReporter(
Expand All @@ -126,7 +134,10 @@ class ReplayPlaywrightReporter implements Reporter {
}

onTestBegin(test: TestCase, testResult: TestResult) {
this.reporter?.onTestBegin(this.getSource(test), getMetadataFilePath(testResult.workerIndex));
this.reporter?.onTestBegin(
this.getTestIdContext(test, testResult),
getMetadataFilePath(testResult.workerIndex)
);
}

onTestEnd(test: TestCase, result: TestResult) {
Expand Down Expand Up @@ -191,7 +202,7 @@ class ReplayPlaywrightReporter implements Reporter {
tests: [
{
id: 0,
attempt: 1,
attempt: result.retry + 1,
approximateDuration: test.results.reduce((acc, r) => acc + r.duration, 0),
source: this.getSource(test),
result: (status as any) === "interrupted" ? "unknown" : status,
Expand Down
7 changes: 6 additions & 1 deletion packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import ReplayReporter from "./reporter";

export type { TestMetadataV1, TestMetadataV2, ReplayReporterConfig } from "./reporter";
export type {
TestMetadataV1,
TestMetadataV2,
ReplayReporterConfig,
TestIdContext,
} from "./reporter";
export { ReporterError } from "./reporter";
export { pingTestMetrics } from "./metrics";
export { removeAnsiCodes } from "./terminal";
Expand Down
26 changes: 20 additions & 6 deletions packages/test-utils/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ interface TestRunTestInputModel {
recordingIds: string[];
}

export interface TestIdContext {
title: string;
scope: string[];
attempt: number;
}

export interface ReplayReporterConfig {
runTitle?: string;
metadata?: Record<string, any> | string;
Expand Down Expand Up @@ -232,12 +238,12 @@ class ReplayReporter {
return { approximateDuration, resultCounts };
}

getTestId(source?: Test["source"]) {
getTestId(source?: TestIdContext) {
if (!source) {
return this.baseId;
}

return `${this.baseId}-${[...source.scope, source.title].join("-")}`;
return `${this.baseId}-${[...source.scope, source.title].join("-")}-${source.attempt}`;
}

parseConfig(config: ReplayReporterConfig = {}, metadataKey?: string) {
Expand Down Expand Up @@ -516,10 +522,13 @@ class ReplayReporter {
}
}

onTestBegin(source?: Test["source"], metadataFilePath = getMetadataFilePath("REPLAY_TEST", 0)) {
debug("onTestBegin: %o", source);
onTestBegin(
testIdContext?: TestIdContext,
metadataFilePath = getMetadataFilePath("REPLAY_TEST", 0)
) {
debug("onTestBegin: %o", testIdContext);

const id = this.getTestId(source);
const id = this.getTestId(testIdContext);
this.errors = [];
const metadata = {
...(this.baseMetadata || {}),
Expand Down Expand Up @@ -609,7 +618,12 @@ class ReplayReporter {

getRecordingsForTest(tests: Test[], includeUploaded: boolean) {
const filter = `function($v) { $v.metadata.\`x-replay-test\`.id in ${JSON.stringify([
...tests.map(test => this.getTestId(test.source)),
...tests.map(test =>
this.getTestId({
...test.source,
attempt: test.attempt,
})
),
this.getTestId(),
])} and $not($exists($v.metadata.test)) }`;

Expand Down

0 comments on commit 025420d

Please sign in to comment.