Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c5d2b7c

Browse filesBrowse files
committed
feat(allure-cypress): make fullName CWD-independent
1 parent 7cca6b1 commit c5d2b7c
Copy full SHA for c5d2b7c

File tree

Expand file treeCollapse file tree

9 files changed

+157
-39
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+157
-39
lines changed

‎packages/allure-cypress/src/model.ts

Copy file name to clipboardExpand all lines: packages/allure-cypress/src/model.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export type CypressTestStartMessage = {
8282
type: "cypress_test_start";
8383
data: {
8484
name: string;
85-
fullName: string;
85+
fullNameSuffix: string;
8686
start: number;
8787
labels: Label[];
8888
};
@@ -161,7 +161,6 @@ export type CypressMessage =
161161

162162
export type SpecContext = {
163163
specPath: string;
164-
package: string;
165164
test: string | undefined;
166165
fixture: string | undefined;
167166
commandSteps: string[];
@@ -183,6 +182,7 @@ export type AllureSpecState = {
183182
};
184183
initialized: boolean;
185184
testPlan: TestPlanV1 | null | undefined;
185+
projectDir?: string;
186186
messages: CypressMessage[];
187187
currentTest?: CypressTest;
188188
};

‎packages/allure-cypress/src/reporter.ts

Copy file name to clipboardExpand all lines: packages/allure-cypress/src/reporter.ts
+17-8Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
getHostLabel,
1212
getLanguageLabel,
1313
getPackageLabel,
14+
getPosixPath,
15+
getProjectRoot,
1416
getSuiteLabels,
1517
getThreadLabel,
1618
parseTestPlan,
@@ -281,15 +283,20 @@ export class AllureCypress {
281283
}
282284
};
283285

284-
#startTest = (context: SpecContext, { data }: CypressTestStartMessage) => {
286+
#startTest = (context: SpecContext, { data: { fullNameSuffix, ...testResultData } }: CypressTestStartMessage) => {
285287
this.#emitPreviousTestScope(context);
286288
const testScope = this.allureRuntime.startScope();
287289
context.testScope = testScope;
288-
context.test = this.#addNewTestResult(context, data, [context.videoScope, ...context.suiteScopes, testScope]);
290+
context.test = this.#addNewTestResult(context, fullNameSuffix, testResultData, [
291+
context.videoScope,
292+
...context.suiteScopes,
293+
testScope,
294+
]);
289295
};
290296

291297
#addNewTestResult = (
292298
context: SpecContext,
299+
fullNameSuffix: string,
293300
{ labels: metadataLabels = [], ...otherTestData }: Partial<TestResult>,
294301
scopes: string[],
295302
) =>
@@ -299,14 +306,14 @@ export class AllureCypress {
299306
labels: [
300307
getLanguageLabel(),
301308
getFrameworkLabel("cypress"),
302-
303309
...getSuiteLabels(context.suiteNames),
304310
...metadataLabels,
305311
...getEnvironmentLabels(),
306312
getHostLabel(),
307313
getThreadLabel(),
308314
getPackageLabel(context.specPath),
309315
],
316+
fullName: `${getPosixPath(context.specPath)}#${fullNameSuffix}`,
310317
...otherTestData,
311318
},
312319
scopes,
@@ -351,13 +358,13 @@ export class AllureCypress {
351358

352359
#addSkippedTest = (
353360
context: SpecContext,
354-
{ data: { suites, duration, retries, ...testResultData } }: CypressSkippedTestMessage,
361+
{ data: { fullNameSuffix, suites, duration, retries, ...testResultData } }: CypressSkippedTestMessage,
355362
) => {
356363
// Tests skipped because of a hook error may share all suites of the current context
357364
// or just a part thereof (if it's from a sibling suite).
358365
const scopes = suites.map((s) => context.suiteIdToScope.get(s)).filter((s): s is string => Boolean(s));
359366

360-
const testUuid = this.#addNewTestResult(context, testResultData, [context.videoScope, ...scopes]);
367+
const testUuid = this.#addNewTestResult(context, fullNameSuffix, testResultData, [context.videoScope, ...scopes]);
361368
this.#stopExistingTestResult(testUuid, { duration, retries });
362369
this.allureRuntime.writeTest(testUuid);
363370
};
@@ -470,10 +477,9 @@ export class AllureCypress {
470477
};
471478

472479
#initializeSpecContext = (absolutePath: string) => {
473-
const specPath = path.relative(process.cwd(), absolutePath);
480+
const specPath = path.relative(getProjectRoot(), absolutePath);
474481
const context: SpecContext = {
475482
specPath,
476-
package: specPath.replaceAll("/", "."),
477483
test: undefined,
478484
fixture: undefined,
479485
commandSteps: [],
@@ -494,6 +500,7 @@ const createRuntimeState = (allureConfig?: AllureCypressConfig): AllureSpecState
494500
initialized: false,
495501
messages: [],
496502
testPlan: parseTestPlan(),
503+
projectDir: getProjectRoot(),
497504
});
498505

499506
const getRuntimeConfigDefaults = ({
@@ -544,10 +551,12 @@ export const allureCypress = (
544551
allureConfig = cypressConfig as AllureCypressConfig;
545552
}
546553

554+
const hasCypressConfig = cypressConfig && "env" in cypressConfig;
555+
547556
const allureCypressReporter = new AllureCypress(allureConfig);
548557
allureCypressReporter.attachToCypress(on);
549558

550-
if (cypressConfig && "env" in cypressConfig) {
559+
if (hasCypressConfig) {
551560
initializeRuntimeState(cypressConfig, allureConfig);
552561
}
553562

‎packages/allure-cypress/src/state.ts

Copy file name to clipboardExpand all lines: packages/allure-cypress/src/state.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const getAllureState = () => {
1010
messages: [],
1111
testPlan: undefined,
1212
currentTest: undefined,
13+
projectDir: undefined,
1314
};
1415
Cypress.env("allure", state);
1516
}
@@ -34,6 +35,8 @@ export const enqueueRuntimeMessage = (message: CypressMessage) => {
3435

3536
export const getAllureTestPlan = () => getAllureState().testPlan;
3637

38+
export const getProjectDir = () => getAllureState().projectDir;
39+
3740
export const getCurrentTest = () => getAllureState().currentTest;
3841

3942
export const setCurrentTest = (test: CypressTest) => {

‎packages/allure-cypress/src/utils.ts

Copy file name to clipboardExpand all lines: packages/allure-cypress/src/utils.ts
+20-8Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { extractMetadataFromString, getMessageAndTraceFromError, getStatusFromEr
33
import type { TestPlanV1 } from "allure-js-commons/sdk";
44
import { ALLURE_REPORT_STEP_COMMAND, ALLURE_REPORT_SYSTEM_HOOK } from "./model.js";
55
import type { CypressCommand, CypressHook, CypressSuite, CypressTest } from "./model.js";
6-
import { getAllureTestPlan } from "./state.js";
6+
import { getAllureTestPlan, getProjectDir } from "./state.js";
77

88
export const DEFAULT_RUNTIME_CONFIG = {
99
stepsFromCommands: {
@@ -74,14 +74,19 @@ export const last = <T = unknown>(arr: T[]): T | undefined => {
7474
return arr[arr.length - 1];
7575
};
7676

77+
const resolveSpecRelativePath = (spec: Cypress.Spec) => {
78+
const projectDir = getProjectDir();
79+
const specPath = projectDir ? spec.absolute.substring(projectDir.length + 1) : spec.relative;
80+
const win = Cypress.platform === "win32";
81+
return win ? specPath.replaceAll("\\", "/") : specPath;
82+
};
83+
7784
export const getNamesAndLabels = (spec: Cypress.Spec, test: CypressTest) => {
7885
const rawName = test.title;
7986
const { cleanTitle: name, labels } = extractMetadataFromString(rawName);
8087
const suites = test.titlePath().slice(0, -1);
81-
const win = Cypress.platform === "win32";
82-
const specPath = win ? spec.relative.replaceAll("\\", "/") : spec.relative;
83-
const fullName = `${specPath}#${[...suites, name].join(" ")}`;
84-
return { name, labels, fullName };
88+
const fullNameSuffix = `${[...suites, name].join(" ")}`;
89+
return { name, labels, fullNameSuffix };
8590
};
8691

8792
export const getTestStartData = (test: CypressTest) => ({
@@ -101,8 +106,9 @@ export const getTestSkipData = () => ({
101106
export const applyTestPlan = (spec: Cypress.Spec, root: CypressSuite) => {
102107
const testPlan = getAllureTestPlan();
103108
if (testPlan) {
109+
const specPath = resolveSpecRelativePath(spec);
104110
for (const suite of iterateSuites(root)) {
105-
const indicesToRemove = getIndicesOfDeselectedTests(testPlan, spec, suite.tests);
111+
const indicesToRemove = getIndicesOfDeselectedTests(testPlan, spec, specPath, suite.tests);
106112
removeSortedIndices(suite.tests, indicesToRemove);
107113
}
108114
}
@@ -150,10 +156,16 @@ export const isRootAfterAllHook = (hook: CypressHook) => hook.parent!.root && ho
150156
const includedInTestPlan = (testPlan: TestPlanV1, fullName: string, allureId: string | undefined): boolean =>
151157
testPlan.tests.some((test) => (allureId && test.id?.toString() === allureId) || test.selector === fullName);
152158

153-
const getIndicesOfDeselectedTests = (testPlan: TestPlanV1, spec: Cypress.Spec, tests: readonly CypressTest[]) => {
159+
const getIndicesOfDeselectedTests = (
160+
testPlan: TestPlanV1,
161+
spec: Cypress.Spec,
162+
specPath: string,
163+
tests: readonly CypressTest[],
164+
) => {
154165
const indicesToRemove: number[] = [];
155166
tests.forEach((test, index) => {
156-
const { fullName, labels } = getNamesAndLabels(spec, test);
167+
const { fullNameSuffix, labels } = getNamesAndLabels(spec, test);
168+
const fullName = `${specPath}#${fullNameSuffix}`;
157169
const allureId = labels.find(({ name }) => name === LabelName.ALLURE_ID)?.value;
158170

159171
if (!includedInTestPlan(testPlan, fullName, allureId)) {
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import path from "node:path";
2+
import { describe, expect, it } from "vitest";
3+
import type { TestPlanV1 } from "allure-js-commons/sdk";
4+
import { runCypressInlineTest } from "../utils.js";
5+
6+
describe("fullName, and package", () => {
7+
describe("with testplan", () => {
8+
it("should not depend on a CWD", async () => {
9+
const testPlan: TestPlanV1 = {
10+
version: "1.0",
11+
tests: [{ selector: "cypress/e2e/sample.cy.js#foo" }],
12+
};
13+
const { tests } = await runCypressInlineTest(
14+
{
15+
"cypress/e2e/sample.cy.js": () => `
16+
it("foo", () => {});
17+
`,
18+
"testplan.json": () => JSON.stringify(testPlan),
19+
},
20+
{
21+
cwd: "cypress",
22+
env: (testDir) => ({ ALLURE_TESTPLAN_PATH: path.join(testDir, "testplan.json") }),
23+
},
24+
);
25+
26+
expect(tests).toHaveLength(1);
27+
expect(tests[0].labels).toEqual(expect.arrayContaining([{ name: "package", value: "cypress.e2e.sample.cy.js" }]));
28+
expect(tests[0].fullName).toEqual("cypress/e2e/sample.cy.js#foo");
29+
});
30+
});
31+
32+
describe("when no Cypress config provided", () => {
33+
it("should not depend on a CWD", async () => {
34+
const { tests } = await runCypressInlineTest(
35+
{
36+
"cypress.config.js": ({ allureCypressReporterModulePath, supportFilePath, specPattern, allureDirPath }) => `
37+
const { allureCypress } = require("${allureCypressReporterModulePath}");
38+
39+
module.exports = {
40+
e2e: {
41+
baseUrl: "https://allurereport.org",
42+
supportFile: "${supportFilePath}",
43+
specPattern: "${specPattern}",
44+
viewportWidth: 1240,
45+
setupNodeEvents: (on, config) => {
46+
allureCypress(on, {
47+
resultsDir: "${allureDirPath}",
48+
});
49+
50+
return config;
51+
},
52+
},
53+
};
54+
`,
55+
"cypress/e2e/sample.cy.js": () => `
56+
it("foo", () => {});
57+
`,
58+
},
59+
{
60+
cwd: "cypress",
61+
},
62+
);
63+
64+
expect(tests).toHaveLength(1);
65+
expect(tests[0].labels).toEqual(expect.arrayContaining([{ name: "package", value: "cypress.e2e.sample.cy.js" }]));
66+
expect(tests[0].fullName).toEqual("cypress/e2e/sample.cy.js#foo");
67+
});
68+
});
69+
});

‎packages/allure-cypress/test/spec/labels.test.ts

Copy file name to clipboardExpand all lines: packages/allure-cypress/test/spec/labels.test.ts
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ it("should add labels from env variables", async () => {
4848
});
4949
`,
5050
},
51-
() => ({
52-
ALLURE_LABEL_A: "a",
53-
ALLURE_LABEL_B: "b",
54-
}),
51+
{
52+
env: () => ({
53+
ALLURE_LABEL_A: "a",
54+
ALLURE_LABEL_B: "b",
55+
}),
56+
},
5557
);
5658

5759
expect(tests).toHaveLength(1);

‎packages/allure-cypress/test/spec/testplan.test.ts

Copy file name to clipboardExpand all lines: packages/allure-cypress/test/spec/testplan.test.ts
+15-9Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ it("respects testplan", async () => {
6868
});
6969
`,
7070
},
71-
(testDir) => ({
72-
ALLURE_TESTPLAN_PATH: join(testDir, testPlanFilename),
73-
}),
71+
{
72+
env: (testDir) => ({
73+
ALLURE_TESTPLAN_PATH: join(testDir, testPlanFilename),
74+
}),
75+
},
7476
);
7577

7678
expect(tests).toHaveLength(3);
@@ -121,9 +123,11 @@ it("should deselect all tests not in test plan", async () => {
121123
it('test 2', () => {});
122124
`,
123125
},
124-
(testDir) => ({
125-
ALLURE_TESTPLAN_PATH: join(testDir, testPlanFilename),
126-
}),
126+
{
127+
env: (testDir) => ({
128+
ALLURE_TESTPLAN_PATH: join(testDir, testPlanFilename),
129+
}),
130+
},
127131
);
128132

129133
expect(tests).toEqual([]);
@@ -156,9 +160,11 @@ it("should not trigger hooks", async () => {
156160
it("bar", () => {});
157161
`,
158162
},
159-
(testDir) => ({
160-
ALLURE_TESTPLAN_PATH: join(testDir, testPlanFilename),
161-
}),
163+
{
164+
env: (testDir) => ({
165+
ALLURE_TESTPLAN_PATH: join(testDir, testPlanFilename),
166+
}),
167+
},
162168
);
163169

164170
expect(tests).toEqual([

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.