Skip to content

Commit

Permalink
fix: include test case location in the report, when available (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
vCaisim authored Jul 16, 2024
1 parent ca33f5a commit f074021
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 78 deletions.
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

0 comments on commit f074021

Please sign in to comment.