Skip to content

Commit

Permalink
add categories and env info support for cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Jul 2, 2024
1 parent 08eea54 commit ee64a05
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/allure-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class AllureCypress {
this.createEmptyRunContext(run.spec.absolute);
this.endSpec(run.spec, run.video || undefined);
});

this.allureRuntime.writeEnvironmentInfo();
this.allureRuntime.writeCategoriesDefinitions();
}

endSpec(spec: Cypress.Spec, cypressVideoPath?: string) {
Expand Down
72 changes: 72 additions & 0 deletions packages/allure-cypress/test/spec/categories.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect, it } from "vitest";
import { Status } from "allure-js-commons";
import { runCypressInlineTest } from "../utils.js";

it("has categories", async () => {
const { categories } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": () => `
it('sample test', () => {});
`,
"cypress.config.js": ({ allureCypressModuleBasePath }) => `
const { allureCypress } = require("${allureCypressModuleBasePath}/reporter.js");
module.exports = {
e2e: {
baseUrl: "https://allurereport.org",
viewportWidth: 1240,
setupNodeEvents: (on, config) => {
allureCypress(on, {
categories: [
{
name: "Sad tests",
messageRegex: ".*Sad.*",
matchedStatuses: ["${Status.FAILED}"],
},
{
name: "Infrastructure problems",
messageRegex: ".*RuntimeException.*",
matchedStatuses: ["${Status.BROKEN}"],
},
{
name: "Outdated tests",
messageRegex: ".*FileNotFound.*",
matchedStatuses: ["${Status.BROKEN}"],
},
{
name: "Regression",
messageRegex: "${String.raw`.*\\sException:.*`}",
matchedStatuses: ["${Status.BROKEN}"],
},
]
});
return config;
},
},
};
`,
});

expect(categories).toEqual([
{
name: "Sad tests",
messageRegex: ".*Sad.*",
matchedStatuses: [Status.FAILED],
},
{
name: "Infrastructure problems",
messageRegex: ".*RuntimeException.*",
matchedStatuses: [Status.BROKEN],
},
{
name: "Outdated tests",
messageRegex: ".*FileNotFound.*",
matchedStatuses: [Status.BROKEN],
},
{
name: "Regression",
messageRegex: ".*\\sException:.*",
matchedStatuses: [Status.BROKEN],
},
]);
});
35 changes: 35 additions & 0 deletions packages/allure-cypress/test/spec/environmentInfo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, it } from "vitest";
import { runCypressInlineTest } from "../utils.js";

it("has environment info", async () => {
const { envInfo } = await runCypressInlineTest({
"cypress/e2e/sample.cy.js": () => `
it('sample test', () => {});
`,
"cypress.config.js": ({ allureCypressModuleBasePath }) => `
const { allureCypress } = require("${allureCypressModuleBasePath}/reporter.js");
module.exports = {
e2e: {
baseUrl: "https://allurereport.org",
viewportWidth: 1240,
setupNodeEvents: (on, config) => {
allureCypress(on, {
environmentInfo: {
envVar1: "envVar1Value",
envVar2: "envVar2Value",
},
});
return config;
},
},
};
`,
});

expect(envInfo).toEqual({
envVar1: "envVar1Value",
envVar2: "envVar2Value",
});
});
27 changes: 22 additions & 5 deletions packages/allure-cypress/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { randomUUID } from "node:crypto";
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { dirname, join, resolve as resolvePath } from "node:path";
import type { TestResult, TestResultContainer } from "allure-js-commons";
import type { AllureResults } from "allure-js-commons/sdk";
import type { AllureResults, EnvironmentInfo } from "allure-js-commons/sdk";
import { parseProperties } from "allure-js-commons/sdk/reporter";

type CypressTestFiles = Record<
string,
(modulesPaths: { allureCommonsModulePath: string; allureCypressModulePath: string }) => string
>;
type CypressModulesPaths = {
allureCommonsModulePath: string;
allureCypressModulePath: string;
allureCypressModuleBasePath: string;
};

type CypressTestFiles = Record<string, (modulesPaths: CypressModulesPaths) => string>;

export const runCypressInlineTest = async (
testFiles: CypressTestFiles,
Expand All @@ -18,6 +22,8 @@ export const runCypressInlineTest = async (
tests: [],
groups: [],
attachments: {},
categories: [],
envInfo: undefined,
};
const testDir = join(__dirname, "fixtures", randomUUID());
const allureCypressModuleBasePath = dirname(require.resolve("allure-cypress"));
Expand Down Expand Up @@ -62,6 +68,7 @@ export const runCypressInlineTest = async (
testFilesToWrite[testFile]({
allureCommonsModulePath,
allureCypressModulePath,
allureCypressModuleBasePath,
}),
"utf8",
);
Expand Down Expand Up @@ -93,6 +100,16 @@ export const runCypressInlineTest = async (
const resultFiles = await readdir(testResultsDir);

for (const resultFile of resultFiles) {
if (resultFile === "categories.json") {
res.categories = JSON.parse(await readFile(join(testResultsDir, resultFile), "utf8"));
continue;
}

if (resultFile === "environment.properties") {
res.envInfo = parseProperties(await readFile(join(testResultsDir, resultFile), "utf8")) as EnvironmentInfo;
continue;
}

if (/-attachment\.\S+$/.test(resultFile)) {
res.attachments[resultFile] = await readFile(join(testResultsDir, resultFile), "utf8");
continue;
Expand Down

0 comments on commit ee64a05

Please sign in to comment.