-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
testTypeImpl.ts
63 lines (55 loc) · 1.93 KB
/
testTypeImpl.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
58
59
60
61
62
63
/**
* Helpers to deal with Playwright test internal stuff.
* See: https://github.com/microsoft/playwright/blob/main/packages/playwright-test/src/common/testType.ts
*/
import { test, Fixtures } from '@playwright/test';
import { Location } from '@playwright/test/reporter';
import { getSymbolByName } from '../utils';
import { TestTypeCommon } from './types';
type FixturesWithLocation = {
fixtures: Fixtures;
location: Location;
};
const testTypeSymbol = getSymbolByName(test, 'testType');
/**
* Returns test fixtures using Symbol.
*/
function getTestFixtures(test: TestTypeCommon) {
return getTestImpl(test).fixtures as FixturesWithLocation[];
}
function getTestImpl(test: TestTypeCommon) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return test[testTypeSymbol] as any;
}
/**
* Run step with location pointing to Given, When, Then call.
*/
// eslint-disable-next-line max-params
export async function runStepWithCustomLocation(
test: TestTypeCommon,
stepText: string,
location: Location,
body: () => unknown,
) {
const testInfo = test.info();
// See: https://github.com/microsoft/playwright/blob/main/packages/playwright/src/common/testType.ts#L221
// @ts-expect-error _runAsStep is hidden from public
return testInfo._runAsStep({ category: 'test.step', title: stepText, location }, async () => {
return await body();
});
}
/**
* Returns true if test contains all fixtures of subtest.
* - test was extended from subtest
* - test is a result of mergeTests(subtest, ...)
*/
export function isTestContainsSubtest(test: TestTypeCommon, subtest: TestTypeCommon) {
if (test === subtest) return true;
const testFixtures = new Set(getTestFixtures(test).map((f) => locationToString(f.location)));
return getTestFixtures(subtest).every((f) => {
return testFixtures.has(locationToString(f.location));
});
}
function locationToString({ file, line, column }: Location) {
return `${file}:${line}:${column}`;
}