Skip to content

Commit 16be832

Browse files
Дмитрий Еврейновformaceft-93
authored andcommitted
fix(allure-js): fix test duplicating by adding package name to path
1 parent 7428daa commit 16be832

7 files changed

Lines changed: 80 additions & 6 deletions

File tree

packages/allure-codeceptjs/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const runCodeceptJsInlineTest = async (
2424
): Promise<RunResult> => {
2525
const testFiles = {
2626
// package.json is used to find project root in case of absolute file paths are used
27-
"package.json": '{ "name": "dummy"}',
27+
"package.json": "{}",
2828
"codecept.conf.js": await readFile(resolvePath(__dirname, "./samples/codecept.conf.js"), "utf-8"),
2929
"helper.js": await readFile(resolvePath(__dirname, "./samples/helper.js"), "utf-8"),
3030
...files,

packages/allure-cucumberjs/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const runCucumberInlineTest = async (
9090
fileExtension: ".js",
9191
});
9292
});
93-
await writeFile(join(testDir, "package.json"), String.raw`{"name": "dummy"}`, "utf8");
93+
await writeFile(join(testDir, "package.json"), String.raw`{}`, "utf8");
9494
await step("world.js", async () => {
9595
await writeFile(worldFilePath, worldContent, "utf8");
9696
await attachment("world.js", worldContent, {

packages/allure-cypress/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const runCypressInlineTest = async (
4444
const configFilePath = relative(processCwd, join(testDir, "cypress.config.js"));
4545

4646
const testFilesToWrite: CypressTestFiles = {
47-
"package.json": () => String.raw`{"name": "dummy"}`,
47+
"package.json": () => String.raw`{}`,
4848
"cypress/support/e2e.js": ({ allureCypressModulePath }) => `
4949
require("${allureCypressModulePath}");
5050
`,

packages/allure-jasmine/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const runJasmineInlineTest = async (
1313
const testDir = path.join(__dirname, "fixtures", randomUUID());
1414
const testFiles = {
1515
// package.json is used to find project root in case of absolute file paths are used
16-
"package.json": '{ "name": "dummy"}',
16+
"package.json": "{}",
1717
"spec/support/jasmine.json": await readFile(path.join(__dirname, "./samples/spec/support/jasmine.json"), "utf8"),
1818
// eslint-disable-next-line @typescript-eslint/no-require-imports
1919
"spec/helpers/allure.js": require("./samples/spec/helpers/modern/allure.cjs"),

packages/allure-js-commons/src/sdk/reporter/utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ export const readImageAsBase64 = async (filePath: string): Promise<string | unde
7070
}
7171
};
7272

73+
export const getPackageName = (() => {
74+
let cachedPackageName: string | undefined;
75+
76+
const resolvePackageName = () => {
77+
const cwd = process.cwd();
78+
const packageJsonPath = path.join(cwd, "package.json");
79+
80+
// Only use package name from cwd, not parent directories
81+
// This ensures test fixtures don't inherit parent package names
82+
try {
83+
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf-8");
84+
const packageJson = JSON.parse(packageJsonContent);
85+
86+
if (packageJson.name && typeof packageJson.name === "string") {
87+
return packageJson.name;
88+
}
89+
} catch {
90+
// No package.json in cwd or invalid - that's fine
91+
}
92+
93+
return undefined;
94+
};
95+
96+
return () => {
97+
if (!cachedPackageName) {
98+
cachedPackageName = resolvePackageName();
99+
}
100+
return cachedPackageName;
101+
};
102+
})();
103+
73104
export const getProjectRoot = (() => {
74105
let cachedProjectRoot: string | null = null;
75106

@@ -107,6 +138,14 @@ export const getRelativePath = (filepath: string) => {
107138
const projectRoot = getProjectRoot();
108139
filepath = path.relative(projectRoot, filepath);
109140
}
141+
142+
// Prepend package name to ensure uniqueness across packages (e.g., in monorepos)
143+
// Industry standard: use package/module prefix for test IDs in multi-module environments
144+
const packageName = getPackageName();
145+
if (packageName) {
146+
filepath = `${packageName}${path.sep}${filepath}`;
147+
}
148+
110149
return filepath;
111150
};
112151

packages/allure-js-commons/test/sdk/reporter/utils.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import path from "node:path";
12
import { describe, expect, it } from "vitest";
23
import { LabelName } from "../../../src/model.js";
3-
import { getSuiteLabels } from "../../../src/sdk/reporter/utils.js";
4+
import { getPackageName, getRelativePath, getSuiteLabels } from "../../../src/sdk/reporter/utils.js";
45

56
describe("getSuiteLabels", () => {
67
describe("with empty suites", () => {
@@ -54,3 +55,37 @@ describe("getSuiteLabels", () => {
5455
});
5556
});
5657
});
58+
59+
describe("getPackageName", () => {
60+
it("should cache the package name on subsequent calls", () => {
61+
const first = getPackageName();
62+
const second = getPackageName();
63+
expect(first).toBe(second);
64+
expect(first).toBe("allure-js-commons");
65+
});
66+
67+
it("should continue searching parent directories if package.json has no name", () => {
68+
// If a package.json exists but has no "name" field (or is malformed),
69+
// the function logs an error and continues searching up the directory tree.
70+
// This test documents the expected behavior.
71+
// In practice, this test will find "allure-js-commons" since all package.json
72+
// files in this repo are well-formed.
73+
const packageName = getPackageName();
74+
expect(packageName).toBeDefined();
75+
});
76+
});
77+
78+
describe("getRelativePath", () => {
79+
it("should prepend package name to relative path", () => {
80+
const filepath = path.join("test", "spec", "example.test.ts");
81+
const result = getRelativePath(filepath);
82+
expect(result).toBe(path.join("allure-js-commons", "test", "spec", "example.test.ts"));
83+
});
84+
85+
it("should handle absolute paths and prepend package name", () => {
86+
const absolutePath = path.join(process.cwd(), "test", "spec", "example.test.ts");
87+
const result = getRelativePath(absolutePath);
88+
expect(result).toContain(`allure-js-commons${path.sep}`);
89+
expect(result).toContain(path.join("test", "spec", "example.test.ts"));
90+
});
91+
});

packages/allure-vitest/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const runVitestInlineTest = async (
6666
},
6767
});
6868
`,
69-
"package.json": JSON.stringify({ name: "dummy" }),
69+
"package.json": JSON.stringify({}),
7070
...testFiles,
7171
};
7272

0 commit comments

Comments
 (0)