From a13a4ef7e6b29f433157750261bed068c3de2a10 Mon Sep 17 00:00:00 2001 From: shadowusr Date: Wed, 22 May 2024 14:23:51 +0300 Subject: [PATCH 1/2] fix: resolve file field for suites based on tests when using mocha exports ui --- .../mocha-reader/tree-builder-decorator.js | 5 +- src/test-reader/mocha-reader/utils.js | 64 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/test-reader/mocha-reader/utils.js diff --git a/src/test-reader/mocha-reader/tree-builder-decorator.js b/src/test-reader/mocha-reader/tree-builder-decorator.js index 4c775ccc4..27cad49de 100644 --- a/src/test-reader/mocha-reader/tree-builder-decorator.js +++ b/src/test-reader/mocha-reader/tree-builder-decorator.js @@ -1,5 +1,6 @@ const { Suite, Test, Hook } = require("../test-object"); const crypto = require("../../utils/crypto"); +const { computeFile } = require("./utils"); class TreeBuilderDecorator { #treeBuilder; @@ -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 }); diff --git a/src/test-reader/mocha-reader/utils.js b/src/test-reader/mocha-reader/utils.js new file mode 100644 index 000000000..eac1aadce --- /dev/null +++ b/src/test-reader/mocha-reader/utils.js @@ -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, +}; From 116f56ca917a840b3b2cbfe6d89546f1284bfc59 Mon Sep 17 00:00:00 2001 From: shadowusr Date: Thu, 23 May 2024 13:56:02 +0300 Subject: [PATCH 2/2] 7.5.8-rc.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1c508fcbc..877007f26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hermione", - "version": "7.5.7", + "version": "7.5.8-rc.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "hermione", - "version": "7.5.7", + "version": "7.5.8-rc.1", "license": "MIT", "dependencies": { "@gemini-testing/commander": "2.15.3", diff --git a/package.json b/package.json index 5848ad5fc..d129fab2a 100644 --- a/package.json +++ b/package.json @@ -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": [