-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
263 additions
and
148 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
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,10 +1,11 @@ | ||
import type { MaybeNullish, MaybePromise, PropertyExtractorContext } from 'jest-allure2-reporter'; | ||
|
||
import { isPromiseLike } from '../../utils'; | ||
import { thruMaybePromise } from '../../utils'; | ||
|
||
export const last = async <T>( | ||
export const last = <T>( | ||
context: PropertyExtractorContext<{}, MaybePromise<MaybeNullish<T[]>>>, | ||
): Promise<T | undefined> => { | ||
const value = isPromiseLike(context.value) ? await context.value : context.value; | ||
return value?.at(-1); | ||
): MaybePromise<T | undefined> => { | ||
return thruMaybePromise<MaybeNullish<T[]>, T | undefined>(context.value, (value) => | ||
value?.at(-1), | ||
); | ||
}; |
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,26 +1,42 @@ | ||
import type { | ||
PropertyExtractorContext, | ||
PromisedProperties, | ||
TestCaseExtractorContext, | ||
AllureTestStepResult, | ||
} from 'jest-allure2-reporter'; | ||
|
||
import { testStep } from './testStep'; | ||
import { testCaseSteps } from './testCaseSteps'; | ||
import { createTestCaseContext } from './__utils__/contexts'; | ||
|
||
describe('testCaseSteps', () => { | ||
it('should extract nested steps', async () => { | ||
let counter = 0; | ||
const testStep = jest.fn().mockImplementation(() => ({ | ||
displayName: `Step ${++counter}`, | ||
})); | ||
const displayName = jest.fn().mockImplementation(() => `Step ${++counter}`); | ||
const testStepExtractor = testStep({ displayName }); | ||
const testCase = testCaseSteps(testStepExtractor, 'testCaseMetadata'); | ||
const context: PropertyExtractorContext< | ||
TestCaseExtractorContext, | ||
PromisedProperties<AllureTestStepResult>[] | ||
> = { | ||
...createTestCaseContext(), | ||
value: [], | ||
}; | ||
|
||
const testCase = testCaseSteps(testStep, 'testCaseMetadata'); | ||
const context = createTestCaseContext(); | ||
context.testCaseMetadata.steps = [{}, {}]; | ||
|
||
const result = testCase(context); | ||
if (Array.isArray(result?.steps)) { | ||
expect(result?.steps).toHaveLength(2); | ||
expect(result?.steps?.[0]?.displayName).toBe('Step 1'); | ||
expect(result?.steps?.[1]?.displayName).toBe('Step 2'); | ||
if (Array.isArray(result)) { | ||
expect(result).toHaveLength(2); | ||
expect(result[0].displayName).toBe('Step 1'); | ||
expect(result[1].displayName).toBe('Step 2'); | ||
// Testing memoization | ||
expect(result[0].displayName).toBe('Step 1'); | ||
expect(result[1].displayName).toBe('Step 2'); | ||
} else { | ||
expect(result?.steps).toBeInstanceOf(Array); | ||
expect(result).toBeInstanceOf(Array); | ||
} | ||
|
||
expect(testStep).toHaveBeenCalledTimes(2); | ||
expect(displayName).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
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,43 +1,40 @@ | ||
import type { TestCaseExtractorContext } from 'jest-allure2-reporter'; | ||
import type { | ||
AllureTestItemMetadata, | ||
AllureTestStepResult, | ||
TestStepExtractorContext, | ||
PromisedProperties, | ||
} from 'jest-allure2-reporter'; | ||
|
||
import type { TestCaseExtractor, TestStepExtractor } from '../types'; | ||
import { isNonNullish, maybePromiseAll, onceWithLoopDetection } from '../../utils'; | ||
import type { TestStepExtractor, TestStepsExtractor } from '../types'; | ||
|
||
interface HasMetadata { | ||
testCaseMetadata?: TestCaseExtractorContext['testCaseMetadata']; | ||
testFileMetadata?: TestCaseExtractorContext['testFileMetadata']; | ||
testRunMetadata?: TestCaseExtractorContext['testRunMetadata']; | ||
} | ||
type HasMetadata<Context, Key extends keyof Context> = Context & { | ||
[key in Key]: AllureTestItemMetadata; | ||
}; | ||
|
||
export function testCaseSteps<Context extends HasMetadata>( | ||
testStep: TestStepExtractor<Context>, | ||
metadataKey: keyof HasMetadata, | ||
): TestCaseExtractor<Context> { | ||
return (context) => { | ||
Object.defineProperty(context.value, 'steps', { | ||
enumerable: true, | ||
get: onceWithLoopDetection(() => { | ||
const steps = context[metadataKey]?.steps; | ||
if (steps && steps.length > 0) { | ||
return maybePromiseAll( | ||
steps.map((testStepMetadata) => { | ||
const stepContext = { | ||
...context, | ||
testStepMetadata, | ||
result: {}, | ||
value: undefined as never, | ||
}; | ||
export function testCaseSteps< | ||
BaseContext extends Partial<Omit<TestStepExtractorContext, 'testStepMetadata'>>, | ||
Key extends keyof BaseContext, | ||
Context extends HasMetadata<BaseContext, Key>, | ||
>( | ||
testStep: TestStepExtractor<TestStepExtractorContext>, | ||
metadataKey: Key, | ||
): TestStepsExtractor<Context, void> { | ||
return (context): PromisedProperties<AllureTestStepResult>[] => { | ||
const steps = context[metadataKey]?.steps; | ||
if (!steps || steps.length === 0) { | ||
return []; | ||
} | ||
|
||
return testStep(stepContext); | ||
}), | ||
(allSteps) => allSteps.filter(isNonNullish), | ||
); | ||
} | ||
return steps.map((testStepMetadata) => { | ||
const testStepContext: any = { | ||
...context, | ||
testStepMetadata, | ||
result: {}, | ||
value: {}, | ||
}; | ||
|
||
return; | ||
}), | ||
testStepContext.result = testStep(testStepContext); | ||
return testStep(testStepContext); | ||
}); | ||
|
||
return context.value; | ||
}; | ||
} |
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,12 +1,23 @@ | ||
import type { ReporterOptions } from 'jest-allure2-reporter'; | ||
|
||
import { testCaseSteps } from './custom'; | ||
import { defaultOptions } from './default'; | ||
import { extendOptions } from './extendOptions'; | ||
import type { ReporterConfig } from './types'; | ||
import type { ReporterConfig, ReporterFinalConfig } from './types'; | ||
import { defaultOverrides } from './override'; | ||
|
||
export function resolveOptions(custom?: ReporterOptions | undefined): ReporterConfig { | ||
return extendOptions(extendOptions(defaultOptions(), custom), defaultOverrides()); | ||
export function resolveOptions(custom?: ReporterOptions | undefined): ReporterFinalConfig { | ||
const config: ReporterConfig = extendOptions( | ||
extendOptions(defaultOptions(), custom), | ||
defaultOverrides(), | ||
); | ||
|
||
return { | ||
...config, | ||
testFileSteps: testCaseSteps(config.testStep, 'testFileMetadata'), | ||
testCaseSteps: testCaseSteps(config.testStep, 'testCaseMetadata'), | ||
testRunSteps: testCaseSteps(config.testStep, 'testRunMetadata'), | ||
} as ReporterFinalConfig; | ||
} | ||
|
||
export { type ReporterConfig } from './types'; | ||
export { type ReporterFinalConfig as ReporterConfig } from './types'; |
Oops, something went wrong.