Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: preserve source file on exports-interface suites (#6371)

Navpreet Singhee5d12846e32
diff --git a/lib/interfaces/exports.js b/lib/interfaces/exports.js
index 96cc09c8..45824c65 100644
--- a/lib/interfaces/exports.js
+++ b/lib/interfaces/exports.js
@@ -48,6 +48,7 @@ function exportsInterface(suite) {
}
} else {
suite = Suite.create(suites[0], key);
+ suite.file = file;
suites.unshift(suite);
visit(obj[key], file);
suites.shift();
diff --git a/test/unit/interfaces/exports.spec.cjs b/test/unit/interfaces/exports.spec.cjs
new file mode 100644
index 00000000..83a77cb3
--- /dev/null
+++ b/test/unit/interfaces/exports.spec.cjs
@@ -0,0 +1,57 @@
+"use strict";
+
+const Mocha = require("../../../lib/mocha.cjs");
+const { Suite } = Mocha;
+
+describe("exports interface", function () {
+ it("assigns file to nested suites and their hooks", function () {
+ const mocha = new Mocha({ ui: "exports" });
+ const file = "/tmp/array.exports.js";
+
+ mocha.suite.emit(
+ Suite.constants.EVENT_FILE_REQUIRE,
+ {
+ Array: {
+ before: function () {},
+ after: function () {},
+ "#indexOf()": {
+ beforeEach: function () {},
+ afterEach: function () {},
+ "should return -1 when the value is not present": function () {},
+ },
+ },
+ },
+ file,
+ );
+
+ const arraySuite = mocha.suite.suites[0];
+ const nestedSuite = arraySuite.suites[0];
+
+ expect(arraySuite.file, "to be", file);
+ expect(nestedSuite.file, "to be", file);
+ expect(arraySuite.getHooks("beforeAll")[0].file, "to be", file);
+ expect(arraySuite.getHooks("afterAll")[0].file, "to be", file);
+ expect(nestedSuite.getHooks("beforeEach")[0].file, "to be", file);
+ expect(nestedSuite.getHooks("afterEach")[0].file, "to be", file);
+ expect(nestedSuite.tests[0].file, "to be", file);
+ });
+
+ it("keeps each file on its own suites and leaves the shared root unset", function () {
+ const mocha = new Mocha({ ui: "exports" });
+
+ mocha.suite.emit(
+ Suite.constants.EVENT_FILE_REQUIRE,
+ { One: {} },
+ "/tmp/one.js",
+ );
+ mocha.suite.emit(
+ Suite.constants.EVENT_FILE_REQUIRE,
+ { Two: {} },
+ "/tmp/two.js",
+ );
+
+ expect(mocha.suite.file, "to be undefined");
+ expect(mocha.suite.suites[0].file, "to be", "/tmp/one.js");
+ expect(mocha.suite.suites[1].file, "to be", "/tmp/two.js");
+ });
+});