Skip to content

Commit

Permalink
add report for allure-mocha package
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Jun 24, 2024
1 parent 4bbfbb5 commit f3c1a79
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 36 deletions.
4 changes: 3 additions & 1 deletion .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/allure-mocha/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
overrides: [
{
extends: ["plugin:@typescript-eslint/disable-type-checked"],
files: [".eslintrc.cjs"],
files: [".eslintrc.cjs", "vitest.config.ts", "vitest-setup.ts"],
},
{
files: ["**/*.cjs", "**/*.js"],
Expand Down
6 changes: 5 additions & 1 deletion packages/allure-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
"dist"
],
"scripts": {
"allure-report": "allure serve ./out/allure-results",
"clean": "rimraf ./dist ./out",
"compile": "run-s 'compile:*'",
"compile:esm-babel": "babel --config-file ./babel.esm.json ./src --out-dir ./dist/esm --extensions '.ts' --source-maps",
"compile:cjs-babel": "babel --config-file ./babel.cjs.json ./src --out-dir ./dist/cjs --extensions '.ts' --source-maps",
"compile:types": "tsc",
"compile:fixup": "node ./scripts/fixup.mjs",
"generate-report": "allure generate ./out/allure-results -o ./out/allure-report --clean",
"lint": "eslint ./src ./test --ext .ts",
"lint:fix": "eslint ./src ./test --ext .ts --fix",
"test": "run-s --print-name 'test:*'",
Expand All @@ -68,6 +70,8 @@
"@types/node": "^20.6.3",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"allure-commandline": "^2.29.0",
"allure-vitest": "workspace:*",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-transform-import-meta": "^2.2.1",
"chai": "^4.3.8",
Expand All @@ -88,7 +92,7 @@
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vitest": "^1.4.0"
"vitest": "^1.6.0"
},
"peerDependencies": {
"mocha": ">=6.2.x"
Expand Down
3 changes: 1 addition & 2 deletions packages/allure-mocha/test/spec/framework/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ describe("defaults", () => {
});

it("has timing props", () => {
expect(test.start).greaterThan(0);
expect(test.stop).greaterThan(test.start as number);
expect(test.start).greaterThanOrEqual(sampleStart);
expect(test.stop).greaterThanOrEqual(test.start as number);
expect(test.stop).lessThanOrEqual(sampleStop);
});

Expand Down
76 changes: 47 additions & 29 deletions packages/allure-mocha/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
import { appendFileSync } from "node:fs";
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
import * as path from "node:path";
import { attachment, attachmentPath, parameter, step } from "allure-js-commons";
import type { AllureResults, Category } from "allure-js-commons/sdk";
import { MessageReader } from "allure-js-commons/sdk/reporter";

Expand Down Expand Up @@ -50,6 +51,10 @@ abstract class AllureMochaTestRunner {
}

run = async (...samples: readonly (string | readonly string[])[]) => {
// TODO parameter should accept any type
await parameter("parallel", `${RUN_IN_PARALLEL}`);
await parameter("module", SPEC_FORMAT);

const testDir = path.join(this.runResultsDir, randomUUID());

const filesToCopy = [...this.getFilesToCopy(testDir)];
Expand All @@ -73,49 +78,61 @@ abstract class AllureMochaTestRunner {
const cmdContent = [scriptPath, ...scriptArgs].join("\n ");

await mkdir(testDir, { recursive: true });
await Promise.all([
...filesToCopy.map(async ([src, dst]) => {
for (const [src, dst] of filesToCopy) {
const name = path.basename(dst);
await step(name, async () => {
const dstDir = path.dirname(dst);
await mkdir(dstDir, { recursive: true });
await copyFile(src, dst);
}),
...filesToTransform.map(async ([src, dst, uncomment]) => {
await attachmentPath(name, src, { contentType: "text/plain" });
});
}

for (const [src, dst, uncomment] of filesToTransform) {
const name = path.basename(dst);
await step(name, async () => {
const dstDir = path.dirname(dst);
const content = await readFile(src, { encoding: "utf-8" });
await mkdir(dstDir, { recursive: true });
const uncommentedSample = content.replace(uncomment, "$1");
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
writeFile(dst, uncommentedSample, { encoding: "utf-8" });
}),
writeFile(cmdPath, cmdContent, "utf-8"),
]);
await writeFile(dst, uncommentedSample, { encoding: "utf-8" });
await attachment(name, uncommentedSample, { contentType: "text/plain" });
});
}

await step(path.basename(cmdPath), async () => {
await writeFile(cmdPath, cmdContent, "utf-8");
});

if (this.config.testplan) {
const testplanPath = path.join(testDir, "testplan.json");
this.config.env ??= {};
this.config.env.ALLURE_TESTPLAN_PATH = testplanPath;
const selectorPrefix = path.relative(path.join(__dirname, ".."), testDir);
await writeFile(
testplanPath,
JSON.stringify({
version: "1.0",
tests: this.config.testplan.map((test) => ({
id: test.id,
selector: test.selector ? this.resolveTestplanSelector(selectorPrefix, test.selector) : undefined,
})),
}),
{ encoding: "utf-8" },
);
const content = JSON.stringify({
version: "1.0",
tests: this.config.testplan.map((test) => ({
id: test.id,
selector: test.selector ? this.resolveTestplanSelector(selectorPrefix, test.selector) : undefined,
})),
});
await writeFile(testplanPath, content, { encoding: "utf-8" });
await attachment(path.basename(testplanPath), content, {
contentType: "application/json",
fileExtension: "json",
});
}

const testProcess = fork(scriptPath, scriptArgs, {
env: {
...process.env,
...this.config.env,
ALLURE_MOCHA_TESTHOST_PID: process.pid.toString(),
},
cwd: testDir,
stdio: "pipe",
const testProcess = await step(`${scriptPath} ${scriptArgs.join(" ")}`, () => {
return fork(scriptPath, scriptArgs, {
env: {
...process.env,
...this.config.env,
ALLURE_MOCHA_TESTHOST_PID: process.pid.toString(),
},
cwd: testDir,
stdio: "pipe",
});
});

const messageReader = new MessageReader();
Expand All @@ -133,7 +150,8 @@ abstract class AllureMochaTestRunner {
});

return await new Promise<AllureResults>((resolve, reject) => {
testProcess.on("exit", (code, signal) => {
testProcess.on("exit", async (code, signal) => {
await messageReader.attachResults();
if ((code ?? -1) >= 0 && !signal) {
resolve(messageReader.results);
} else if (signal) {
Expand Down
1 change: 1 addition & 0 deletions packages/allure-mocha/vitest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "allure-vitest/setup";
3 changes: 2 additions & 1 deletion packages/allure-mocha/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export default defineConfig({
fileParallelism: false,
testTimeout: 25000,
hookTimeout: 25000,
reporters: ["default"],
globalSetup: ["./test/setup.ts"],
setupFiles: ["./vitest-setup.ts"],
reporters: ["default", ["allure-vitest/reporter", { resultsDir: "./out/allure-results" }]],
typecheck: {
enabled: true,
tsconfig: "./tsconfig.test.json",
Expand Down
4 changes: 3 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4442,7 +4442,9 @@ __metadata:
"@types/node": "npm:^20.6.3"
"@typescript-eslint/eslint-plugin": "npm:^7.0.0"
"@typescript-eslint/parser": "npm:^7.0.0"
allure-commandline: "npm:^2.29.0"
allure-js-commons: "workspace:*"
allure-vitest: "workspace:*"
babel-plugin-add-module-exports: "npm:^1.0.4"
babel-plugin-transform-import-meta: "npm:^2.2.1"
chai: "npm:^4.3.8"
Expand All @@ -4463,7 +4465,7 @@ __metadata:
ts-node: "npm:^10.9.1"
tslib: "npm:^2.6.2"
typescript: "npm:^5.2.2"
vitest: "npm:^1.4.0"
vitest: "npm:^1.6.0"
peerDependencies:
mocha: ">=6.2.x"
languageName: unknown
Expand Down

0 comments on commit f3c1a79

Please sign in to comment.