Skip to content

Commit

Permalink
Merge pull request #517 from hrhino/bug516
Browse files Browse the repository at this point in the history
fix: use duration field of AssertionResult to report test duration
  • Loading branch information
no23reason authored Apr 12, 2022
2 parents 23ad065 + 99d36bf commit 972b7b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
18 changes: 11 additions & 7 deletions src/__tests__/trx-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe("trx-generator", (): void => {
line: 0,
},
failureDetails: [],
duration: 4100,
},
{
ancestorTitles: ["foo's", "bar method"],
Expand All @@ -97,6 +98,7 @@ describe("trx-generator", (): void => {
line: 0,
},
failureDetails: [],
duration: 2900,
},
],
},
Expand All @@ -119,13 +121,13 @@ describe("trx-generator", (): void => {
"Passed",
);
expect(parsed.TestRun.Results[0].UnitTestResult[0].$.duration).toEqual(
"00:00:03.500",
"00:00:04.100",
);
expect(parsed.TestRun.Results[0].UnitTestResult[1].$.outcome).toEqual(
"Failed",
);
expect(parsed.TestRun.Results[0].UnitTestResult[1].$.duration).toEqual(
"00:00:03.500",
"00:00:02.900",
);

done();
Expand Down Expand Up @@ -179,6 +181,7 @@ describe("trx-generator", (): void => {
line: 0,
},
failureDetails: [],
duration: 4100,
},
{
ancestorTitles: ["foo's", "bar method"],
Expand All @@ -192,6 +195,7 @@ describe("trx-generator", (): void => {
line: 0,
},
failureDetails: [],
duration: 2900,
},
],
},
Expand Down Expand Up @@ -237,7 +241,7 @@ describe("trx-generator", (): void => {
testResults: [
{
ancestorTitles: [],
duration: 0,
duration: 181,
failureMessages: [],
fullName: "first",
numPassingAsserts: 0,
Expand Down Expand Up @@ -295,7 +299,7 @@ describe("trx-generator", (): void => {
testResults: [
{
ancestorTitles: [],
duration: 0,
duration: 181,
failureMessages: [],
fullName: "first",
numPassingAsserts: 0,
Expand Down Expand Up @@ -353,7 +357,7 @@ describe("trx-generator", (): void => {
testResults: [
{
ancestorTitles: [],
duration: 0,
duration: 181,
failureMessages: [],
fullName: "first",
numPassingAsserts: 0,
Expand Down Expand Up @@ -481,7 +485,7 @@ describe("trx-generator", (): void => {
testResults: [
{
ancestorTitles: [],
duration: 0,
duration: 181,
failureMessages: [],
fullName: "first",
numPassingAsserts: 0,
Expand Down Expand Up @@ -588,7 +592,7 @@ describe("trx-generator", (): void => {
testResults: [
{
ancestorTitles: [],
duration: 0,
duration: 181,
failureMessages: [],
fullName: "first",
numPassingAsserts: 0,
Expand Down
16 changes: 8 additions & 8 deletions src/trx-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
formatDuration,
getEnvInfo,
getFullTestName,
getSuitePerTestDuration,
getTestClassName,
} from "./utils";

Expand Down Expand Up @@ -148,15 +147,14 @@ const renderTestSuiteResult = (
) => void,
],
): void => {
const perTestDuration = getSuitePerTestDuration(testSuiteResult);
const perTestDurationFormatted = formatDuration(perTestDuration);

if (testSuiteResult.testResults && testSuiteResult.testResults.length) {
testSuiteResult.testResults.forEach((testResult, index) => {
let runningDuration = 0;
testSuiteResult.testResults.forEach((testResult) => {
const testId = uuidv4();
const executionId = uuidv4();
const fullTestName = getFullTestName(testResult);
const filepath = path.relative("./", testSuiteResult.testFilePath);
const duration = testResult.duration || 0;

// UnitTest
const unitTest = testDefinitionsNode
Expand Down Expand Up @@ -185,23 +183,25 @@ const renderTestSuiteResult = (
.att("executionId", executionId)
.att("testName", fullTestName)
.att("computerName", computerName)
.att("duration", perTestDurationFormatted)
.att("duration", formatDuration(duration))
.att(
"startTime",
new Date(
testSuiteResult.perfStats.start + index * perTestDuration,
testSuiteResult.perfStats.start + runningDuration,
).toISOString(),
)
.att(
"endTime",
new Date(
testSuiteResult.perfStats.start + (index + 1) * perTestDuration,
testSuiteResult.perfStats.start + runningDuration,
).toISOString(),
)
.att("testType", testType)
.att("outcome", testOutcomeTable[testResult.status])
.att("testListId", testListNotInListId);

runningDuration += duration;

if (testResult.status === "failed") {
result
.ele("Output")
Expand Down
13 changes: 1 addition & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssertionResult, TestResult } from "@jest/test-result";
import { AssertionResult } from "@jest/test-result";
import * as os from "os";

// Adapted from https://github.com/hatchteam/karma-trx-reporter
Expand Down Expand Up @@ -34,17 +34,6 @@ export const getTestClassName = (testResult: AssertionResult): string =>
? testResult.ancestorTitles[0]
: "No suite";

export const getSuitePerTestDuration = (testSuiteResult: TestResult): number =>
// take the total duration of suite and divide it by the number of tests
// (Jest does not provide per test performance info)
Math.floor(
(testSuiteResult.perfStats.end - testSuiteResult.perfStats.start) /
(testSuiteResult.numPassingTests +
testSuiteResult.numFailingTests +
testSuiteResult.numPendingTests +
testSuiteResult.numTodoTests),
);

export const getEnvInfo = (
defaultUserName = "anonymous",
): { computerName: string; userName: string } => ({
Expand Down

0 comments on commit 972b7b7

Please sign in to comment.