@itmetsam/samurai-framework 0.21.5 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/dist/api.d.ts +6 -1
  2. package/dist/api.js +1 -0
  3. package/dist/browser/browser-context.d.ts +18 -6
  4. package/dist/browser/browser-context.js +25 -7
  5. package/dist/browser/browser.d.ts +4 -4
  6. package/dist/browser/browser.js +11 -8
  7. package/dist/browser/chrome-launch.js +9 -1
  8. package/dist/browser/context-cookie-store.d.ts +17 -0
  9. package/dist/browser/context-cookie-store.js +90 -0
  10. package/dist/browser/index.d.ts +3 -1
  11. package/dist/browser/index.js +1 -1
  12. package/dist/browser/keyboard.d.ts +35 -0
  13. package/dist/browser/keyboard.js +83 -0
  14. package/dist/browser/mouse.d.ts +42 -0
  15. package/dist/browser/mouse.js +66 -0
  16. package/dist/browser/page-navigation.d.ts +0 -2
  17. package/dist/browser/page-navigation.js +1 -6
  18. package/dist/browser/page.d.ts +18 -8
  19. package/dist/browser/page.js +18 -9
  20. package/dist/browser/storage-state.d.ts +0 -2
  21. package/dist/browser/storage-state.js +0 -20
  22. package/dist/browser/use-applier.d.ts +16 -5
  23. package/dist/browser/use-applier.js +44 -17
  24. package/dist/browser/use-commands.d.ts +36 -0
  25. package/dist/browser/use-commands.js +120 -0
  26. package/dist/cli/args.d.ts +8 -4
  27. package/dist/cli/args.js +8 -5
  28. package/dist/cli/console-reporter.js +5 -1
  29. package/dist/cli/main.js +1 -0
  30. package/dist/cli/usage.d.ts +1 -1
  31. package/dist/cli/usage.js +4 -1
  32. package/dist/config/base-url.d.ts +4 -0
  33. package/dist/config/base-url.js +6 -0
  34. package/dist/config/config.js +6 -0
  35. package/dist/config/fully-parallel.d.ts +3 -0
  36. package/dist/config/fully-parallel.js +6 -0
  37. package/dist/config/group-use.d.ts +8 -0
  38. package/dist/config/group-use.js +41 -0
  39. package/dist/config/resolved-config.d.ts +2 -0
  40. package/dist/config/resolved-config.js +3 -0
  41. package/dist/config/storage-state-file.d.ts +3 -0
  42. package/dist/config/storage-state-file.js +20 -0
  43. package/dist/config/use-options.d.ts +3 -1
  44. package/dist/config/use-options.js +70 -14
  45. package/dist/config/web-server-options.d.ts +7 -0
  46. package/dist/config/web-server-options.js +111 -0
  47. package/dist/locator/check-actions.js +1 -1
  48. package/dist/locator/drag-actions.d.ts +17 -0
  49. package/dist/locator/drag-actions.js +80 -0
  50. package/dist/locator/element-actions.d.ts +10 -4
  51. package/dist/locator/element-actions.js +23 -16
  52. package/dist/locator/element-state.d.ts +5 -0
  53. package/dist/locator/element-state.js +2 -1
  54. package/dist/locator/event-actions.d.ts +10 -0
  55. package/dist/locator/event-actions.js +81 -0
  56. package/dist/locator/input-actions.d.ts +2 -11
  57. package/dist/locator/input-actions.js +3 -56
  58. package/dist/locator/key-actions.d.ts +11 -0
  59. package/dist/locator/key-actions.js +52 -0
  60. package/dist/locator/key-map.d.ts +16 -0
  61. package/dist/locator/key-map.js +76 -0
  62. package/dist/locator/locator.d.ts +35 -5
  63. package/dist/locator/locator.js +70 -16
  64. package/dist/locator/page-scripts.d.ts +2 -0
  65. package/dist/locator/page-scripts.js +7 -0
  66. package/dist/locator/pointer-actions.d.ts +44 -0
  67. package/dist/locator/pointer-actions.js +117 -0
  68. package/dist/locator/pointer-element-actions.d.ts +21 -0
  69. package/dist/locator/pointer-element-actions.js +42 -0
  70. package/dist/request/api-request-context.d.ts +65 -0
  71. package/dist/request/api-request-context.js +198 -0
  72. package/dist/request/api-response.d.ts +34 -0
  73. package/dist/request/api-response.js +57 -0
  74. package/dist/request/cookie-jar.d.ts +48 -0
  75. package/dist/request/cookie-jar.js +164 -0
  76. package/dist/request/http-transport.d.ts +31 -0
  77. package/dist/request/http-transport.js +113 -0
  78. package/dist/request/redirect.d.ts +10 -0
  79. package/dist/request/redirect.js +21 -0
  80. package/dist/request/request-body.d.ts +27 -0
  81. package/dist/request/request-body.js +71 -0
  82. package/dist/request/request.d.ts +16 -0
  83. package/dist/request/request.js +20 -0
  84. package/dist/runner/all-hooks.d.ts +7 -1
  85. package/dist/runner/all-hooks.js +17 -1
  86. package/dist/runner/fixture-defs.js +1 -0
  87. package/dist/runner/fixtures.d.ts +4 -1
  88. package/dist/runner/fixtures.js +3 -2
  89. package/dist/runner/global-hooks.d.ts +13 -0
  90. package/dist/runner/global-hooks.js +66 -0
  91. package/dist/runner/options.d.ts +13 -2
  92. package/dist/runner/output-tail.d.ts +14 -0
  93. package/dist/runner/output-tail.js +30 -0
  94. package/dist/runner/pool-schedule.d.ts +22 -0
  95. package/dist/runner/pool-schedule.js +55 -0
  96. package/dist/runner/pool-units.d.ts +34 -0
  97. package/dist/runner/pool-units.js +85 -0
  98. package/dist/runner/pooled-run.d.ts +7 -5
  99. package/dist/runner/pooled-run.js +19 -9
  100. package/dist/runner/registry.js +6 -2
  101. package/dist/runner/reporter.d.ts +10 -0
  102. package/dist/runner/reporter.js +12 -2
  103. package/dist/runner/request-fixture.d.ts +14 -0
  104. package/dist/runner/request-fixture.js +25 -0
  105. package/dist/runner/run-hooks.d.ts +16 -0
  106. package/dist/runner/run-hooks.js +52 -0
  107. package/dist/runner/run-test.js +7 -2
  108. package/dist/runner/run.d.ts +1 -0
  109. package/dist/runner/run.js +1 -0
  110. package/dist/runner/select.d.ts +15 -4
  111. package/dist/runner/select.js +38 -12
  112. package/dist/runner/serial-run.d.ts +23 -0
  113. package/dist/runner/serial-run.js +88 -0
  114. package/dist/runner/serial.d.ts +34 -0
  115. package/dist/runner/serial.js +63 -0
  116. package/dist/runner/shard.d.ts +22 -0
  117. package/dist/runner/shard.js +73 -0
  118. package/dist/runner/suite.d.ts +3 -3
  119. package/dist/runner/suite.js +4 -3
  120. package/dist/runner/test-function.d.ts +13 -1
  121. package/dist/runner/test-info.js +2 -1
  122. package/dist/runner/test-runner.d.ts +1 -0
  123. package/dist/runner/test-runner.js +62 -18
  124. package/dist/runner/use-options.d.ts +4 -4
  125. package/dist/runner/use-options.js +10 -3
  126. package/dist/runner/wait-for-url.d.ts +20 -0
  127. package/dist/runner/wait-for-url.js +65 -0
  128. package/dist/runner/web-server.d.ts +16 -0
  129. package/dist/runner/web-server.js +165 -0
  130. package/dist/runner/worker-pool.d.ts +13 -5
  131. package/dist/runner/worker-pool.js +85 -51
  132. package/dist/runner/worker-protocol.d.ts +8 -0
  133. package/dist/runner/worker-session.js +3 -1
  134. package/dist/runner/worker.d.ts +2 -0
  135. package/dist/runner/worker.js +7 -1
  136. package/dist/types/config.d.ts +83 -0
  137. package/dist/types/test.d.ts +18 -0
  138. package/dist/types/use.d.ts +45 -7
  139. package/package.json +1 -1
  140. package/dist/browser/emulation.d.ts +0 -61
  141. package/dist/browser/emulation.js +0 -180
@@ -1,7 +1,7 @@
1
1
  import path from "node:path";
2
2
  import { takePendingAssertions } from "../assert/expect.js";
3
3
  import { Browser } from "../browser/browser.js";
4
- import { Evidence } from "../browser/evidence.js";
4
+ import { cleanName, Evidence } from "../browser/evidence.js";
5
5
  import { projectDir, readConfig } from "../config/config.js";
6
6
  import logger from "../logger/index.js";
7
7
  import { ConnectionClosedError } from "../transport/bidi-connection.js";
@@ -18,6 +18,7 @@ import { TestTimeout } from "./test-timeout.js";
18
18
  import { ioOfAttempt, missingOutputs } from "./test-io.js";
19
19
  import { buildTestFixtures } from "./fixtures.js";
20
20
  import { FixtureScope } from "./fixture-scope.js";
21
+ import { RequestFixture } from "./request-fixture.js";
21
22
  import { effectiveUse } from "./use-options.js";
22
23
  import { beginOutputs, endOutputs } from "../wait/output-sink.js";
23
24
  /**
@@ -84,6 +85,7 @@ export async function runOneTest(test, browserName, run, options, srcDir, report
84
85
  let browser;
85
86
  let page;
86
87
  let customFixtures;
88
+ let requestFixture;
87
89
  let onRunAborted;
88
90
  const timeoutController = new AbortController();
89
91
  let browserGone = () => { };
@@ -142,7 +144,7 @@ export async function runOneTest(test, browserName, run, options, srcDir, report
142
144
  const applied = Object.keys(use).length > 0
143
145
  ? await launched.browser.setUse(use, projectDir())
144
146
  : undefined;
145
- const testFolder = `${browserName}/${testDirOf(test, srcDir)}${resources.attempt ? `/retry-${resources.attempt}` : ""}`;
147
+ const testFolder = `${browserName}${test.group === undefined ? "" : `/${cleanName(test.group)}`}/${testDirOf(test, srcDir)}${resources.attempt ? `/retry-${resources.attempt}` : ""}`;
146
148
  outcome.evidence = new Evidence(resultDir, testFolder);
147
149
  launched.page.setEvidence(outcome.evidence);
148
150
  const info = new TestInfoImpl({
@@ -157,7 +159,9 @@ export async function runOneTest(test, browserName, run, options, srcDir, report
157
159
  });
158
160
  outcome.info = info;
159
161
  applyDeclaredMarks(info, test);
162
+ requestFixture = new RequestFixture(settings.baseURL, use);
160
163
  const built = buildTestFixtures({
164
+ request: requestFixture,
161
165
  info,
162
166
  launched,
163
167
  browserName,
@@ -246,6 +250,7 @@ export async function runOneTest(test, browserName, run, options, srcDir, report
246
250
  const late = await customFixtures?.teardown(timeout);
247
251
  if (late)
248
252
  logger.debug("A fixture failed after the test ended", { late });
253
+ await requestFixture?.dispose();
249
254
  endOutputs();
250
255
  }
251
256
  testTimeout?.stop();
@@ -48,6 +48,7 @@ export declare function listGroups(options?: RunTestsOptions): Promise<ResolvedG
48
48
  export { BROWSER_NAMES, checkBrowserList } from "../config/browsers.js";
49
49
  export { parseWorkers } from "../config/workers.js";
50
50
  export { parseRetries } from "../config/retries.js";
51
+ export { parseShard, type Shard } from "./shard.js";
51
52
  export type { RunEvent } from "./reporter.js";
52
53
  export type { Modifier, TestError, TestResult, TestSummary, } from "../types/test.js";
53
54
  export type { ResolvedGroup } from "./groups.js";
@@ -70,4 +70,5 @@ export async function listGroups(options = {}) {
70
70
  export { BROWSER_NAMES, checkBrowserList } from "../config/browsers.js";
71
71
  export { parseWorkers } from "../config/workers.js";
72
72
  export { parseRetries } from "../config/retries.js";
73
+ export { parseShard } from "./shard.js";
73
74
  export { DEFAULT_PORT } from "./ports.js";
@@ -15,10 +15,21 @@ export declare function isSelected(name: string, { grep, testNames }: Pick<Runne
15
15
  export declare function idOf(test: RegisteredTestCase, srcDir: string): string;
16
16
  /** Imports the spec files once and resolves the config's groups against what they registered */
17
17
  export declare function loadSpecs(testFiles: string[]): Promise<LoadedSpecs>;
18
- /** The tests the options select: by `group`, `testNames` and `grep` */
18
+ /** The group names the options select, once each and in the order given; none when no group is selected */
19
+ export declare function groupNamesOf(group: RunnerOptions["group"]): string[];
20
+ /** The tests the options select: by `group` (in any of them), `testNames` and `grep`; each test once */
19
21
  export declare function selectTests({ all, groups, srcDir }: LoadedSpecs, options: RunnerOptions): RegisteredTestCase[];
20
22
  /**
21
- * The browsers the run uses: the options' (`--browser`), else the selected group's, else the project's, else
22
- * Firefox
23
+ * The runs of the selected tests: without a selected group, the tests as they are; else one copy per selected group
24
+ * that holds the test (its `group` set), the first group's tests first
23
25
  */
24
- export declare function browsersOfRun(groups: ResolvedGroup[], options: RunnerOptions): Promise<SupportedBrowser[]>;
26
+ export declare function expandByGroup(selected: RegisteredTestCase[], { groups, srcDir }: LoadedSpecs, options: RunnerOptions): RegisteredTestCase[];
27
+ /**
28
+ * The browsers a run of a test uses: the options' (`--browser`), else its group's, else the project's, else Firefox
29
+ */
30
+ export declare function browserPlan(groups: ResolvedGroup[], options: RunnerOptions): Promise<{
31
+ /** Every browser of the run, in the order they run */
32
+ browsers: SupportedBrowser[];
33
+ /** The browsers one run of a test uses */
34
+ browsersOf(test: RegisteredTestCase): SupportedBrowser[];
35
+ }>;
@@ -53,25 +53,51 @@ export async function loadSpecs(testFiles) {
53
53
  const all = await registerAll(testFiles);
54
54
  return { all, ...(await resolveAll(all)) };
55
55
  }
56
- /** The tests the options select: by `group`, `testNames` and `grep` */
56
+ /** The group names the options select, once each and in the order given; none when no group is selected */
57
+ export function groupNamesOf(group) {
58
+ return [...new Set(group === undefined ? [] : [group].flat())];
59
+ }
60
+ /** The tests the options select: by `group` (in any of them), `testNames` and `grep`; each test once */
57
61
  export function selectTests({ all, groups, srcDir }, options) {
58
- const { group } = options;
62
+ const names = groupNamesOf(options.group);
59
63
  let inGroup = () => true;
60
- if (group !== undefined) {
61
- const members = new Set(findGroup(groups, group).testIds);
64
+ if (names.length > 0) {
65
+ const members = new Set(names.flatMap((name) => findGroup(groups, name).testIds));
62
66
  inGroup = (test) => members.has(idOf(test, srcDir));
63
67
  }
64
68
  return all.filter((test) => inGroup(test) && isSelected(test.name, options));
65
69
  }
66
70
  /**
67
- * The browsers the run uses: the options' (`--browser`), else the selected group's, else the project's, else
68
- * Firefox
71
+ * The runs of the selected tests: without a selected group, the tests as they are; else one copy per selected group
72
+ * that holds the test (its `group` set), the first group's tests first
73
+ */
74
+ export function expandByGroup(selected, { groups, srcDir }, options) {
75
+ const names = groupNamesOf(options.group);
76
+ if (names.length === 0)
77
+ return selected;
78
+ return names.flatMap((name) => {
79
+ const members = new Set(findGroup(groups, name).testIds);
80
+ return selected
81
+ .filter((test) => members.has(idOf(test, srcDir)))
82
+ .map((test) => ({ ...test, group: name }));
83
+ });
84
+ }
85
+ /**
86
+ * The browsers a run of a test uses: the options' (`--browser`), else its group's, else the project's, else Firefox
69
87
  */
70
- export async function browsersOfRun(groups, options) {
71
- const { browsers: flag, group } = options;
88
+ export async function browserPlan(groups, options) {
89
+ const { browsers: flag } = options;
72
90
  const config = await loadRawConfig();
73
- const ofGroup = group !== undefined && !(flag && flag.length > 0)
74
- ? findGroup(groups, group).browsers
75
- : undefined;
76
- return browsersFor({ flag, group: ofGroup, project: config.browsers });
91
+ const names = groupNamesOf(options.group);
92
+ const flagged = flag && flag.length > 0;
93
+ const ofGroup = new Map(names.map((name) => [name, findGroup(groups, name).browsers]));
94
+ const project = browsersFor({ flag, project: config.browsers });
95
+ const forGroup = (name) => flagged ? project : (ofGroup.get(name) ?? project);
96
+ const browsers = [
97
+ ...new Set(names.length > 0 ? names.flatMap(forGroup) : project),
98
+ ];
99
+ return {
100
+ browsers,
101
+ browsersOf: (test) => test.group === undefined ? project : forGroup(test.group),
102
+ };
77
103
  }
@@ -0,0 +1,23 @@
1
+ import type { SupportedBrowser } from "../types/browser.js";
2
+ import type { RegisteredTestCase } from "../types/test.js";
3
+ import type { TestSink } from "./reporter.js";
4
+ /** One attempt at one test of the block, reporting to `sink` */
5
+ export type BlockTest = (test: RegisteredTestCase, sink: TestSink, attempt: number) => Promise<void>;
6
+ export interface SerialBlockRun {
7
+ tests: RegisteredTestCase[];
8
+ browser: SupportedBrowser;
9
+ retries: number;
10
+ signal: AbortSignal | undefined;
11
+ runTest: BlockTest;
12
+ /** Called before the block starts again after a failure: closes what the block's tests share (`afterAll`) */
13
+ restart(): Promise<void>;
14
+ sink: TestSink;
15
+ }
16
+ /**
17
+ * Runs the tests of a serial block in order. After a failure the rest of the block is reported as skipped; with
18
+ * `retries`, the whole block then runs again from its first test (after `restart`), up to `retries` times.
19
+ * Without retries every test is reported as it ends. With them the ends are held until the block's last attempt is
20
+ * known, as `runWithRetries` does for one test: each test ends once, with `retry` saying which attempt that was.
21
+ * An aborted run is never run again.
22
+ */
23
+ export declare function serialBlockRun(run: SerialBlockRun): Promise<void>;
@@ -0,0 +1,88 @@
1
+ import { SERIAL_SKIP_REASON } from "./serial.js";
2
+ import { staticSkip } from "./skip.js";
3
+ /**
4
+ * Runs the tests of a serial block in order. After a failure the rest of the block is reported as skipped; with
5
+ * `retries`, the whole block then runs again from its first test (after `restart`), up to `retries` times.
6
+ * Without retries every test is reported as it ends. With them the ends are held until the block's last attempt is
7
+ * known, as `runWithRetries` does for one test: each test ends once, with `retry` saying which attempt that was.
8
+ * An aborted run is never run again.
9
+ */
10
+ export async function serialBlockRun(run) {
11
+ const { tests, browser, retries, signal, sink } = run;
12
+ const live = retries === 0;
13
+ const previous = tests.map(() => []);
14
+ for (let attempt = 0;; attempt++) {
15
+ const ended = [];
16
+ let failedAt = -1;
17
+ for (const [at, test] of tests.entries()) {
18
+ const skip = staticSkip(test);
19
+ if (skip) {
20
+ sink.onTestSkipped(test, browser, skip);
21
+ continue;
22
+ }
23
+ if (signal?.aborted) {
24
+ for (const rest of tests.slice(at)) {
25
+ if (staticSkip(rest))
26
+ continue;
27
+ sink.onTestStart(rest, browser);
28
+ sink.onTestEnd(rest, browser, {
29
+ message: "Run aborted",
30
+ type: "error",
31
+ });
32
+ }
33
+ return;
34
+ }
35
+ const attemptSink = {
36
+ onTestStart: (t, b) => sink.onTestStart(t, b, attempt > 0 ? attempt : undefined),
37
+ onTestEnd: (t, b, error, extras) => {
38
+ ended[at] = { error, extras };
39
+ if (live)
40
+ sink.onTestEnd(t, b, error, extras);
41
+ },
42
+ onTestSkipped: (t, b, why) => sink.onTestSkipped(t, b, why),
43
+ };
44
+ await run.runTest(test, attemptSink, attempt);
45
+ if (ended[at]?.error) {
46
+ failedAt = at;
47
+ break;
48
+ }
49
+ }
50
+ const error = ended[failedAt]?.error;
51
+ if (error &&
52
+ attempt < retries &&
53
+ !signal?.aborted &&
54
+ error.message !== "Run aborted") {
55
+ previous[failedAt].push(error);
56
+ await run.restart();
57
+ continue;
58
+ }
59
+ if (!live)
60
+ flush(run, ended, previous, attempt);
61
+ if (failedAt >= 0)
62
+ for (const rest of tests.slice(failedAt + 1)) {
63
+ const skip = staticSkip(rest) ?? {
64
+ annotation: "skip",
65
+ reason: SERIAL_SKIP_REASON,
66
+ };
67
+ sink.onTestSkipped(rest, browser, skip);
68
+ }
69
+ return;
70
+ }
71
+ }
72
+ /** Reports the held ends of the last attempt, each saying which attempt it was and what failed before */
73
+ function flush({ tests, browser, sink }, ended, previous, attempt) {
74
+ for (const [at, test] of tests.entries()) {
75
+ const end = ended[at];
76
+ if (!end)
77
+ continue;
78
+ const before = previous[at];
79
+ sink.onTestEnd(test, browser, end.error, {
80
+ ...end.extras,
81
+ ...(attempt > 0 && {
82
+ retry: attempt,
83
+ previousAttempts: [...before],
84
+ ...(!end.error && before.length > 0 && { flaky: true }),
85
+ }),
86
+ });
87
+ }
88
+ }
@@ -0,0 +1,34 @@
1
+ import type { RegisteredTestCase, Suite } from "../types/test.js";
2
+ /** Why a test of a serial block did not run: an earlier test of the block failed */
3
+ export declare const SERIAL_SKIP_REASON = "serial: an earlier test failed";
4
+ /** `describe.configure(options)`: sets the mode of the suite the call is written in */
5
+ export declare function configureSuite(suite: Suite, options: unknown): void;
6
+ /** A group of tests that run in order on one worker; if one fails, the rest of the block is skipped */
7
+ export interface SerialBlock {
8
+ /** The file and the `describe` titles of the block's outermost serial suite; the same in every process */
9
+ key: string;
10
+ /** The outermost suite in serial mode */
11
+ suite: Suite;
12
+ }
13
+ /** The serial block a test belongs to: the outermost suite around it in `"serial"` mode, else none */
14
+ export declare function serialBlockOf(test: RegisteredTestCase): SerialBlock | undefined;
15
+ /** The tests that must run one after the other on one worker, in the order they are listed */
16
+ export interface RunUnit {
17
+ /** The same for every test of the unit, in every process */
18
+ key: string;
19
+ /** A serial block: a failure skips the rest and a retry runs the whole block again */
20
+ serial: boolean;
21
+ }
22
+ /**
23
+ * The unit a test runs in. A serial block is one. With `fullyParallel: false` every other test is in the unit of
24
+ * its file, unless a suite around it is in `"parallel"` mode. Otherwise (`true`, or not set: the tests of a file
25
+ * go to workers one by one) there is no unit.
26
+ */
27
+ export declare function unitOf(test: RegisteredTestCase, fullyParallel: boolean | undefined): RunUnit | undefined;
28
+ /** Tests that follow each other and belong to one serial block (`block`), or one test outside any block */
29
+ export interface Chunk {
30
+ block?: SerialBlock;
31
+ tests: RegisteredTestCase[];
32
+ }
33
+ /** Splits a list of tests into serial blocks and the free tests between them, keeping the order */
34
+ export declare function chunkBySerialBlock(tests: RegisteredTestCase[]): Chunk[];
@@ -0,0 +1,63 @@
1
+ import { suiteChain } from "./suite.js";
2
+ /** Why a test of a serial block did not run: an earlier test of the block failed */
3
+ export const SERIAL_SKIP_REASON = "serial: an earlier test failed";
4
+ const MODES = ["default", "parallel", "serial"];
5
+ /** `describe.configure(options)`: sets the mode of the suite the call is written in */
6
+ export function configureSuite(suite, options) {
7
+ if (typeof options !== "object" || options === null)
8
+ throw new TypeError("describe.configure(): expected an options object like { mode: 'serial' }");
9
+ const { mode, ...rest } = options;
10
+ const unknown = Object.keys(rest);
11
+ if (unknown.length > 0)
12
+ throw new TypeError(`describe.configure(): unknown option "${unknown[0]}" (only mode is supported)`);
13
+ if (!MODES.includes(mode))
14
+ throw new TypeError(`describe.configure(): mode must be "default", "parallel" or "serial", got ${JSON.stringify(mode)}`);
15
+ if (mode === "default")
16
+ delete suite.mode;
17
+ else
18
+ suite.mode = mode;
19
+ }
20
+ const groupSuffix = (test) => test.group === undefined ? "" : `@${test.group}`;
21
+ /** The serial block a test belongs to: the outermost suite around it in `"serial"` mode, else none */
22
+ export function serialBlockOf(test) {
23
+ const suite = suiteChain(test.suite).find((s) => s.mode === "serial");
24
+ if (!suite)
25
+ return undefined;
26
+ const titles = suiteChain(suite).flatMap((s) => s.title === undefined ? [] : [s.title]);
27
+ // The same block run for two groups is two blocks
28
+ return {
29
+ key: `serial:${test.file}::${titles.join(" > ")}${groupSuffix(test)}`,
30
+ suite,
31
+ };
32
+ }
33
+ /**
34
+ * The unit a test runs in. A serial block is one. With `fullyParallel: false` every other test is in the unit of
35
+ * its file, unless a suite around it is in `"parallel"` mode. Otherwise (`true`, or not set: the tests of a file
36
+ * go to workers one by one) there is no unit.
37
+ */
38
+ export function unitOf(test, fullyParallel) {
39
+ const block = serialBlockOf(test);
40
+ if (block)
41
+ return { key: block.key, serial: true };
42
+ if (fullyParallel !== false)
43
+ return undefined;
44
+ const nearest = suiteChain(test.suite)
45
+ .reverse()
46
+ .find((s) => s.mode === "parallel");
47
+ return nearest
48
+ ? undefined
49
+ : { key: `file:${test.file}${groupSuffix(test)}`, serial: false };
50
+ }
51
+ /** Splits a list of tests into serial blocks and the free tests between them, keeping the order */
52
+ export function chunkBySerialBlock(tests) {
53
+ const chunks = [];
54
+ for (const test of tests) {
55
+ const block = serialBlockOf(test);
56
+ const last = chunks[chunks.length - 1];
57
+ if (block && last?.block?.key === block.key)
58
+ last.tests.push(test);
59
+ else
60
+ chunks.push({ ...(block && { block }), tests: [test] });
61
+ }
62
+ return chunks;
63
+ }
@@ -0,0 +1,22 @@
1
+ import type { RegisteredTestCase } from "../types/test.js";
2
+ /** One slice of a run split over several machines: slice `current` of `total`, counting from 1 */
3
+ export interface Shard {
4
+ current: number;
5
+ total: number;
6
+ }
7
+ /**
8
+ * Reads a shard: `"2/4"` (the command line's `--shard`) or `{ current: 2, total: 4 }` (the `shard` run option).
9
+ * `where` names the source in the error.
10
+ */
11
+ export declare function parseShard(value: unknown, where: string): Shard;
12
+ /**
13
+ * What a shard keeps together: a serial block is never split, and so is a file, unless `fullyParallel` is on or a
14
+ * `describe` around the test is in `"parallel"` mode: then the test goes by itself.
15
+ */
16
+ export declare function shardGroupOf(test: RegisteredTestCase, fullyParallel: boolean | undefined): string;
17
+ /**
18
+ * The tests of one shard. The groups (in the order their first test comes) are dealt out in order, so the shards are
19
+ * as equal in tests as whole groups allow: a group goes to the shard that holds its first test when all tests are
20
+ * cut into `total` equal ranges. The shards are disjoint and together hold every test; the order stays.
21
+ */
22
+ export declare function shardTests(tests: RegisteredTestCase[], { current, total }: Shard, groupOf: (test: RegisteredTestCase) => string): RegisteredTestCase[];
@@ -0,0 +1,73 @@
1
+ import { serialBlockOf } from "./serial.js";
2
+ import { suiteChain } from "./suite.js";
3
+ /**
4
+ * Reads a shard: `"2/4"` (the command line's `--shard`) or `{ current: 2, total: 4 }` (the `shard` run option).
5
+ * `where` names the source in the error.
6
+ */
7
+ export function parseShard(value, where) {
8
+ const shard = shardOf(value);
9
+ if (shard &&
10
+ Number.isInteger(shard.current) &&
11
+ Number.isInteger(shard.total) &&
12
+ shard.current >= 1 &&
13
+ shard.current <= shard.total)
14
+ return { current: shard.current, total: shard.total };
15
+ const shown = typeof value === "string" ? value : JSON.stringify(value);
16
+ throw new Error(`${where}: expected current/total with 1 <= current <= total, got ${typeof value === "string" ? `"${shown}"` : String(shown)}`);
17
+ }
18
+ function shardOf(value) {
19
+ if (typeof value === "string") {
20
+ const match = /^\s*(\d+)\s*\/\s*(\d+)\s*$/.exec(value);
21
+ return match
22
+ ? { current: Number(match[1]), total: Number(match[2]) }
23
+ : undefined;
24
+ }
25
+ if (typeof value === "object" && value !== null) {
26
+ const { current, total } = value;
27
+ if (typeof current === "number" && typeof total === "number")
28
+ return { current, total };
29
+ }
30
+ return undefined;
31
+ }
32
+ /**
33
+ * What a shard keeps together: a serial block is never split, and so is a file, unless `fullyParallel` is on or a
34
+ * `describe` around the test is in `"parallel"` mode: then the test goes by itself.
35
+ */
36
+ export function shardGroupOf(test, fullyParallel) {
37
+ const block = serialBlockOf(test);
38
+ if (block)
39
+ return block.key;
40
+ const alone = fullyParallel === true ||
41
+ suiteChain(test.suite).some((s) => s.mode === "parallel");
42
+ return alone ? `test:${test.file}::${test.name}` : `file:${test.file}`;
43
+ }
44
+ /**
45
+ * The tests of one shard. The groups (in the order their first test comes) are dealt out in order, so the shards are
46
+ * as equal in tests as whole groups allow: a group goes to the shard that holds its first test when all tests are
47
+ * cut into `total` equal ranges. The shards are disjoint and together hold every test; the order stays.
48
+ */
49
+ export function shardTests(tests, { current, total }, groupOf) {
50
+ const groups = new Map();
51
+ for (const test of tests) {
52
+ const key = groupOf(test);
53
+ const members = groups.get(key);
54
+ if (members)
55
+ members.push(test);
56
+ else
57
+ groups.set(key, [test]);
58
+ }
59
+ const size = Math.floor(tests.length / total);
60
+ const extra = tests.length - size * total;
61
+ const index = current - 1;
62
+ const from = size * index + Math.min(extra, index);
63
+ const to = from + size + (index < extra ? 1 : 0);
64
+ const kept = new Set();
65
+ let first = 0;
66
+ for (const members of groups.values()) {
67
+ if (first >= from && first < to)
68
+ for (const test of members)
69
+ kept.add(test);
70
+ first += members.length;
71
+ }
72
+ return tests.filter((test) => kept.has(test));
73
+ }
@@ -1,12 +1,12 @@
1
- import type { EachHook, Modifier, Suite } from "../types/test.js";
1
+ import type { EachHook, Modifier, Suite, SuiteMode } from "../types/test.js";
2
2
  import type { UseOptions } from "../types/use.js";
3
3
  /** The four hook lists a suite can hold */
4
4
  export type HookKind = "beforeEach" | "afterEach" | "beforeAll" | "afterAll";
5
- export declare function newSuite(title?: string, parent?: Suite, modifier?: Modifier, reason?: string): Suite;
5
+ export declare function newSuite(title?: string, parent?: Suite, modifier?: Modifier, reason?: string, mode?: SuiteMode): Suite;
6
6
  /** The suite a test or hook written now belongs to: the innermost open `describe`, else the file's own */
7
7
  export declare function currentSuite(file: string): Suite;
8
8
  /** Opens a `describe` block under the current suite; `leaveSuite` closes it */
9
- export declare function enterSuite(file: string, title: string, modifier?: Modifier, reason?: string): Suite;
9
+ export declare function enterSuite(file: string, title: string, modifier?: Modifier, reason?: string, mode?: SuiteMode): Suite;
10
10
  export declare function leaveSuite(): void;
11
11
  /** Forgets every suite: a run registers its specs afresh */
12
12
  export declare function resetSuites(): void;
@@ -2,12 +2,13 @@
2
2
  const roots = new Map();
3
3
  /** The `describe` blocks being registered, outermost first */
4
4
  const open = [];
5
- export function newSuite(title, parent, modifier, reason) {
5
+ export function newSuite(title, parent, modifier, reason, mode) {
6
6
  return {
7
7
  ...(title !== undefined && { title }),
8
8
  ...(parent && { parent }),
9
9
  ...(modifier && { modifier }),
10
10
  ...(reason !== undefined && { reason }),
11
+ ...(mode && { mode }),
11
12
  };
12
13
  }
13
14
  /** The suite a test or hook written now belongs to: the innermost open `describe`, else the file's own */
@@ -23,8 +24,8 @@ export function currentSuite(file) {
23
24
  return root;
24
25
  }
25
26
  /** Opens a `describe` block under the current suite; `leaveSuite` closes it */
26
- export function enterSuite(file, title, modifier, reason) {
27
- const suite = newSuite(title, currentSuite(file), modifier, reason);
27
+ export function enterSuite(file, title, modifier, reason, mode) {
28
+ const suite = newSuite(title, currentSuite(file), modifier, reason, mode);
28
29
  open.push(suite);
29
30
  return suite;
30
31
  }
@@ -1,4 +1,4 @@
1
- import type { AllHook, TestCaseBase, TestDetails, TestInfo, TestOptions } from "../types/test.js";
1
+ import type { AllHook, SuiteMode, TestCaseBase, TestDetails, TestInfo, TestOptions } from "../types/test.js";
2
2
  import type { UseOptions } from "../types/use.js";
3
3
  import type { FixtureDefinitions, FixturesOf, MergeFixtures } from "../types/extend.js";
4
4
  /** What the registry keeps of a test body; the fixtures it gets are those of the test it was written for */
@@ -86,6 +86,18 @@ export interface DescribeFunction {
86
86
  skip: DescribeSkipFunction;
87
87
  fixme: DescribeSkipFunction;
88
88
  only(title: string, fn: () => void): void;
89
+ /**
90
+ * Sets how the tests of the `describe` block it is written in (of the file, at the top level) run:
91
+ * `"serial"` runs them in order on one worker and skips the rest after a failure, `"parallel"` lets them run on
92
+ * different workers even when the config does not, `"default"` follows the config's `fullyParallel`
93
+ */
94
+ configure(options: {
95
+ mode: SuiteMode;
96
+ }): void;
97
+ /** A `describe` block in `"serial"` mode */
98
+ serial(title: string, fn: () => void): void;
99
+ /** A `describe` block in `"parallel"` mode */
100
+ parallel(title: string, fn: () => void): void;
89
101
  }
90
102
  /** `test.fail()` and `test.slow()`: with a condition (a function gets the test's fixtures), or none for "always" */
91
103
  export interface ConditionFunction<X> {
@@ -26,7 +26,8 @@ export class TestInfoImpl {
26
26
  this.column = test.column;
27
27
  this.retry = from.retry;
28
28
  this.workerIndex = from.workerIndex;
29
- this.project = { name: from.browserName };
29
+ // A run for a group is named after it (as a Playwright project is), else after the browser
30
+ this.project = { name: test.group ?? from.browserName };
30
31
  this.config = { ...from.settings };
31
32
  this.resultDir = from.resultDir;
32
33
  this.outputDir = from.outputDir;
@@ -39,6 +39,7 @@ export default class TestRunner {
39
39
  * was also written to the report. With more than one worker the tests run at the same time in worker processes.
40
40
  */
41
41
  start(): Promise<import("../types/test.js").TestSummary>;
42
+ private execute;
42
43
  /** Whether anything but the default says how many workers to use */
43
44
  private workersAsked;
44
45
  /** The pool of a run: up to `workers` worker processes that read the same project and settings as this run */