@itmetsam/samurai-framework 0.16.0 → 0.17.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.
@@ -25,8 +25,12 @@ export declare const CAPABILITIES: {
25
25
  readonly testIds: true;
26
26
  /** `loadConfig` from `./project`: the config with every default applied, the way a run uses it. */
27
27
  readonly resolvedConfig: true;
28
+ /** `loadConfig({ written: true })`: the config as written next to the resolved one, and `writtenConfig`. */
29
+ readonly writtenConfig: true;
28
30
  /** The recorder's first `onStatus` call, `{ state: "recording", toolbar }`, right after it starts, and a `RecordResult` with every field always present. */
29
31
  readonly recorderStartStatus: true;
32
+ /** `outputDir` on `runTests` and `runFlowInProject`: a folder per run for the report and the screenshots. */
33
+ readonly outputDir: true;
30
34
  };
31
35
  /** The shape of {@link CAPABILITIES}. */
32
36
  export type Capabilities = typeof CAPABILITIES;
@@ -25,6 +25,10 @@ export const CAPABILITIES = {
25
25
  testIds: true,
26
26
  /** `loadConfig` from `./project`: the config with every default applied, the way a run uses it. */
27
27
  resolvedConfig: true,
28
+ /** `loadConfig({ written: true })`: the config as written next to the resolved one, and `writtenConfig`. */
29
+ writtenConfig: true,
28
30
  /** The recorder's first `onStatus` call, `{ state: "recording", toolbar }`, right after it starts, and a `RecordResult` with every field always present. */
29
31
  recorderStartStatus: true,
32
+ /** `outputDir` on `runTests` and `runFlowInProject`: a folder per run for the report and the screenshots. */
33
+ outputDir: true,
30
34
  };
@@ -21,6 +21,8 @@ export declare function browsersDir(): string;
21
21
  export declare function validateConfig(config: SamuraiTestConfig): void;
22
22
  /** The whole `samurai.config.ts` of the project (the working directory unless a project is active) */
23
23
  export declare function loadRawConfig(): Promise<SamuraiTestConfig>;
24
+ /** The default export of `samurai.config.ts` in `dir`, not validated */
25
+ export declare function importConfigFile(dir: string): Promise<SamuraiTestConfig>;
24
26
  /** A project's config without keeping the project active (`withProject` is one at a time): the given one, else its samurai.config.ts */
25
27
  export declare function projectConfig(options: {
26
28
  projectDir?: string;
@@ -37,13 +37,18 @@ export async function loadRawConfig() {
37
37
  validateConfig(activeProject.config);
38
38
  return activeProject.config;
39
39
  }
40
- const configUrl = path.join(projectDir(), "samurai.config.ts");
40
+ const config = await importConfigFile(projectDir());
41
+ validateConfig(config);
42
+ return config;
43
+ }
44
+ /** The default export of `samurai.config.ts` in `dir`, not validated */
45
+ export async function importConfigFile(dir) {
46
+ const configUrl = path.join(dir, "samurai.config.ts");
41
47
  logger.debug("Reading config from: %s", configUrl);
42
48
  const config = (await import(pathToFileURL(configUrl).href));
43
49
  if (!config.default) {
44
50
  throw new Error("samurai.config.ts does not export defineConfig as default!");
45
51
  }
46
- validateConfig(config.default);
47
52
  return config.default;
48
53
  }
49
54
  /** A project's config without keeping the project active (`withProject` is one at a time): the given one, else its samurai.config.ts */
@@ -91,6 +91,26 @@ export declare function resolveConfig(config: SamuraiTestConfig, options: {
91
91
  projectDir: string;
92
92
  env: Env;
93
93
  }): ResolvedConfig;
94
+ /**
95
+ * `samurai.config.ts` as the file states it, as JSON: nothing filled in (an absent `workers` stays absent, a config
96
+ * without `environments` has none), `logs.ignoreErrors` patterns as {@link ResolvedIgnoreError}, and `steps` as
97
+ * their kinds. For showing and editing the config; {@link ResolvedConfig} is what a run uses.
98
+ */
99
+ export type WrittenConfig = Omit<SamuraiTestConfig, "logs" | "steps"> & {
100
+ logs?: Omit<NonNullable<SamuraiTestConfig["logs"]>, "ignoreErrors"> & {
101
+ ignoreErrors?: ResolvedIgnoreError[];
102
+ };
103
+ steps?: string[];
104
+ };
105
+ /** Both views of a config: as written, and resolved; `resolved` is absent, and `error` says why, when the config is invalid */
106
+ export interface ConfigViews {
107
+ written: WrittenConfig;
108
+ resolved?: ResolvedConfig;
109
+ /** The error a run would throw for this config */
110
+ error?: string;
111
+ }
112
+ /** The config as written, as plain JSON (see {@link WrittenConfig}). Pure: reads no files. */
113
+ export declare function writtenConfig(config: SamuraiTestConfig): WrittenConfig;
94
114
  /**
95
115
  * A project's `samurai.config.ts` with every default applied, the way a run uses it (see {@link ResolvedConfig}).
96
116
  * Leaves no project active. The caller registers `tsx` first (the framework does not), so the config file can be
@@ -98,9 +118,17 @@ export declare function resolveConfig(config: SamuraiTestConfig, options: {
98
118
  * (Node caches it): a later edit to `samurai.config.ts` is seen only in a new process.
99
119
  * @param options.projectDir The project folder. @default the working directory
100
120
  * @param options.env Where `CI` is read from. @default `process.env`
121
+ * @param options.written Also return the config as written (see {@link ConfigViews})
101
122
  */
102
123
  export declare function loadConfig(options?: {
103
124
  projectDir?: string;
104
125
  env?: Env;
126
+ written?: false;
105
127
  }): Promise<ResolvedConfig>;
128
+ /** With `written: true`: the config as written next to the resolved one (see {@link ConfigViews}); an invalid config is reported in `error`, not thrown */
129
+ export declare function loadConfig(options: {
130
+ projectDir?: string;
131
+ env?: Env;
132
+ written: true;
133
+ }): Promise<ConfigViews>;
106
134
  export {};
@@ -1,6 +1,6 @@
1
1
  import path from "node:path";
2
2
  import { browsersFor } from "./browsers-for.js";
3
- import { projectConfig, validateConfig } from "./config.js";
3
+ import { importConfigFile, projectConfig, validateConfig } from "./config.js";
4
4
  import { DEFAULT_COLLECT_BODIES, DEFAULT_COMMAND_TIMEOUT, DEFAULT_DOWNLOADS_DIR, DEFAULT_FAIL_ON_PAGE_ERROR, DEFAULT_FILES_DIR, DEFAULT_IDLE_TIME, DEFAULT_LOGS_CAPTURE, DEFAULT_MAX_BODY_SIZE, DEFAULT_NAVIGATION_TIMEOUT, DEFAULT_RETRIES, DEFAULT_ROUTE_TIMEOUT, DEFAULT_SRC_DIR, DEFAULT_TRACK_NETWORK, DEFAULT_VIEWPORT, DEFAULT_WAIT_UNTIL, } from "./defaults.js";
5
5
  import { forbidsOnly } from "./forbid-only.js";
6
6
  import { IMPLICIT_ENVIRONMENT, resolveRunSettings, UnknownEnvironmentError, } from "./run-settings.js";
@@ -97,16 +97,39 @@ export function resolveConfig(config, options) {
97
97
  steps: (config.steps ?? []).map((step) => step.kind),
98
98
  };
99
99
  }
100
- /**
101
- * A project's `samurai.config.ts` with every default applied, the way a run uses it (see {@link ResolvedConfig}).
102
- * Leaves no project active. The caller registers `tsx` first (the framework does not), so the config file can be
103
- * imported; an app does that in the child process that calls this. The config module is imported once per process
104
- * (Node caches it): a later edit to `samurai.config.ts` is seen only in a new process.
105
- * @param options.projectDir The project folder. @default the working directory
106
- * @param options.env Where `CI` is read from. @default `process.env`
107
- */
100
+ /** The config as written, as plain JSON (see {@link WrittenConfig}). Pure: reads no files. */
101
+ export function writtenConfig(config) {
102
+ const { logs, steps, ...rest } = config;
103
+ const { ignoreErrors, ...logSettings } = logs ?? {};
104
+ const written = {
105
+ ...rest,
106
+ ...(logs && {
107
+ logs: {
108
+ ...logSettings,
109
+ ...(ignoreErrors && { ignoreErrors: ignoreErrorsOf(ignoreErrors) }),
110
+ },
111
+ }),
112
+ ...(steps && { steps: steps.map((step) => step.kind) }),
113
+ };
114
+ // A JSON copy: shares nothing with the config, and leaves out what JSON can't carry
115
+ return JSON.parse(JSON.stringify(written));
116
+ }
108
117
  export async function loadConfig(options = {}) {
109
118
  const projectDir = path.resolve(options.projectDir ?? process.cwd());
110
- const config = await projectConfig({ projectDir });
111
- return resolveConfig(config, { projectDir, env: options.env ?? process.env });
119
+ const env = options.env ?? process.env;
120
+ if (!options.written) {
121
+ const config = await projectConfig({ projectDir });
122
+ return resolveConfig(config, { projectDir, env });
123
+ }
124
+ const config = await importConfigFile(projectDir);
125
+ const written = writtenConfig(config);
126
+ try {
127
+ return { written, resolved: resolveConfig(config, { projectDir, env }) };
128
+ }
129
+ catch (error) {
130
+ return {
131
+ written,
132
+ error: error instanceof Error ? error.message : String(error),
133
+ };
134
+ }
112
135
  }
@@ -20,6 +20,8 @@ export interface RunFlowInProjectOptions extends ProjectOptions {
20
20
  /** `"fail"` (default): a flow with a node that can't run is refused. `"skip"`: those nodes are skipped. */
21
21
  unsupported?: "fail" | "skip";
22
22
  port?: number;
23
+ /** The folder the tests' named screenshots go in, instead of `<project>/result` (see `runTests`). Relative to the project folder */
24
+ outputDir?: string;
23
25
  /**
24
26
  * How many worker processes the tests of the whole run share: a number, or a percentage like `"50%"`. Unset
25
27
  * takes `SAMURAI_WORKERS` (`workersEnv`), then the config's `workers`, then half the CPUs. 1 runs each Test
@@ -176,6 +176,9 @@ async function walkFlowInProject(options) {
176
176
  ...(options.headless !== undefined && {
177
177
  headless: options.headless,
178
178
  }),
179
+ ...(options.outputDir !== undefined && {
180
+ outputDir: options.outputDir,
181
+ }),
179
182
  files,
180
183
  basePort: options.port ?? DEFAULT_PORT,
181
184
  ...(retries > 0 && { retries }),
@@ -214,6 +217,9 @@ async function walkFlowInProject(options) {
214
217
  files: [...new Set(tests.map((t) => t.file))],
215
218
  testNames: tests.map((t) => t.name),
216
219
  reportPath: false,
220
+ ...(options.outputDir !== undefined && {
221
+ outputDir: options.outputDir,
222
+ }),
217
223
  signal,
218
224
  onEvent,
219
225
  });
@@ -2,4 +2,4 @@ export { ConfigEditError, editConfig, hasConfigPath, propertyText, type ConfigVa
2
2
  export { addVariable, ExistsError, VARIABLE_NAME } from "./variables.js";
3
3
  export { isGitIgnored, SECRET_NAME, setSecret } from "./secrets-file.js";
4
4
  export { cleanFileName, filesFolder, isFolderIgnored, RECORD_LIMIT, storeFile, } from "./files-store.js";
5
- export { loadConfig, type ResolvedConfig, type ResolvedEnvironment, type ResolvedIgnoreError, } from "../config/resolved-config.js";
5
+ export { loadConfig, writtenConfig, type ConfigViews, type ResolvedConfig, type ResolvedEnvironment, type ResolvedIgnoreError, type WrittenConfig, } from "../config/resolved-config.js";
@@ -2,4 +2,4 @@ export { ConfigEditError, editConfig, hasConfigPath, propertyText, } from "./con
2
2
  export { addVariable, ExistsError, VARIABLE_NAME } from "./variables.js";
3
3
  export { isGitIgnored, SECRET_NAME, setSecret } from "./secrets-file.js";
4
4
  export { cleanFileName, filesFolder, isFolderIgnored, RECORD_LIMIT, storeFile, } from "./files-store.js";
5
- export { loadConfig, } from "../config/resolved-config.js";
5
+ export { loadConfig, writtenConfig, } from "../config/resolved-config.js";
@@ -28,7 +28,13 @@ export interface RunnerOptions {
28
28
  headless?: boolean;
29
29
  /** Remote debugging port of the browser. @default DEFAULT_PORT (9223) */
30
30
  port?: number;
31
- /** Where the report is written; `false` writes none. @default `<project>/result/report.json` */
31
+ /**
32
+ * The folder for what the run writes, instead of `<project>/result`: `report.json` and the named screenshots
33
+ * (their paths in the results are relative to it). Relative to the project folder.
34
+ * @default `<project>/result`
35
+ */
36
+ outputDir?: string;
37
+ /** Where the report is written; `false` writes none. Beats `outputDir` for the report. @default `report.json` in `outputDir` */
32
38
  reportPath?: string | false;
33
39
  /** Receives start and end of the run and of every test, as they happen */
34
40
  onEvent?: (event: RunEvent) => void;
@@ -0,0 +1,6 @@
1
+ import type { RunnerOptions } from "./options.js";
2
+ /** Where a run writes: the folder its screenshots go in, and its report (`false`: none) */
3
+ export declare function runOutput(options: Pick<RunnerOptions, "outputDir" | "reportPath">, projectDir: string): {
4
+ resultDir: string;
5
+ report: string | false;
6
+ };
@@ -0,0 +1,14 @@
1
+ import path from "node:path";
2
+ /** Where a run writes: the folder its screenshots go in, and its report (`false`: none) */
3
+ export function runOutput(options, projectDir) {
4
+ const { outputDir, reportPath } = options;
5
+ const resultDir = outputDir !== undefined
6
+ ? path.resolve(projectDir, outputDir)
7
+ : typeof reportPath === "string"
8
+ ? path.dirname(reportPath)
9
+ : path.join(projectDir, "result");
10
+ return {
11
+ resultDir,
12
+ report: reportPath ?? path.join(resultDir, "report.json"),
13
+ };
14
+ }
@@ -1,4 +1,3 @@
1
- import path from "path";
2
1
  import { takePendingAssertions } from "../assert/expect.js";
3
2
  import { Browser } from "../browser/browser.js";
4
3
  import { Evidence } from "../browser/evidence.js";
@@ -11,6 +10,7 @@ import { judgePageLogs } from "./page-logs-report.js";
11
10
  import { DEFAULT_PORT } from "./ports.js";
12
11
  import { runWithEachHooks } from "./hooks.js";
13
12
  import { isSkipSignal, staticSkip } from "./skip.js";
13
+ import { runOutput } from "./output.js";
14
14
  import { testDirOf } from "./test-dir.js";
15
15
  import { toTestError } from "./test-error.js";
16
16
  /**
@@ -75,10 +75,7 @@ export async function runOneTest(test, browserName, run, options, srcDir, report
75
75
  const outcome = new TestOutcome(reporter, test, browserName);
76
76
  const { settings, secrets } = run;
77
77
  const timeout = settings.timeout;
78
- const { reportPath } = options;
79
- const resultDir = typeof reportPath === "string"
80
- ? path.dirname(reportPath)
81
- : path.join(projectDir(), "result");
78
+ const { resultDir } = runOutput(options, projectDir());
82
79
  let browser;
83
80
  let page;
84
81
  let timeoutTimer;
@@ -17,6 +17,7 @@ import { browsersOfRun, loadSpecs, selectTests } from "./select.js";
17
17
  import { resolveRetries } from "./retries-option.js";
18
18
  import { runWithRetries } from "./retry.js";
19
19
  import { resolveWorkers } from "../config/workers-option.js";
20
+ import { runOutput } from "./output.js";
20
21
  import { WorkerPool } from "./worker-pool.js";
21
22
  import { processWorkerFactory } from "./worker-process.js";
22
23
  /** Finds the spec files, picks the tests and browsers of a run and runs each test in each browser */
@@ -61,7 +62,7 @@ export default class TestRunner {
61
62
  * was also written to the report. With more than one worker the tests run at the same time in worker processes.
62
63
  */
63
64
  async start() {
64
- const { reportPath, onEvent, signal } = this.options;
65
+ const { onEvent, signal } = this.options;
65
66
  // The specs are imported once: their top-level code runs once per run
66
67
  const loaded = await loadSpecs(this.testFiles);
67
68
  const config = await loadRawConfig();
@@ -72,7 +73,7 @@ export default class TestRunner {
72
73
  const reporter = new TestReporter({
73
74
  environment: this.run.settings.environment,
74
75
  browsers,
75
- ...(reportPath !== undefined && { output: reportPath }),
76
+ output: runOutput(this.options, projectDir()).report,
76
77
  ...(onEvent && { onEvent }),
77
78
  });
78
79
  // How the run goes is settled first: a bad `workers` is an error before any browser starts
@@ -159,7 +160,7 @@ export default class TestRunner {
159
160
  /** The pool of a run: up to `workers` worker processes that read the same project and settings as this run */
160
161
  createPool(workers, retries) {
161
162
  const { settings } = this.run;
162
- const { reportPath, headless, port } = this.options;
163
+ const { reportPath, outputDir, headless, port } = this.options;
163
164
  const options = {
164
165
  workers,
165
166
  factory: processWorkerFactory(),
@@ -173,6 +174,7 @@ export default class TestRunner {
173
174
  expectTimeout: settings.expectTimeout,
174
175
  ...(headless !== undefined && { headless }),
175
176
  ...(typeof reportPath === "string" && { reportPath }),
177
+ ...(outputDir !== undefined && { outputDir }),
176
178
  files: this.testFiles,
177
179
  basePort: port ?? DEFAULT_PORT,
178
180
  ...(retries > 0 && { retries }),
@@ -24,6 +24,8 @@ export interface WorkerInit {
24
24
  retries?: number;
25
25
  /** Where the run's report goes when it is not the default; screenshots are kept beside it */
26
26
  reportPath?: string;
27
+ /** The run's `outputDir`: screenshots go there */
28
+ outputDir?: string;
27
29
  /** Spec files to import, absolute */
28
30
  files: string[];
29
31
  /** 0 for the first worker; picks the worker's browser port and profile folder */
@@ -48,6 +48,9 @@ export function openSession(init) {
48
48
  ...(init.reportPath !== undefined && {
49
49
  reportPath: init.reportPath,
50
50
  }),
51
+ ...(init.outputDir !== undefined && {
52
+ outputDir: init.outputDir,
53
+ }),
51
54
  port,
52
55
  signal,
53
56
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itmetsam/samurai-framework",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Browser test framework over WebDriver BiDi: auto-waiting locators, environments and secrets, a recorder, and a command line.",
5
5
  "main": "dist/api.js",
6
6
  "exports": {