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

fix: resolve file field for suites based on tests when using mocha exports ui #935

Draft
wants to merge 2 commits into
base: hermione@7
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hermione",
"version": "7.5.7",
"version": "7.5.8-rc.1",
"description": "Tests framework based on mocha and wdio",
"main": "build/hermione.js",
"files": [
Expand Down
5 changes: 4 additions & 1 deletion src/test-reader/mocha-reader/tree-builder-decorator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Suite, Test, Hook } = require("../test-object");
const crypto = require("../../utils/crypto");
const { computeFile } = require("./utils");

class TreeBuilderDecorator {
#treeBuilder;
Expand All @@ -17,7 +18,9 @@ class TreeBuilderDecorator {
}

addSuite(mochaSuite) {
const { id: mochaId, file } = mochaSuite;
const { id: mochaId } = mochaSuite;
const file = computeFile(mochaSuite) ?? "unknown-file";

const positionInFile = this.#suiteCounter.get(file) || 0;
const id = mochaSuite.root ? mochaId : crypto.getShortMD5(file) + positionInFile;
const suite = this.#mkTestObject(Suite, mochaSuite, { id });
Expand Down
64 changes: 64 additions & 0 deletions src/test-reader/mocha-reader/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// When using "exports" mocha interface, "file" field is absent on suites, and available on tests only.
// This helper tries to resolve "file" field for suites, drilling down to child tests and using their file field.

const findTopmostSuite = mochaSuite => {
if (mochaSuite.parent && mochaSuite.parent.root) {
return mochaSuite;
}

if (!mochaSuite.parent) {
return null;
}

return findTopmostSuite(mochaSuite.parent);
};

const getFile = mochaSuite => {
if (mochaSuite.file) {
return mochaSuite.file;
}

if (mochaSuite.tests.length > 0 && mochaSuite.tests[0].file) {
return mochaSuite.tests[0].file;
}

for (const childSuite of mochaSuite.suites) {
const computedFile = getFile(childSuite);
if (computedFile) {
return computedFile;
}
}

return null;
};

const fillSuitesFileField = (mochaSuite, file) => {
mochaSuite.file = file;

if (mochaSuite.suites) {
for (const childSuite of mochaSuite.suites) {
fillSuitesFileField(childSuite, file);
}
}
};

const computeFile = mochaSuite => {
if (mochaSuite.file) {
return mochaSuite.file;
}

const topmostSuite = findTopmostSuite(mochaSuite);
const file = topmostSuite && getFile(topmostSuite);

if (topmostSuite && file) {
fillSuitesFileField(topmostSuite, file);

return file;
}

return null;
};

module.exports = {
computeFile,
};
Loading