-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(allure-jasmine): selective run implementation (via #1063)
- Loading branch information
Showing
14 changed files
with
328 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 +1,10 @@ | ||
export type JasmineBeforeAfterFn = typeof beforeEach; | ||
export type JasmineBeforeAfterFn = typeof global.beforeEach; | ||
|
||
export type JasmineSpecFn = typeof global.it; | ||
|
||
export type JasmineSuiteFn = typeof global.describe; | ||
|
||
export type TestPlanIndex = { | ||
ids: ReadonlySet<string>; | ||
fullNames: ReadonlySet<string>; | ||
}; |
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,113 @@ | ||
import path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import { LabelName } from "allure-js-commons"; | ||
import type { Label } from "allure-js-commons"; | ||
import { addSkipLabelAsMeta, parseTestPlan } from "allure-js-commons/sdk/reporter"; | ||
import type { JasmineSpecFn, JasmineSuiteFn, TestPlanIndex } from "./model.js"; | ||
import { getAllureNamesAndLabels } from "./utils.js"; | ||
|
||
const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const getCallerFileStackTraceFormatFn = (_: Error, stackTraces: NodeJS.CallSite[]): string | undefined => { | ||
return stackTraces[0]?.getFileName(); | ||
}; | ||
|
||
const getCallerFile = <TFn extends (...args: any) => any>(fn: TFn) => { | ||
const originalPrepareStackTrace = Error.prepareStackTrace; | ||
Error.prepareStackTrace = getCallerFileStackTraceFormatFn; | ||
try { | ||
const obj = {}; | ||
Error.captureStackTrace(obj, fn); | ||
return (obj as any).stack as ReturnType<typeof getCallerFileStackTraceFormatFn>; | ||
} finally { | ||
Error.prepareStackTrace = originalPrepareStackTrace; | ||
} | ||
}; | ||
|
||
const originalErrorToString = (e: Error) => Error.prototype.toString.call(e); | ||
|
||
const defaultPrepareStackTrace = (error: Error, stackTraces: NodeJS.CallSite[]): string => | ||
stackTraces.length === 0 | ||
? originalErrorToString(error) | ||
: [originalErrorToString(error), ...stackTraces].join("\n at "); | ||
|
||
const isAllureJasmineFrame = (frame: NodeJS.CallSite) => frame.getFileName()?.startsWith(dirname + path.sep) ?? false; | ||
|
||
const createStackFilter = | ||
(prepareStackTrace: (error: Error, stackTraces: NodeJS.CallSite[]) => any) => | ||
(error: Error, stackTraces: NodeJS.CallSite[]) => | ||
prepareStackTrace( | ||
error, | ||
stackTraces.filter((frame) => !isAllureJasmineFrame(frame)), | ||
); | ||
|
||
const hideAllureFramesFromFunc = <TArgs extends any[], TReturn, TFn extends (...args: TArgs) => TReturn>( | ||
target: TFn, | ||
...args: TArgs | ||
) => { | ||
const originalPrepareStackTrace = Error.prepareStackTrace; | ||
const underlyingPrepareStackTrace = originalPrepareStackTrace ?? defaultPrepareStackTrace; | ||
Error.prepareStackTrace = createStackFilter(underlyingPrepareStackTrace); | ||
try { | ||
return target(...args); | ||
} finally { | ||
Error.prepareStackTrace = originalPrepareStackTrace; | ||
} | ||
}; | ||
|
||
const getIndexedTestPlan = (): TestPlanIndex | undefined => { | ||
const testplan = parseTestPlan(); | ||
if (testplan) { | ||
return { | ||
ids: new Set(testplan.tests.filter((e) => e.id).map((e) => e.id!.toString())), | ||
fullNames: new Set(testplan.tests.filter((e) => e.selector).map((e) => e.selector!)), | ||
}; | ||
} | ||
}; | ||
|
||
const isInTestPlan = (testplan: TestPlanIndex | undefined, fullName: string, labels: readonly Label[]) => { | ||
if (testplan && !testplan.fullNames.has(fullName)) { | ||
const allureId = labels.find((l) => l.name === LabelName.ALLURE_ID)?.value; | ||
return allureId && testplan.ids.has(allureId); | ||
} | ||
return true; | ||
}; | ||
|
||
export const enableAllureJasmineTestPlan = () => { | ||
const jasmineDescribe: JasmineSuiteFn = global.describe; | ||
const jasmineIt: JasmineSpecFn = global.it; | ||
const jasmineXit: JasmineSpecFn = global.xit; | ||
|
||
const suites: string[] = []; | ||
let currentFile: string | undefined; | ||
const testplan = getIndexedTestPlan(); | ||
|
||
global.describe = (description: string, specDefinitions: () => void) => { | ||
const callerFile = getCallerFile(global.describe); | ||
if (!callerFile) { | ||
return hideAllureFramesFromFunc(jasmineDescribe, description, specDefinitions); | ||
} else { | ||
if (callerFile !== currentFile) { | ||
currentFile = callerFile; | ||
suites.splice(0, suites.length); | ||
} | ||
|
||
suites.push(description); | ||
try { | ||
return hideAllureFramesFromFunc(jasmineDescribe, description, specDefinitions); | ||
} finally { | ||
suites.pop(); | ||
} | ||
} | ||
}; | ||
|
||
global.it = (expectation: string, assertion?: jasmine.ImplementationCallback, timeout?: number) => { | ||
const filename = currentFile ?? getCallerFile(global.it); | ||
const { fullName, labels } = getAllureNamesAndLabels(filename, suites, expectation); | ||
if (isInTestPlan(testplan, fullName, labels)) { | ||
return hideAllureFramesFromFunc(jasmineIt, expectation, assertion, timeout); | ||
} else { | ||
return hideAllureFramesFromFunc(jasmineXit, addSkipLabelAsMeta(expectation), assertion, timeout); | ||
} | ||
}; | ||
}; |
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
Oops, something went wrong.