Skip to content

Commit 7d01e33

Browse files
fix: emit suite names as TAP comments and use test.title instead of fullTitle
1 parent 5b27e3a commit 7d01e33

3 files changed

Lines changed: 92 additions & 2 deletions

File tree

lib/reporters/tap.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
2020
var EVENT_RUN_END = constants.EVENT_RUN_END;
2121
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
2222
var EVENT_TEST_END = constants.EVENT_TEST_END;
23+
var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
2324
var sprintf = util.format;
2425

2526
class TAP extends Base {
@@ -53,6 +54,12 @@ class TAP extends Base {
5354
self._producer.writeVersion();
5455
});
5556

57+
runner.on(EVENT_SUITE_BEGIN, function (suite) {
58+
if (!suite.root) {
59+
println("# %s", suite.title);
60+
}
61+
});
62+
5663
runner.on(EVENT_TEST_END, function () {
5764
++n;
5865
});
@@ -83,7 +90,7 @@ class TAP extends Base {
8390
* @return {String} title with any hash character removed
8491
*/
8592
function title(test) {
86-
return test.fullTitle().replace(/#/g, "");
93+
return test.title.replace(/#/g, "");
8794
}
8895

8996
/**

test/integration/reporters.spec.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe("reporters", function () {
236236
for (var i = 0; i + 1 < outputLines.length; i++) {
237237
if (
238238
testLinePredicate(outputLines[i]) &&
239-
testLinePredicate(outputLines[i + 1]) === false
239+
anythingElsePredicate(outputLines[i + 1])
240240
) {
241241
var blockLinesStart = i + 1;
242242
var blockLinesEnd =

test/reporters/tap.spec.cjs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var makeRunReporter = helpers.createRunReporterFunction;
1010

1111
var EVENT_RUN_BEGIN = events.EVENT_RUN_BEGIN;
1212
var EVENT_RUN_END = events.EVENT_RUN_END;
13+
var EVENT_SUITE_BEGIN = events.EVENT_SUITE_BEGIN;
1314
var EVENT_TEST_END = events.EVENT_TEST_END;
1415
var EVENT_TEST_FAIL = events.EVENT_TEST_FAIL;
1516
var EVENT_TEST_PASS = events.EVENT_TEST_PASS;
@@ -23,6 +24,7 @@ describe("TAP reporter", function () {
2324

2425
function createTest() {
2526
return {
27+
title: expectedTitle,
2628
fullTitle: function () {
2729
return expectedTitle;
2830
},
@@ -529,4 +531,85 @@ describe("TAP reporter", function () {
529531
});
530532
});
531533
});
534+
535+
describe("suite hierarchy", function () {
536+
var options = {
537+
reporterOptions: {
538+
tapVersion: "12",
539+
},
540+
};
541+
542+
describe("on 'suite' event", function () {
543+
var stdout;
544+
var expectedSuiteTitle = "My Suite";
545+
546+
before(async function () {
547+
var suite = { root: false, title: expectedSuiteTitle };
548+
var runner = createMockRunner(
549+
"suite",
550+
EVENT_SUITE_BEGIN,
551+
null,
552+
null,
553+
suite,
554+
);
555+
runner.suite = "";
556+
({ stdout } = await runReporter({}, runner, options));
557+
});
558+
559+
it("should write a comment with the suite title", function () {
560+
expect(stdout[0], "to equal", "# " + expectedSuiteTitle + "\n");
561+
});
562+
});
563+
564+
describe("on 'suite' event with root suite", function () {
565+
var stdout;
566+
567+
before(async function () {
568+
var suite = { root: true, title: "" };
569+
var runner = createMockRunner(
570+
"suite",
571+
EVENT_SUITE_BEGIN,
572+
null,
573+
null,
574+
suite,
575+
);
576+
runner.suite = "";
577+
({ stdout } = await runReporter({}, runner, options));
578+
});
579+
580+
it("should not write a comment for the root suite", function () {
581+
expect(stdout, "to be empty");
582+
});
583+
});
584+
585+
describe("on 'pass' event with suite context", function () {
586+
var stdout;
587+
var testTitle = "should work";
588+
589+
before(async function () {
590+
var test = {
591+
title: testTitle,
592+
fullTitle: function () {
593+
return "My Suite should work";
594+
},
595+
slow: noop,
596+
};
597+
var runner = createMockRunner(
598+
"start test",
599+
EVENT_TEST_END,
600+
EVENT_TEST_PASS,
601+
null,
602+
test,
603+
);
604+
runner.suite = "";
605+
({ stdout } = await runReporter({}, runner, options));
606+
});
607+
608+
it("should use test title not fullTitle in output", function () {
609+
var expectedMessage =
610+
"ok " + countAfterTestEnd + " " + testTitle + "\n";
611+
expect(stdout[0], "to equal", expectedMessage);
612+
});
613+
});
614+
});
532615
});

0 commit comments

Comments
 (0)