Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test case location #3

Merged
merged 1 commit into from
Jul 16, 2024
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
3 changes: 2 additions & 1 deletion examples/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/** @type {import('jest').Config} */
const config = {
testTimeout: 10000,
reporters: [
// "default",
["@currents/jest", {}],
],
projects: [
{
displayName: "spec",
testLocationInResults: true,
testMatch: ["<rootDir>/**/*.spec.ts"],
},
{
displayName: "test",
testLocationInResults: true,
testMatch: ["<rootDir>/**/*.test.ts"],
},
],
Expand Down
17 changes: 17 additions & 0 deletions examples/src/same-title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jest.retryTimes(2);

describe("Test cases with same title", () => {
let j = 0;

test("Test case example", () => {
expect(j++).toBe(2);
});

test.skip("Test case example", () => {
expect(1).toBe(1);
});

test("Test case example", () => {
expect(1).toBe(1);
});
});
167 changes: 90 additions & 77 deletions packages/jest/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type TestCase = {
result: TestCaseResult[];
worker: WorkerInfo;
config: Test["context"]["config"];
location?: {
column?: number;
line?: number;
} | null;
};

type SpecInfo = {
Expand Down Expand Up @@ -212,6 +216,7 @@ export default class CustomReporter implements Reporter {
result: [],
worker: getWorker(),
config: test.context.config,
location: testCaseResult.location,
};
debug(
"Test case execution was skipped [%s]: %o",
Expand Down Expand Up @@ -244,92 +249,100 @@ export default class CustomReporter implements Reporter {
testResult
);

await Promise.all(
testResult.testResults.map(async (testResult) => {
const testId = getTestCaseId(test, testResult);
const testCaseKey = getTestCaseKey(projectId, specName, testId);
if (!this.specInfo[specKey].testCaseList[testCaseKey]) {
this.specInfo[specKey].testCaseList[testCaseKey] = {
id: testId,
timestamps: [],
title: getTestCaseFullTitle(testResult),
result: [testResult],
worker: getWorker(),
config: test.context.config,
};
debug(
"Spec execution completed [%s][%s], adding skipped tests: %o",
specName,
testId,
this.specInfo[specKey].testCaseList[testCaseKey]
);
} else {
await this.resultsDeferred[testCaseKey].promise;
}
})
);
testResult.testResults.forEach(async (testCaseResult) => {
const testId = getTestCaseId(test, testCaseResult);
const testCaseKey = getTestCaseKey(projectId, specName, testId);
if (!this.specInfo[specKey].testCaseList[testCaseKey]) {
this.specInfo[specKey].testCaseList[testCaseKey] = {
id: testId,
timestamps: [],
title: getTestCaseFullTitle(testCaseResult),
result: [testCaseResult],
worker: getWorker(),
config: test.context.config,
location: testCaseResult.location,
};

this.resultsDeferred[testCaseKey] = new Deferred<void>();
this.resultsDeferred[testCaseKey].resolve();

debug(
"Spec execution completed [%s][%s], adding skipped tests: %o",
specName,
testId,
this.specInfo[specKey].testCaseList[testCaseKey]
);
}
});

const startTime = new Date(testResult.perfStats.start).toISOString();
const endTime = new Date(testResult.perfStats.end).toISOString();
const wallClockDuration =
testResult.perfStats.end - testResult.perfStats.start;

const tests = Object.values(this.specInfo[specKey].testCaseList).map(
(testCase) => {
const jestStatus = jestStatusFromInvocations(testCase.result);

return {
_t: testCase.timestamps[0] ?? testResult.perfStats.start,
testId: testCase.id,
title: testCase.title,
state: getTestCaseStatus(jestStatus),
isFlaky: isTestFlaky(testCase.result),
expectedStatus: getExpectedStatus(jestStatus),
timeout: 0,
location: {
column: 1,
file: specName,
line: 1,
},
retries: testCase.result.length + 1,
attempts: testCase.result.map((result, index) => {
const errors = (result.failureMessages ?? []).map((i) =>
getError(
formatError(
testCase.config.rootDir,
new Error(i),
false,
specName
),
testCase.config.rootDir
)
);

return {
_s: getTestCaseStatus(result.status as JestTestCaseStatus),
attempt: getAttemptNumber(result),

workerIndex: testCase.worker.workerIndex,
parallelIndex: testCase.worker.parallelIndex,

startTime:
testCase.timestamps.length && testCase.timestamps[index]
? new Date(testCase.timestamps[index]).toISOString()
: startTime,
steps: [],
const tests = await Promise.all(
Object.values(this.specInfo[specKey].testCaseList).map(
async (testCase) => {
const testCaseKey = getTestCaseKey(projectId, specName, testCase.id);
await this.resultsDeferred[testCaseKey].promise;

duration: testCase.result[index].duration ?? 0,
status: getTestRunnerStatus(result.status as JestTestCaseStatus),
const jestStatus = jestStatusFromInvocations(testCase.result);

return {
_t: testCase.timestamps[0] ?? testResult.perfStats.start,
testId: testCase.id,
title: testCase.title,
state: getTestCaseStatus(jestStatus),
isFlaky: isTestFlaky(testCase.result),
expectedStatus: getExpectedStatus(jestStatus),
timeout: 0,
location: {
column: testCase.location?.column ?? 1,
file: specName,
line: testCase.location?.line ?? 1,
},
retries: testCase.result.length + 1,
attempts: testCase.result.map((result, index) => {
const errors = (result.failureMessages ?? []).map((i) =>
getError(
formatError(
testCase.config.rootDir,
new Error(i),
false,
specName
),
testCase.config.rootDir
)
);

return {
_s: getTestCaseStatus(result.status as JestTestCaseStatus),
attempt: getAttemptNumber(result),

workerIndex: testCase.worker.workerIndex,
parallelIndex: testCase.worker.parallelIndex,

startTime:
testCase.timestamps.length && testCase.timestamps[index]
? new Date(testCase.timestamps[index]).toISOString()
: startTime,
steps: [],

duration: testCase.result[index].duration ?? 0,
status: getTestRunnerStatus(
result.status as JestTestCaseStatus
),

stdout: [],
stderr: result.failureMessages ?? [],
stdout: [],
stderr: result.failureMessages ?? [],

errors,
error: errors[0],
};
}),
};
}
errors,
error: errors[0],
};
}),
};
}
)
);

const flakyCount = tests.filter((t) => t.isFlaky).length;
Expand Down