Skip to content

Commit a32a10d

Browse files
committed
fix: resolve file field for suites based on tests when using mocha exports ui
1 parent a67b802 commit a32a10d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/test-reader/mocha-reader/tree-builder-decorator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { Suite, Test, Hook } = require("../test-object");
22
const crypto = require("../../utils/crypto");
3+
const { computeFile } = require("./utils");
34

45
class TreeBuilderDecorator {
56
#treeBuilder;
@@ -17,7 +18,9 @@ class TreeBuilderDecorator {
1718
}
1819

1920
addSuite(mochaSuite) {
20-
const { id: mochaId, file } = mochaSuite;
21+
const { id: mochaId } = mochaSuite;
22+
const file = computeFile(mochaSuite) ?? "unknown-file";
23+
2124
const positionInFile = this.#suiteCounter.get(file) || 0;
2225
const id = mochaSuite.root ? mochaId : crypto.getShortMD5(file) + positionInFile;
2326
const suite = this.#mkTestObject(Suite, mochaSuite, { id });
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// When using "exports" mocha interface, "file" field is absent on suites, and available on tests only.
2+
// This helper tries to resolve "file" field for suites, drilling down to child tests and using their file field.
3+
const computeFile = mochaSuite => {
4+
if (mochaSuite.file) {
5+
return mochaSuite.file;
6+
}
7+
8+
if (mochaSuite.tests.length > 0 && mochaSuite.tests[0].file) {
9+
mochaSuite.file = mochaSuite.tests[0].file;
10+
11+
return mochaSuite.file;
12+
}
13+
14+
for (const childSuite of mochaSuite.suites) {
15+
const computedFile = computeFile(childSuite);
16+
if (computedFile) {
17+
mochaSuite.file = computedFile;
18+
19+
return mochaSuite.file;
20+
}
21+
}
22+
23+
return null;
24+
};
25+
26+
module.exports = {
27+
computeFile,
28+
};

0 commit comments

Comments
 (0)