/
step.ts
57 lines (51 loc) · 1.46 KB
/
step.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { TestInfo } from '@playwright/test'
// eslint-disable-next-line import/no-extraneous-dependencies
import { test } from '@playwright/test'
export interface StepProps {
category: string
title: string
apiName?: string
params?: Record<string, string | number | boolean | null | undefined>
}
// Access the internal Playwright API until it's exposed publicly.
// See https://github.com/microsoft/playwright/issues/27059.
interface TestInfoWithRunAsStep extends TestInfo {
_runAsStep: <T>(
stepInfo: StepProps,
handler: (result: { complete: Complete }) => Promise<T>
) => Promise<T>
}
type Complete = (result: { error?: any }) => void
function isWithRunAsStep(
testInfo: TestInfo
): testInfo is TestInfoWithRunAsStep {
return '_runAsStep' in testInfo
}
export async function step<T>(
testInfo: TestInfo,
props: StepProps,
handler: (complete: Complete) => Promise<Awaited<T>>
): Promise<Awaited<T>> {
if (isWithRunAsStep(testInfo)) {
return testInfo._runAsStep(props, ({ complete }) => handler(complete))
}
// Fallback to the `test.step()`.
let result: Awaited<T>
let reportedError: any
try {
console.log(props.title, props)
await test.step(props.title, async () => {
result = await handler(({ error }) => {
reportedError = error
if (reportedError) {
throw reportedError
}
})
})
} catch (error) {
if (error !== reportedError) {
throw error
}
}
return result!
}