-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mocha): testplan support, env info and categories, metadata (via #…
- Loading branch information
Showing
16 changed files
with
413 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,110 @@ | ||
import type * as Mocha from "mocha"; | ||
import { hostname } from "node:os"; | ||
import { dirname, extname, join } from "node:path"; | ||
import { env, pid } from "node:process"; | ||
import { env } from "node:process"; | ||
import { fileURLToPath } from "node:url"; | ||
import { isMainThread, threadId } from "node:worker_threads"; | ||
import type { Label } from "allure-js-commons"; | ||
import { LabelName } from "allure-js-commons"; | ||
import type { TestPlanV1, TestPlanV1Test } from "allure-js-commons/sdk"; | ||
import { extractMetadataFromString } from "allure-js-commons/sdk"; | ||
import { getHostLabel, getRelativePath, getThreadLabel, md5, parseTestPlan } from "allure-js-commons/sdk/reporter"; | ||
|
||
const filename = fileURLToPath(import.meta.url); | ||
|
||
export const getSuitesOfMochaTest = (test: Mocha.Test) => test.titlePath().slice(0, -1); | ||
const allureMochaDataKey = Symbol("Used to access Allure extra data in Mocha objects"); | ||
|
||
export const resolveParallelModeSetupFile = () => | ||
join(dirname(filename), `setupAllureMochaParallel${extname(filename)}`); | ||
type AllureMochaTestData = { | ||
isIncludedInTestRun: boolean; | ||
fullName: string; | ||
labels: readonly Label[]; | ||
displayName: string; | ||
}; | ||
|
||
const getAllureData = (item: Mocha.Test): AllureMochaTestData => { | ||
const data = (item as any)[allureMochaDataKey]; | ||
if (!data) { | ||
const meta = extractMetadataFromString(item.title); | ||
const defaultData: AllureMochaTestData = { | ||
isIncludedInTestRun: true, | ||
fullName: createAllureFullName(item), | ||
labels: meta.labels, | ||
displayName: meta.cleanTitle, | ||
}; | ||
(item as any)[allureMochaDataKey] = defaultData; | ||
return defaultData; | ||
} | ||
return data; | ||
}; | ||
|
||
const createAllureFullName = (test: Mocha.Test) => { | ||
const titlePath = test.titlePath().join(" > "); | ||
return test.file ? `${getRelativePath(test.file)}: ${titlePath}` : titlePath; | ||
}; | ||
|
||
const createTestPlanSelectorIndex = (testplan: TestPlanV1) => createTestPlanIndex((e) => e.selector, testplan); | ||
|
||
const createTestPlanIdIndex = (testplan: TestPlanV1) => createTestPlanIndex((e) => e.id?.toString(), testplan); | ||
|
||
const createTestPlanIndex = <T>(keySelector: (entry: TestPlanV1Test) => T, testplan: TestPlanV1) => | ||
new Set(testplan.tests.map((e) => keySelector(e)).filter((v) => v) as readonly T[]); | ||
|
||
export type TestPlanIndices = { | ||
fullNameIndex: ReadonlySet<string>; | ||
idIndex: ReadonlySet<string>; | ||
}; | ||
|
||
export const resolveMochaWorkerId = () => env.MOCHA_WORKER_ID ?? (isMainThread ? pid : threadId).toString(); | ||
export const createTestPlanIndices = (): TestPlanIndices | undefined => { | ||
const testplan = parseTestPlan(); | ||
if (testplan) { | ||
return { | ||
fullNameIndex: createTestPlanSelectorIndex(testplan), | ||
idIndex: createTestPlanIdIndex(testplan), | ||
}; | ||
} | ||
}; | ||
|
||
const allureHostName = env.ALLURE_HOST_NAME || hostname(); | ||
export const getAllureFullName = (test: Mocha.Test) => getAllureData(test).fullName; | ||
|
||
export const getHostLabel = (): Label => ({ | ||
name: LabelName.HOST, | ||
value: allureHostName, | ||
}); | ||
export const isIncludedInTestRun = (test: Mocha.Test) => getAllureData(test).isIncludedInTestRun; | ||
|
||
export const getWorkerIdLabel = (): Label => ({ | ||
name: LabelName.THREAD, | ||
value: resolveMochaWorkerId(), | ||
}); | ||
export const getAllureMetaLabels = (test: Mocha.Test) => getAllureData(test).labels; | ||
|
||
export const getAllureId = (data: AllureMochaTestData) => { | ||
const values = data.labels.filter((l) => l.name === LabelName.ALLURE_ID).map((l) => l.value); | ||
if (values.length) { | ||
return values[0]; | ||
} | ||
}; | ||
|
||
export const getAllureDisplayName = (test: Mocha.Test) => getAllureData(test).displayName; | ||
|
||
export const getSuitesOfMochaTest = (test: Mocha.Test) => test.titlePath().slice(0, -1); | ||
|
||
export const resolveParallelModeSetupFile = () => | ||
join(dirname(filename), `setupAllureMochaParallel${extname(filename)}`); | ||
|
||
export const getInitialLabels = (): Label[] => [ | ||
{ name: LabelName.LANGUAGE, value: "javascript" }, | ||
{ name: LabelName.FRAMEWORK, value: "mocha" }, | ||
getHostLabel(), | ||
getWorkerIdLabel(), | ||
getThreadLabel(env.MOCHA_WORKER_ID), | ||
]; | ||
|
||
export const getTestCaseId = (test: Mocha.Test) => { | ||
const suiteTitles = test.titlePath().slice(0, -1); | ||
return md5(JSON.stringify([...suiteTitles, getAllureDisplayName(test)])); | ||
}; | ||
|
||
export const applyTestPlan = (ids: ReadonlySet<string>, selectors: ReadonlySet<string>, rootSuite: Mocha.Suite) => { | ||
const suiteQueue = []; | ||
for (let s: Mocha.Suite | undefined = rootSuite; s; s = suiteQueue.shift()) { | ||
for (const test of s.tests) { | ||
const allureData = getAllureData(test); | ||
const allureId = getAllureId(allureData); | ||
if (!selectors.has(allureData.fullName) && (!allureId || !ids.has(allureId))) { | ||
allureData.isIncludedInTestRun = false; | ||
test.pending = true; | ||
} | ||
} | ||
suiteQueue.push(...s.suites); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/allure-mocha/test/fixtures/samples/labels/meta/change.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// cjs: const { it } = require("mocha"); | ||
// esm: import { it } from "mocha"; | ||
|
||
it("a test a changing meta @allure.label.foo:bar", async () => {}); | ||
|
||
it("a test a changing meta @allure.label.foo:baz", async () => {}); |
4 changes: 4 additions & 0 deletions
4
packages/allure-mocha/test/fixtures/samples/labels/meta/id/1004.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// cjs: const { it } = require("mocha"); | ||
// esm: import { it } from "mocha"; | ||
|
||
it("a test with ID 1004 @allure.id:1004", async () => {}); |
4 changes: 4 additions & 0 deletions
4
packages/allure-mocha/test/fixtures/samples/labels/meta/id/1005.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// cjs: const { it } = require("mocha"); | ||
// esm: import { it } from "mocha"; | ||
|
||
it("a test with ID 1005 @allure.id:1005", async () => {}); |
4 changes: 4 additions & 0 deletions
4
packages/allure-mocha/test/fixtures/samples/labels/meta/multiple.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// cjs: const { it } = require("mocha"); | ||
// esm: import { it } from "mocha"; | ||
|
||
it("a test two embedded custom label @allure.label.foo:bar @allure.label.baz:qux", async () => {}); |
4 changes: 4 additions & 0 deletions
4
packages/allure-mocha/test/fixtures/samples/labels/meta/single.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// cjs: const { it } = require("mocha"); | ||
// esm: import { it } from "mocha"; | ||
|
||
it("a test with an embedded custom label @allure.label.foo:bar", async () => {}); |
Oops, something went wrong.