@virtocommerce/test-automation-framework 0.0.0-stage → 0.1.0-alpha.1
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.
- package/.biome/formatter.json +11 -0
- package/.biome/imports.json +52 -0
- package/.biome/preset.json +4 -0
- package/.biome/rules.json +45 -0
- package/.claude/agents/qa-automation-expert.md +119 -0
- package/.claude/agents/qa-coverage.md +96 -0
- package/.claude/agents/qa-explorer.md +112 -0
- package/.claude/agents/qa-healer.md +88 -0
- package/.claude/agents/qa-judge.md +102 -0
- package/.claude/agents/qa-planner.md +104 -0
- package/.claude/agents/qa-reviewer.md +121 -0
- package/.claude/checks/browser-secrets.mts +107 -0
- package/.claude/checks/contract.mts +469 -0
- package/.claude/checks/expectation-guard.mts +665 -0
- package/.claude/checks/explore-extractors.mts +4 -0
- package/.claude/checks/judge-verdict.mts +165 -0
- package/.claude/checks/project-facts.mts +491 -0
- package/.claude/checks/prove-fails.mts +770 -0
- package/.claude/checks/rank-gaps.mts +177 -0
- package/.claude/checks/readonly-shell.mts +740 -0
- package/.claude/checks/reconcile-run.mts +270 -0
- package/.claude/checks/scan-prompts.mts +351 -0
- package/.claude/checks/stand.mts +45 -0
- package/.claude/checks/test-duration.mts +134 -0
- package/.claude/checks/test-shape.mts +224 -0
- package/.claude/checks/ticket-card.mts +195 -0
- package/.claude/commands/qa-coverage.md +13 -0
- package/.claude/commands/qa-explore.md +17 -0
- package/.claude/commands/qa-heal.md +44 -0
- package/.claude/commands/qa-implement.md +60 -0
- package/.claude/commands/qa-plan.md +55 -0
- package/.claude/commands/qa-review.md +90 -0
- package/.claude/commands/qa.md +156 -0
- package/.claude/hooks/readonly-shell.mts +65 -0
- package/.claude/settings.json +15 -0
- package/.claude/skills/coverage-analysis/SKILL.md +107 -0
- package/.claude/skills/create-graphql-layer/SKILL.md +159 -0
- package/.claude/skills/create-ui-layer/SKILL.md +189 -0
- package/.claude/skills/explore-api/SKILL.md +74 -0
- package/.claude/skills/explore-frontend/SKILL.md +85 -0
- package/.claude/skills/framework-rules/SKILL.md +189 -0
- package/.claude/skills/heal-tests/SKILL.md +197 -0
- package/.claude/skills/heal-tests/guard-recipe.md +53 -0
- package/.claude/skills/judge-criteria/SKILL.md +40 -0
- package/.claude/skills/judge-criteria/coverage.md +9 -0
- package/.claude/skills/judge-criteria/expert.md +23 -0
- package/.claude/skills/judge-criteria/explorer.md +9 -0
- package/.claude/skills/judge-criteria/healer.md +13 -0
- package/.claude/skills/judge-criteria/planner.md +12 -0
- package/.claude/skills/plan-implementation/SKILL.md +82 -0
- package/.claude/skills/plan-implementation/contract-template.md +72 -0
- package/.claude/skills/plan-test-strategy/SKILL.md +100 -0
- package/.claude/skills/project-facts/SKILL.md +130 -0
- package/.claude/skills/read-change/SKILL.md +64 -0
- package/.claude/skills/read-ticket/SKILL.md +63 -0
- package/.claude/skills/read-ticket/providers.md +58 -0
- package/.claude/skills/review-pr/SKILL.md +152 -0
- package/.claude/skills/review-test-code/SKILL.md +208 -0
- package/.claude/skills/write-e2e-test/SKILL.md +175 -0
- package/.claude/skills/write-graphql-test/SKILL.md +158 -0
- package/.claude/skills/write-rest-api-test/SKILL.md +182 -0
- package/.claude/skills/write-security-test/SKILL.md +253 -0
- package/.qa/project.example.json +71 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE +12 -0
- package/README.md +53 -2
- package/bin/install-qa-agents.mjs +94 -0
- package/dist/auth/credentials.d.ts +10 -0
- package/dist/auth/credentials.js +2 -0
- package/dist/auth/credentials.js.map +1 -0
- package/dist/auth/frontend-browser-sessions.d.ts +71 -0
- package/dist/auth/frontend-browser-sessions.js +130 -0
- package/dist/auth/frontend-browser-sessions.js.map +1 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/platform-browser-sessions.d.ts +47 -0
- package/dist/auth/platform-browser-sessions.js +92 -0
- package/dist/auth/platform-browser-sessions.js.map +1 -0
- package/dist/auth/sign-in-lock.d.ts +14 -0
- package/dist/auth/sign-in-lock.js +70 -0
- package/dist/auth/sign-in-lock.js.map +1 -0
- package/dist/auth/storage-state.d.ts +3 -0
- package/dist/auth/storage-state.js +2 -0
- package/dist/auth/storage-state.js.map +1 -0
- package/dist/auth/token-client.d.ts +82 -0
- package/dist/auth/token-client.js +111 -0
- package/dist/auth/token-client.js.map +1 -0
- package/dist/auth/token-manager.d.ts +67 -0
- package/dist/auth/token-manager.js +138 -0
- package/dist/auth/token-manager.js.map +1 -0
- package/dist/core/default-reporters.d.ts +31 -0
- package/dist/core/default-reporters.js +36 -0
- package/dist/core/default-reporters.js.map +1 -0
- package/dist/core/default-use.d.ts +28 -0
- package/dist/core/default-use.js +25 -0
- package/dist/core/default-use.js.map +1 -0
- package/dist/core/env.d.ts +24 -0
- package/dist/core/env.js +33 -0
- package/dist/core/env.js.map +1 -0
- package/dist/core/error-message.d.ts +10 -0
- package/dist/core/error-message.js +13 -0
- package/dist/core/error-message.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +11 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/is-ci.d.ts +14 -0
- package/dist/core/is-ci.js +18 -0
- package/dist/core/is-ci.js.map +1 -0
- package/dist/core/parse-env.d.ts +13 -0
- package/dist/core/parse-env.js +21 -0
- package/dist/core/parse-env.js.map +1 -0
- package/dist/core/projects.d.ts +24 -0
- package/dist/core/projects.js +33 -0
- package/dist/core/projects.js.map +1 -0
- package/dist/core/run-options.d.ts +31 -0
- package/dist/core/run-options.js +43 -0
- package/dist/core/run-options.js.map +1 -0
- package/dist/core/secret.d.ts +105 -0
- package/dist/core/secret.js +167 -0
- package/dist/core/secret.js.map +1 -0
- package/dist/core/string-schemas.d.ts +39 -0
- package/dist/core/string-schemas.js +56 -0
- package/dist/core/string-schemas.js.map +1 -0
- package/dist/core/url-path.d.ts +7 -0
- package/dist/core/url-path.js +2 -0
- package/dist/core/url-path.js.map +1 -0
- package/dist/dataset/collection.d.ts +44 -0
- package/dist/dataset/collection.js +76 -0
- package/dist/dataset/collection.js.map +1 -0
- package/dist/dataset/index.d.ts +11 -0
- package/dist/dataset/index.js +6 -0
- package/dist/dataset/index.js.map +1 -0
- package/dist/dataset/load-dataset.d.ts +39 -0
- package/dist/dataset/load-dataset.js +62 -0
- package/dist/dataset/load-dataset.js.map +1 -0
- package/dist/dataset/manifest.d.ts +77 -0
- package/dist/dataset/manifest.js +10 -0
- package/dist/dataset/manifest.js.map +1 -0
- package/dist/dataset/placeholders.d.ts +66 -0
- package/dist/dataset/placeholders.js +87 -0
- package/dist/dataset/placeholders.js.map +1 -0
- package/dist/dataset/seed-collection.d.ts +53 -0
- package/dist/dataset/seed-collection.js +115 -0
- package/dist/dataset/seed-collection.js.map +1 -0
- package/dist/dataset/seed-dataset.d.ts +70 -0
- package/dist/dataset/seed-dataset.js +138 -0
- package/dist/dataset/seed-dataset.js.map +1 -0
- package/dist/dataset/seed-events.d.ts +81 -0
- package/dist/dataset/seed-events.js +2 -0
- package/dist/dataset/seed-events.js.map +1 -0
- package/dist/dataset/seed-requests.d.ts +102 -0
- package/dist/dataset/seed-requests.js +160 -0
- package/dist/dataset/seed-requests.js.map +1 -0
- package/dist/dataset/seed-scope.d.ts +39 -0
- package/dist/dataset/seed-scope.js +67 -0
- package/dist/dataset/seed-scope.js.map +1 -0
- package/dist/fixtures/default-fixtures.d.ts +93 -0
- package/dist/fixtures/default-fixtures.js +105 -0
- package/dist/fixtures/default-fixtures.js.map +1 -0
- package/dist/fixtures/index.d.ts +2 -0
- package/dist/fixtures/index.js +2 -0
- package/dist/fixtures/index.js.map +1 -0
- package/dist/graphql/graphql-client.d.ts +78 -0
- package/dist/graphql/graphql-client.js +83 -0
- package/dist/graphql/graphql-client.js.map +1 -0
- package/dist/graphql/graphql-codegen.d.ts +56 -0
- package/dist/graphql/graphql-codegen.js +42 -0
- package/dist/graphql/graphql-codegen.js.map +1 -0
- package/dist/graphql/graphql-error.d.ts +54 -0
- package/dist/graphql/graphql-error.js +74 -0
- package/dist/graphql/graphql-error.js.map +1 -0
- package/dist/graphql/index.d.ts +6 -0
- package/dist/graphql/index.js +4 -0
- package/dist/graphql/index.js.map +1 -0
- package/dist/http/authentication.d.ts +16 -0
- package/dist/http/authentication.js +2 -0
- package/dist/http/authentication.js.map +1 -0
- package/dist/http/http-client.d.ts +110 -0
- package/dist/http/http-client.js +237 -0
- package/dist/http/http-client.js.map +1 -0
- package/dist/http/http-error.d.ts +26 -0
- package/dist/http/http-error.js +34 -0
- package/dist/http/http-error.js.map +1 -0
- package/dist/http/http-method.d.ts +2 -0
- package/dist/http/http-method.js +2 -0
- package/dist/http/http-method.js.map +1 -0
- package/dist/http/http-response.d.ts +137 -0
- package/dist/http/http-response.js +166 -0
- package/dist/http/http-response.js.map +1 -0
- package/dist/http/index.d.ts +7 -0
- package/dist/http/index.js +4 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/request-options.d.ts +62 -0
- package/dist/http/request-options.js +2 -0
- package/dist/http/request-options.js.map +1 -0
- package/dist/modules/index.d.ts +2 -0
- package/dist/modules/index.js +2 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/installed-modules.d.ts +86 -0
- package/dist/modules/installed-modules.js +135 -0
- package/dist/modules/installed-modules.js.map +1 -0
- package/docs/README.md +43 -0
- package/docs/concepts/areas.md +38 -0
- package/docs/concepts/auth.md +92 -0
- package/docs/concepts/config.md +102 -0
- package/docs/concepts/dataset.md +183 -0
- package/docs/concepts/environment.md +54 -0
- package/docs/concepts/fixtures.md +109 -0
- package/docs/concepts/graphql.md +76 -0
- package/docs/concepts/http.md +88 -0
- package/docs/concepts/modules.md +63 -0
- package/docs/concepts/secrets.md +50 -0
- package/docs/contributing/architecture.md +67 -0
- package/docs/contributing/conventions.md +39 -0
- package/docs/contributing/documentation.md +37 -0
- package/docs/contributing/releasing.md +76 -0
- package/docs/examples/auth.ts +20 -0
- package/docs/examples/config.ts +36 -0
- package/docs/examples/dataset.ts +70 -0
- package/docs/examples/environment.ts +8 -0
- package/docs/examples/fixtures.ts +41 -0
- package/docs/examples/getting-started.ts +22 -0
- package/docs/examples/graphql-codegen.ts +9 -0
- package/docs/examples/graphql.ts +32 -0
- package/docs/examples/http.ts +18 -0
- package/docs/examples/modules.ts +19 -0
- package/docs/examples/secrets.ts +11 -0
- package/docs/examples/tsconfig.json +10 -0
- package/docs/getting-started.md +106 -0
- package/docs/guides/generating-types.md +75 -0
- package/docs/guides/qa-agents.md +66 -0
- package/package.json +156 -4
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { errorMessage } from "../core/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decides which collections a seed run covers: every collection, or the selected ones, except those whose module is
|
|
4
|
+
* not installed or not in its range of versions, and those without records.
|
|
5
|
+
*
|
|
6
|
+
* @param manifest - The collections, in seed order.
|
|
7
|
+
* @param installed - The modules installed on the platform.
|
|
8
|
+
* @param recordCount - Returns the number of records of a collection.
|
|
9
|
+
* @param only - The selected collections, or `undefined` for every collection. Their order does not matter.
|
|
10
|
+
* @returns The scope, in the seed order of the manifest.
|
|
11
|
+
* @throws {Error} The installed version of a module cannot be compared with its range. The message starts with the
|
|
12
|
+
* collection, and the cause is the original error.
|
|
13
|
+
*/
|
|
14
|
+
export function resolveSeedScope(manifest, installed, recordCount, only) {
|
|
15
|
+
const collections = [];
|
|
16
|
+
const skipped = [];
|
|
17
|
+
for (const [key, spec] of Object.entries(manifest)) {
|
|
18
|
+
const name = key;
|
|
19
|
+
if (only !== undefined && !only.includes(name)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const reason = unmetReason(name, spec, installed) ?? (recordCount(name) === 0 ? "no records" : undefined);
|
|
23
|
+
if (reason === undefined) {
|
|
24
|
+
collections.push([name, spec]);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
skipped.push({ name, reason });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { collections, skipped };
|
|
31
|
+
}
|
|
32
|
+
function unmetReason(name, spec, installed) {
|
|
33
|
+
try {
|
|
34
|
+
return installed.unmetReason(spec.module);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
throw new Error(`${name}: ${errorMessage(error)}`, { cause: error });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parses a comma-separated list of collection names, such as `products, prices`, for example from an environment
|
|
42
|
+
* variable.
|
|
43
|
+
*
|
|
44
|
+
* @param manifest - The known collections.
|
|
45
|
+
* @param value - The list, or `undefined` for every collection.
|
|
46
|
+
* @returns The listed collections, or `undefined` for every collection.
|
|
47
|
+
* @throws {Error} The list is empty or names an unknown collection. The message lists the known collections.
|
|
48
|
+
*/
|
|
49
|
+
export function parseCollectionNames(manifest, value) {
|
|
50
|
+
if (value === undefined) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const known = Object.keys(manifest);
|
|
54
|
+
const names = value
|
|
55
|
+
.split(",")
|
|
56
|
+
.map((name) => name.trim())
|
|
57
|
+
.filter((name) => name !== "");
|
|
58
|
+
if (names.length === 0) {
|
|
59
|
+
throw new Error(`The list names no collection; the known collections are ${known.join(", ")}`);
|
|
60
|
+
}
|
|
61
|
+
const unknown = names.filter((name) => !known.includes(name));
|
|
62
|
+
if (unknown.length > 0) {
|
|
63
|
+
throw new Error(`The list names unknown collections: ${unknown.join(", ")}; the known collections are ${known.join(", ")}`);
|
|
64
|
+
}
|
|
65
|
+
return names;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=seed-scope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-scope.js","sourceRoot":"","sources":["../../src/dataset/seed-scope.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAkBtC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAC9B,QAAmB,EACnB,S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KAAyB;IAEzB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,KAAK;SAChB,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;SAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;IACjC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,2DAA2D,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjG,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9D,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,uCAAuC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,+BAA+B,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC3G,CAAC;IACJ,CAAC;IACD,OAAO,KAAoC,CAAC;AAC9C,CAAC","sourcesContent":["import type { InstalledModules } from \"@/modules\";\nimport type { CollectionName, CollectionSpec, Manifest } from \"./manifest.ts\";\n\nimport { errorMessage } from \"@/core\";\n\n/** A collection that a seed run leaves out, and the reason. */\nexport interface SkippedCollection<TManifest extends Manifest> {\n /** The name of the collection. */\n readonly name: CollectionName<TManifest>;\n /** Why the collection is left out, such as `VirtoCommerce.XPickup is not installed` or `no records`. */\n readonly reason: string;\n}\n\n/** The collections that a seed run covers. */\nexport interface SeedScope<TManifest extends Manifest> {\n /** The collections to seed, each with its manifest entry, in seed order. */\n readonly collections: readonly (readonly [CollectionName<TManifest>, CollectionSpec])[];\n /** The selected collections whose module does not qualify or that have no records, in seed order. */\n readonly skipped: readonly SkippedCollection<TManifest>[];\n}\n\n/**\n * Decides which collections a seed run covers: every collection, or the selected ones, except those whose module is\n * not installed or not in its range of versions, and those without records.\n *\n * @param manifest - The collections, in seed order.\n * @param installed - The modules installed on the platform.\n * @param recordCount - Returns the number of records of a collection.\n * @param only - The selected collections, or `undefined` for every collection. Their order does not matter.\n * @returns The scope, in the seed order of the manifest.\n * @throws {Error} The installed version of a module cannot be compared with its range. The message starts with the\n * collection, and the cause is the original error.\n */\nexport function resolveSeedScope<TManifest extends Manifest>(\n manifest: TManifest,\n installed: InstalledModules,\n recordCount: (name: CollectionName<TManifest>) => number,\n only?: readonly CollectionName<TManifest>[] | undefined,\n): SeedScope<TManifest> {\n const collections: (readonly [CollectionName<TManifest>, CollectionSpec])[] = [];\n const skipped: SkippedCollection<TManifest>[] = [];\n for (const [key, spec] of Object.entries(manifest)) {\n const name = key as CollectionName<TManifest>;\n if (only !== undefined && !only.includes(name)) {\n continue;\n }\n const reason = unmetReason(name, spec, installed) ?? (recordCount(name) === 0 ? \"no records\" : undefined);\n if (reason === undefined) {\n collections.push([name, spec]);\n } else {\n skipped.push({ name, reason });\n }\n }\n return { collections, skipped };\n}\n\nfunction unmetReason(name: string, spec: CollectionSpec, installed: InstalledModules): string | undefined {\n try {\n return installed.unmetReason(spec.module);\n } catch (error) {\n throw new Error(`${name}: ${errorMessage(error)}`, { cause: error });\n }\n}\n\n/**\n * Parses a comma-separated list of collection names, such as `products, prices`, for example from an environment\n * variable.\n *\n * @param manifest - The known collections.\n * @param value - The list, or `undefined` for every collection.\n * @returns The listed collections, or `undefined` for every collection.\n * @throws {Error} The list is empty or names an unknown collection. The message lists the known collections.\n */\nexport function parseCollectionNames<TManifest extends Manifest>(\n manifest: TManifest,\n value: string | undefined,\n): readonly CollectionName<TManifest>[] | undefined {\n if (value === undefined) {\n return undefined;\n }\n const known = Object.keys(manifest);\n const names = value\n .split(\",\")\n .map((name) => name.trim())\n .filter((name) => name !== \"\");\n if (names.length === 0) {\n throw new Error(`The list names no collection; the known collections are ${known.join(\", \")}`);\n }\n const unknown = names.filter((name) => !known.includes(name));\n if (unknown.length > 0) {\n throw new Error(\n `The list names unknown collections: ${unknown.join(\", \")}; the known collections are ${known.join(\", \")}`,\n );\n }\n return names as CollectionName<TManifest>[];\n}\n"]}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { APIRequestContext, Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions } from "@playwright/test";
|
|
2
|
+
import type { Credentials, FrontendStorageKeys } from "../auth/index.js";
|
|
3
|
+
import type { Env } from "../core/index.js";
|
|
4
|
+
import type { ModuleRequirement } from "../modules/index.js";
|
|
5
|
+
import { FrontendBrowserSessions, PlatformBrowserSessions, TokenManager } from "../auth/index.js";
|
|
6
|
+
import { GraphqlClient } from "../graphql/index.js";
|
|
7
|
+
import { HttpClient } from "../http/index.js";
|
|
8
|
+
/** The fixtures that one worker shares between its tests. */
|
|
9
|
+
export interface DefaultWorkerFixtures<TEnv extends Env> {
|
|
10
|
+
/**
|
|
11
|
+
* The environment variables of the test suite. The other fixtures read the platform and the administrator from it,
|
|
12
|
+
* so a test suite that overrides it points them all elsewhere.
|
|
13
|
+
*/
|
|
14
|
+
readonly env: TEnv;
|
|
15
|
+
/**
|
|
16
|
+
* A client of the platform without authentication, for the work of the worker, such as signing in and arranging
|
|
17
|
+
* data that the tests of a file share.
|
|
18
|
+
*/
|
|
19
|
+
readonly workerHttp: HttpClient;
|
|
20
|
+
/**
|
|
21
|
+
* Signs users in to the platform once for each worker, keeps their tokens fresh, and signs them out when the worker
|
|
22
|
+
* ends. Sign a user out with `signOut` before the user is deleted.
|
|
23
|
+
*/
|
|
24
|
+
readonly tokens: TokenManager;
|
|
25
|
+
/**
|
|
26
|
+
* A client of the platform signed in as the administrator of the environment, for the work of the worker, such as
|
|
27
|
+
* arranging data that the tests of a file share.
|
|
28
|
+
*/
|
|
29
|
+
readonly workerAdmin: HttpClient;
|
|
30
|
+
/**
|
|
31
|
+
* Builds the storage states that start a browser on a frontend, signed in or anonymous. Its tokens are revoked when
|
|
32
|
+
* the worker ends.
|
|
33
|
+
*/
|
|
34
|
+
readonly frontendSessions: FrontendBrowserSessions;
|
|
35
|
+
/** Builds the storage states that start a browser signed in to the admin UI of the platform. */
|
|
36
|
+
readonly platformSessions: PlatformBrowserSessions;
|
|
37
|
+
}
|
|
38
|
+
/** The fixtures of one test. */
|
|
39
|
+
export interface DefaultFixtures {
|
|
40
|
+
/** The request context of the test, disposed of after it. */
|
|
41
|
+
readonly apiContext: APIRequestContext;
|
|
42
|
+
/** A client of the platform without authentication. */
|
|
43
|
+
readonly http: HttpClient;
|
|
44
|
+
/** Returns a client of the platform signed in as a user. The token is cached for the worker. */
|
|
45
|
+
readonly signIn: (credentials: Credentials) => Promise<HttpClient>;
|
|
46
|
+
/** A GraphQL client of the platform without authentication. */
|
|
47
|
+
readonly graphql: GraphqlClient;
|
|
48
|
+
/**
|
|
49
|
+
* The modules that the tests need, set with `test.use`, such as `{ QUOTES, XPICKUP }`. The keys are labels; a test
|
|
50
|
+
* whose module is not installed or not in its range of versions is skipped, with the reason.
|
|
51
|
+
*/
|
|
52
|
+
readonly modules: Readonly<Record<string, ModuleRequirement>>;
|
|
53
|
+
/**
|
|
54
|
+
* Skips a test whose required modules do not qualify. It runs for every test, but reads the modules only when the
|
|
55
|
+
* test names requirements; tests do not use it.
|
|
56
|
+
*/
|
|
57
|
+
readonly checkModules: undefined;
|
|
58
|
+
}
|
|
59
|
+
/** The options of {@link defaultFixtures}. */
|
|
60
|
+
export interface DefaultFixturesOptions {
|
|
61
|
+
/** Where the frontends keep their sign-in in local storage. The defaults are those of the Virto Commerce frontend. */
|
|
62
|
+
readonly frontendStorageKeys?: FrontendStorageKeys | undefined;
|
|
63
|
+
}
|
|
64
|
+
/** The definitions of the default fixtures, for `test.extend`. */
|
|
65
|
+
export type DefaultFixtureDefinitions<TEnv extends Env> = Fixtures<DefaultFixtures, DefaultWorkerFixtures<TEnv>, PlaywrightTestArgs & PlaywrightTestOptions, PlaywrightWorkerArgs & PlaywrightWorkerOptions>;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the fixtures that tests of a Virto Commerce solution need, for its platform and its frontends, for
|
|
68
|
+
* `test.extend`.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* - Users sign in once for each worker, and the tests of the worker reuse their tokens. They are signed out when the
|
|
72
|
+
* worker ends.
|
|
73
|
+
* - Each test has its own request context, disposed of after it. Every request appears in the trace of the test that
|
|
74
|
+
* runs at the time; requests of the worker, such as sign-ins and reading the modules, appear in the trace of the
|
|
75
|
+
* test that first needs them.
|
|
76
|
+
* - A test whose `modules` do not qualify is skipped, with the reason. A module version that cannot be
|
|
77
|
+
* compared fails the test. The modules are read, as the administrator, only for a test that names requirements.
|
|
78
|
+
* - Browser tests start signed in: the test suite applies a storage state of `frontendSessions` or `platformSessions`
|
|
79
|
+
* to `context`, so no sign-in page is needed.
|
|
80
|
+
* - TypeScript cannot infer the fixture types from the definitions, so pass them to `extend`, as in the example.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* export const test = base
|
|
84
|
+
* .extend<DefaultFixtures, DefaultWorkerFixtures<SuiteEnv>>(defaultFixtures(env))
|
|
85
|
+
* .extend<{ storeAdmin: HttpClient }>({
|
|
86
|
+
* storeAdmin: async ({ signIn }, use) => use(await signIn(credentialsOf(STORE_ADMINISTRATOR))),
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* @param env - The parsed environment variables of the test suite, the value of the `env` fixture.
|
|
90
|
+
* @param options - The frontend storage keys, for a test suite whose frontend keeps its sign-in elsewhere.
|
|
91
|
+
* @returns The fixture definitions.
|
|
92
|
+
*/
|
|
93
|
+
export declare function defaultFixtures<TEnv extends Env>(env: TEnv, options?: DefaultFixturesOptions): DefaultFixtureDefinitions<TEnv>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { FrontendBrowserSessions, PlatformBrowserSessions, TokenClient, TokenManager } from "../auth/index.js";
|
|
2
|
+
import { GraphqlClient } from "../graphql/index.js";
|
|
3
|
+
import { HttpClient } from "../http/index.js";
|
|
4
|
+
import { InstalledModules } from "../modules/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* Returns the fixtures that tests of a Virto Commerce solution need, for its platform and its frontends, for
|
|
7
|
+
* `test.extend`.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* - Users sign in once for each worker, and the tests of the worker reuse their tokens. They are signed out when the
|
|
11
|
+
* worker ends.
|
|
12
|
+
* - Each test has its own request context, disposed of after it. Every request appears in the trace of the test that
|
|
13
|
+
* runs at the time; requests of the worker, such as sign-ins and reading the modules, appear in the trace of the
|
|
14
|
+
* test that first needs them.
|
|
15
|
+
* - A test whose `modules` do not qualify is skipped, with the reason. A module version that cannot be
|
|
16
|
+
* compared fails the test. The modules are read, as the administrator, only for a test that names requirements.
|
|
17
|
+
* - Browser tests start signed in: the test suite applies a storage state of `frontendSessions` or `platformSessions`
|
|
18
|
+
* to `context`, so no sign-in page is needed.
|
|
19
|
+
* - TypeScript cannot infer the fixture types from the definitions, so pass them to `extend`, as in the example.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* export const test = base
|
|
23
|
+
* .extend<DefaultFixtures, DefaultWorkerFixtures<SuiteEnv>>(defaultFixtures(env))
|
|
24
|
+
* .extend<{ storeAdmin: HttpClient }>({
|
|
25
|
+
* storeAdmin: async ({ signIn }, use) => use(await signIn(credentialsOf(STORE_ADMINISTRATOR))),
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* @param env - The parsed environment variables of the test suite, the value of the `env` fixture.
|
|
29
|
+
* @param options - The frontend storage keys, for a test suite whose frontend keeps its sign-in elsewhere.
|
|
30
|
+
* @returns The fixture definitions.
|
|
31
|
+
*/
|
|
32
|
+
export function defaultFixtures(env, options = {}) {
|
|
33
|
+
return {
|
|
34
|
+
env: [async ({}, use) => use(env), { scope: "worker", box: true }],
|
|
35
|
+
workerHttp: [
|
|
36
|
+
async ({ playwright, env }, use) => {
|
|
37
|
+
const context = await playwright.request.newContext(contextOptions(env));
|
|
38
|
+
await use(new HttpClient(context, env.BACKEND_BASE_URL));
|
|
39
|
+
await context.dispose();
|
|
40
|
+
},
|
|
41
|
+
{ scope: "worker", box: true },
|
|
42
|
+
],
|
|
43
|
+
tokens: [
|
|
44
|
+
async ({ workerHttp }, use) => {
|
|
45
|
+
const tokens = new TokenManager(new TokenClient(workerHttp));
|
|
46
|
+
await use(tokens);
|
|
47
|
+
await tokens.signOutAll();
|
|
48
|
+
},
|
|
49
|
+
{ scope: "worker", box: true },
|
|
50
|
+
],
|
|
51
|
+
workerAdmin: [
|
|
52
|
+
async ({ workerHttp, tokens, env }, use) => use(await tokens.signIn(workerHttp, administratorOf(env))),
|
|
53
|
+
{ scope: "worker", title: "sign in as the administrator" },
|
|
54
|
+
],
|
|
55
|
+
frontendSessions: [
|
|
56
|
+
async ({ playwright, env }, use) => {
|
|
57
|
+
const sessions = new FrontendBrowserSessions({
|
|
58
|
+
newContext: () => playwright.request.newContext(contextOptions(env)),
|
|
59
|
+
storageKeys: options.frontendStorageKeys,
|
|
60
|
+
});
|
|
61
|
+
await use(sessions);
|
|
62
|
+
await sessions.dispose();
|
|
63
|
+
},
|
|
64
|
+
{ scope: "worker", box: true },
|
|
65
|
+
],
|
|
66
|
+
platformSessions: [
|
|
67
|
+
async ({ playwright, env }, use) => use(new PlatformBrowserSessions({
|
|
68
|
+
platformBaseUrl: env.BACKEND_BASE_URL,
|
|
69
|
+
newContext: () => playwright.request.newContext(contextOptions(env)),
|
|
70
|
+
})),
|
|
71
|
+
{ scope: "worker", box: true },
|
|
72
|
+
],
|
|
73
|
+
apiContext: [
|
|
74
|
+
async ({ playwright, env }, use) => {
|
|
75
|
+
const context = await playwright.request.newContext(contextOptions(env));
|
|
76
|
+
await use(context);
|
|
77
|
+
await context.dispose();
|
|
78
|
+
},
|
|
79
|
+
{ box: true },
|
|
80
|
+
],
|
|
81
|
+
http: [async ({ apiContext, env }, use) => use(new HttpClient(apiContext, env.BACKEND_BASE_URL)), { box: true }],
|
|
82
|
+
signIn: [async ({ http, tokens }, use) => use((credentials) => tokens.signIn(http, credentials)), { box: true }],
|
|
83
|
+
graphql: [async ({ http }, use) => use(new GraphqlClient(http)), { box: true }],
|
|
84
|
+
modules: [{}, { option: true }],
|
|
85
|
+
checkModules: [
|
|
86
|
+
async ({ modules, workerHttp, tokens, env }, use, testInfo) => {
|
|
87
|
+
const requirements = Object.values(modules);
|
|
88
|
+
if (requirements.length > 0) {
|
|
89
|
+
const installed = await InstalledModules.read(await tokens.signIn(workerHttp, administratorOf(env)));
|
|
90
|
+
const reasons = installed.unmetReasons(requirements);
|
|
91
|
+
testInfo.skip(reasons.length > 0, reasons.join("; "));
|
|
92
|
+
}
|
|
93
|
+
await use(undefined);
|
|
94
|
+
},
|
|
95
|
+
{ auto: true, box: true },
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function administratorOf(env) {
|
|
100
|
+
return { username: env.ADMIN_USERNAME, password: env.ADMIN_PASSWORD };
|
|
101
|
+
}
|
|
102
|
+
function contextOptions(env) {
|
|
103
|
+
return { ignoreHTTPSErrors: !env.VERIFY_SSL, timeout: env.REQUEST_TIMEOUT_MS };
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=default-fixtures.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-fixtures.js","sourceRoot":"","sources":["../../src/fixtures/default-fixtures.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;A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,MAAM,OAAO,GAAG,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;oBACrD,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBACxD,CAAC;gBACD,MAAM,GAAG,CAAC,SAAS,CAAC,CAAC;YACvB,CAAC;YACD,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE;SAC1B;KACF,CAAC;AACJ,CAAC;AAED,SAAS,eAAe,CAAC,GAAQ;IAC/B,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,cAAc,EAAE,QAAQ,EAAE,GAAG,CAAC,cAAc,EAAE,CAAC;AACxE,CAAC;AAED,SAAS,cAAc,CAAC,GAAQ;IAC9B,OAAO,EAAE,iBAAiB,EAAE,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,GAAG,CAAC,kBAAkB,EAAE,CAAC;AACjF,CAAC","sourcesContent":["import type {\n APIRequest,\n APIRequestContext,\n Fixtures,\n PlaywrightTestArgs,\n PlaywrightTestOptions,\n PlaywrightWorkerArgs,\n PlaywrightWorkerOptions,\n} from \"@playwright/test\";\nimport type { Credentials, FrontendStorageKeys } from \"@/auth\";\nimport type { Env } from \"@/core\";\nimport type { ModuleRequirement } from \"@/modules\";\n\nimport { FrontendBrowserSessions, PlatformBrowserSessions, TokenClient, TokenManager } from \"@/auth\";\nimport { GraphqlClient } from \"@/graphql\";\nimport { HttpClient } from \"@/http\";\nimport { InstalledModules } from \"@/modules\";\n\n/** The fixtures that one worker shares between its tests. */\nexport interface DefaultWorkerFixtures<TEnv extends Env> {\n /**\n * The environment variables of the test suite. The other fixtures read the platform and the administrator from it,\n * so a test suite that overrides it points them all elsewhere.\n */\n readonly env: TEnv;\n /**\n * A client of the platform without authentication, for the work of the worker, such as signing in and arranging\n * data that the tests of a file share.\n */\n readonly workerHttp: HttpClient;\n /**\n * Signs users in to the platform once for each worker, keeps their tokens fresh, and signs them out when the worker\n * ends. Sign a user out with `signOut` before the user is deleted.\n */\n readonly tokens: TokenManager;\n /**\n * A client of the platform signed in as the administrator of the environment, for the work of the worker, such as\n * arranging data that the tests of a file share.\n */\n readonly workerAdmin: HttpClient;\n /**\n * Builds the storage states that start a browser on a frontend, signed in or anonymous. Its tokens are revoked when\n * the worker ends.\n */\n readonly frontendSessions: FrontendBrowserSessions;\n /** Builds the storage states that start a browser signed in to the admin UI of the platform. */\n readonly platformSessions: PlatformBrowserSessions;\n}\n\n/** The fixtures of one test. */\nexport interface DefaultFixtures {\n /** The request context of the test, disposed of after it. */\n readonly apiContext: APIRequestContext;\n /** A client of the platform without authentication. */\n readonly http: HttpClient;\n /** Returns a client of the platform signed in as a user. The token is cached for the worker. */\n readonly signIn: (credentials: Credentials) => Promise<HttpClient>;\n /** A GraphQL client of the platform without authentication. */\n readonly graphql: GraphqlClient;\n /**\n * The modules that the tests need, set with `test.use`, such as `{ QUOTES, XPICKUP }`. The keys are labels; a test\n * whose module is not installed or not in its range of versions is skipped, with the reason.\n */\n readonly modules: Readonly<Record<string, ModuleRequirement>>;\n /**\n * Skips a test whose required modules do not qualify. It runs for every test, but reads the modules only when the\n * test names requirements; tests do not use it.\n */\n readonly checkModules: undefined;\n}\n\n/** The options of {@link defaultFixtures}. */\nexport interface DefaultFixturesOptions {\n /** Where the frontends keep their sign-in in local storage. The defaults are those of the Virto Commerce frontend. */\n readonly frontendStorageKeys?: FrontendStorageKeys | undefined;\n}\n\n/** The definitions of the default fixtures, for `test.extend`. */\nexport type DefaultFixtureDefinitions<TEnv extends Env> = Fixtures<\n DefaultFixtures,\n DefaultWorkerFixtures<TEnv>,\n PlaywrightTestArgs & PlaywrightTestOptions,\n PlaywrightWorkerArgs & PlaywrightWorkerOptions\n>;\n\n/**\n * Returns the fixtures that tests of a Virto Commerce solution need, for its platform and its frontends, for\n * `test.extend`.\n *\n * @remarks\n * - Users sign in once for each worker, and the tests of the worker reuse their tokens. They are signed out when the\n * worker ends.\n * - Each test has its own request context, disposed of after it. Every request appears in the trace of the test that\n * runs at the time; requests of the worker, such as sign-ins and reading the modules, appear in the trace of the\n * test that first needs them.\n * - A test whose `modules` do not qualify is skipped, with the reason. A module version that cannot be\n * compared fails the test. The modules are read, as the administrator, only for a test that names requirements.\n * - Browser tests start signed in: the test suite applies a storage state of `frontendSessions` or `platformSessions`\n * to `context`, so no sign-in page is needed.\n * - TypeScript cannot infer the fixture types from the definitions, so pass them to `extend`, as in the example.\n *\n * @example\n * export const test = base\n * .extend<DefaultFixtures, DefaultWorkerFixtures<SuiteEnv>>(defaultFixtures(env))\n * .extend<{ storeAdmin: HttpClient }>({\n * storeAdmin: async ({ signIn }, use) => use(await signIn(credentialsOf(STORE_ADMINISTRATOR))),\n * });\n *\n * @param env - The parsed environment variables of the test suite, the value of the `env` fixture.\n * @param options - The frontend storage keys, for a test suite whose frontend keeps its sign-in elsewhere.\n * @returns The fixture definitions.\n */\nexport function defaultFixtures<TEnv extends Env>(\n env: TEnv,\n options: DefaultFixturesOptions = {},\n): DefaultFixtureDefinitions<TEnv> {\n return {\n env: [async ({}, use) => use(env), { scope: \"worker\", box: true }],\n\n workerHttp: [\n async ({ playwright, env }, use) => {\n const context = await playwright.request.newContext(contextOptions(env));\n await use(new HttpClient(context, env.BACKEND_BASE_URL));\n await context.dispose();\n },\n { scope: \"worker\", box: true },\n ],\n\n tokens: [\n async ({ workerHttp }, use) => {\n const tokens = new TokenManager(new TokenClient(workerHttp));\n await use(tokens);\n await tokens.signOutAll();\n },\n { scope: \"worker\", box: true },\n ],\n\n workerAdmin: [\n async ({ workerHttp, tokens, env }, use) => use(await tokens.signIn(workerHttp, administratorOf(env))),\n { scope: \"worker\", title: \"sign in as the administrator\" },\n ],\n\n frontendSessions: [\n async ({ playwright, env }, use) => {\n const sessions = new FrontendBrowserSessions({\n newContext: () => playwright.request.newContext(contextOptions(env)),\n storageKeys: options.frontendStorageKeys,\n });\n await use(sessions);\n await sessions.dispose();\n },\n { scope: \"worker\", box: true },\n ],\n\n platformSessions: [\n async ({ playwright, env }, use) =>\n use(\n new PlatformBrowserSessions({\n platformBaseUrl: env.BACKEND_BASE_URL,\n newContext: () => playwright.request.newContext(contextOptions(env)),\n }),\n ),\n { scope: \"worker\", box: true },\n ],\n\n apiContext: [\n async ({ playwright, env }, use) => {\n const context = await playwright.request.newContext(contextOptions(env));\n await use(context);\n await context.dispose();\n },\n { box: true },\n ],\n\n http: [async ({ apiContext, env }, use) => use(new HttpClient(apiContext, env.BACKEND_BASE_URL)), { box: true }],\n\n signIn: [async ({ http, tokens }, use) => use((credentials) => tokens.signIn(http, credentials)), { box: true }],\n\n graphql: [async ({ http }, use) => use(new GraphqlClient(http)), { box: true }],\n\n modules: [{}, { option: true }],\n\n checkModules: [\n async ({ modules, workerHttp, tokens, env }, use, testInfo) => {\n const requirements = Object.values(modules);\n if (requirements.length > 0) {\n const installed = await InstalledModules.read(await tokens.signIn(workerHttp, administratorOf(env)));\n const reasons = installed.unmetReasons(requirements);\n testInfo.skip(reasons.length > 0, reasons.join(\"; \"));\n }\n await use(undefined);\n },\n { auto: true, box: true },\n ],\n };\n}\n\nfunction administratorOf(env: Env): Credentials {\n return { username: env.ADMIN_USERNAME, password: env.ADMIN_PASSWORD };\n}\n\nfunction contextOptions(env: Env): Parameters<APIRequest[\"newContext\"]>[0] {\n return { ignoreHTTPSErrors: !env.VERIFY_SSL, timeout: env.REQUEST_TIMEOUT_MS };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fixtures/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC","sourcesContent":["export type {\n DefaultFixtureDefinitions,\n DefaultFixtures,\n DefaultFixturesOptions,\n DefaultWorkerFixtures,\n} from \"./default-fixtures.ts\";\n\nexport { defaultFixtures } from \"./default-fixtures.ts\";\n"]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { SecretsAllowed } from "../core/index.js";
|
|
2
|
+
import type { HttpClient } from "../http/index.js";
|
|
3
|
+
import type { GraphqlErrorEntry } from "./graphql-error.ts";
|
|
4
|
+
/**
|
|
5
|
+
* An operation document typed with its result and variables, such as a document that GraphQL Code Generator creates
|
|
6
|
+
* in the string document mode.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* A test suite passes its generated documents directly. The framework reads only the text of the document, so it does
|
|
10
|
+
* not need a GraphQL package.
|
|
11
|
+
*/
|
|
12
|
+
export interface GraphqlDocument<TResult, TVariables> {
|
|
13
|
+
/** Carries the result and variable types of the document. It is never called. */
|
|
14
|
+
readonly __apiType?: (variables: TVariables) => TResult;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the text of the document.
|
|
17
|
+
*
|
|
18
|
+
* @returns The text of the document.
|
|
19
|
+
*/
|
|
20
|
+
toString(): string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The variables argument of an operation. It is optional only when the operation has no required variables, and each
|
|
24
|
+
* string in it can be a `Secret`.
|
|
25
|
+
*/
|
|
26
|
+
export type GraphqlVariablesArgument<TVariables> = Record<string, never> extends TVariables ? [variables?: SecretsAllowed<TVariables>] : [variables: SecretsAllowed<TVariables>];
|
|
27
|
+
/** The complete result of a GraphQL operation, including its errors. */
|
|
28
|
+
export interface GraphqlResult<TResult> {
|
|
29
|
+
/** The name of the operation, taken from the document, or `(anonymous)` for an operation without a name. */
|
|
30
|
+
readonly operationName: string;
|
|
31
|
+
/** The HTTP status of the response. */
|
|
32
|
+
readonly status: number;
|
|
33
|
+
/** The data of the response, or `null` when the response has no data. */
|
|
34
|
+
readonly data: TResult | null;
|
|
35
|
+
/** The error entries of the response. It is empty when the response has no errors. */
|
|
36
|
+
readonly errors: readonly GraphqlErrorEntry[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Represents a client that runs typed GraphQL operations.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* The client reads GraphQL errors from a response with any status, because a server can return them with a success
|
|
43
|
+
* status or, for an invalid operation, with a 400 status. A failed status without GraphQL errors is an HTTP error.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const graphql = new GraphqlClient(signedInClient);
|
|
47
|
+
* const { cart } = await graphql.execute(GetCartDocument, { storeId, userId, currencyCode, cultureName });
|
|
48
|
+
*/
|
|
49
|
+
export declare class GraphqlClient {
|
|
50
|
+
#private;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a GraphQL client.
|
|
53
|
+
*
|
|
54
|
+
* @param httpClient - The client that sends the operations, usually signed in.
|
|
55
|
+
* @param path - The path of the GraphQL endpoint. The default is `/graphql`.
|
|
56
|
+
*/
|
|
57
|
+
constructor(httpClient: HttpClient, path?: string);
|
|
58
|
+
/**
|
|
59
|
+
* Runs an operation and returns its data.
|
|
60
|
+
*
|
|
61
|
+
* @param document - The operation document.
|
|
62
|
+
* @param variables - The variables of the operation.
|
|
63
|
+
* @returns The data of the response.
|
|
64
|
+
* @throws {GraphqlError} The response has GraphQL errors, even with partial data, or has no data.
|
|
65
|
+
* @throws {HttpError} The status is a failed status without GraphQL errors, or the body is not a GraphQL response.
|
|
66
|
+
*/
|
|
67
|
+
execute<TResult, TVariables>(document: GraphqlDocument<TResult, TVariables>, ...variables: GraphqlVariablesArgument<TVariables>): Promise<TResult>;
|
|
68
|
+
/**
|
|
69
|
+
* Runs an operation and returns its complete result without throwing for GraphQL errors. Use it in tests that check
|
|
70
|
+
* errors or partial data.
|
|
71
|
+
*
|
|
72
|
+
* @param document - The operation document.
|
|
73
|
+
* @param variables - The variables of the operation.
|
|
74
|
+
* @returns The data, the errors and the status of the response.
|
|
75
|
+
* @throws {HttpError} The status is a failed status without GraphQL errors, or the body is not a GraphQL response.
|
|
76
|
+
*/
|
|
77
|
+
executeRaw<TResult, TVariables>(document: GraphqlDocument<TResult, TVariables>, ...[variables]: GraphqlVariablesArgument<TVariables>): Promise<GraphqlResult<TResult>>;
|
|
78
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { array, looseObject, unknown } from "zod";
|
|
2
|
+
import { GraphqlError, graphqlErrorEntrySchema } from "./graphql-error.js";
|
|
3
|
+
const DEFAULT_PATH = "/graphql";
|
|
4
|
+
const ANONYMOUS_OPERATION = "(anonymous)";
|
|
5
|
+
const OPERATION_NAME = /\b(?:query|mutation|subscription)\s+([A-Za-z_]\w*)/;
|
|
6
|
+
const graphqlResponseSchema = looseObject({
|
|
7
|
+
data: unknown().optional(),
|
|
8
|
+
errors: array(graphqlErrorEntrySchema).optional(),
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Represents a client that runs typed GraphQL operations.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* The client reads GraphQL errors from a response with any status, because a server can return them with a success
|
|
15
|
+
* status or, for an invalid operation, with a 400 status. A failed status without GraphQL errors is an HTTP error.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const graphql = new GraphqlClient(signedInClient);
|
|
19
|
+
* const { cart } = await graphql.execute(GetCartDocument, { storeId, userId, currencyCode, cultureName });
|
|
20
|
+
*/
|
|
21
|
+
export class GraphqlClient {
|
|
22
|
+
#httpClient;
|
|
23
|
+
#path;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a GraphQL client.
|
|
26
|
+
*
|
|
27
|
+
* @param httpClient - The client that sends the operations, usually signed in.
|
|
28
|
+
* @param path - The path of the GraphQL endpoint. The default is `/graphql`.
|
|
29
|
+
*/
|
|
30
|
+
constructor(httpClient, path = DEFAULT_PATH) {
|
|
31
|
+
this.#httpClient = httpClient;
|
|
32
|
+
this.#path = path;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Runs an operation and returns its data.
|
|
36
|
+
*
|
|
37
|
+
* @param document - The operation document.
|
|
38
|
+
* @param variables - The variables of the operation.
|
|
39
|
+
* @returns The data of the response.
|
|
40
|
+
* @throws {GraphqlError} The response has GraphQL errors, even with partial data, or has no data.
|
|
41
|
+
* @throws {HttpError} The status is a failed status without GraphQL errors, or the body is not a GraphQL response.
|
|
42
|
+
*/
|
|
43
|
+
async execute(document, ...variables) {
|
|
44
|
+
const result = await this.executeRaw(document, ...variables);
|
|
45
|
+
if (result.errors.length > 0 || result.data === null) {
|
|
46
|
+
throw new GraphqlError(result.operationName, result.status, result.errors);
|
|
47
|
+
}
|
|
48
|
+
return result.data;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Runs an operation and returns its complete result without throwing for GraphQL errors. Use it in tests that check
|
|
52
|
+
* errors or partial data.
|
|
53
|
+
*
|
|
54
|
+
* @param document - The operation document.
|
|
55
|
+
* @param variables - The variables of the operation.
|
|
56
|
+
* @returns The data, the errors and the status of the response.
|
|
57
|
+
* @throws {HttpError} The status is a failed status without GraphQL errors, or the body is not a GraphQL response.
|
|
58
|
+
*/
|
|
59
|
+
async executeRaw(document, ...[variables]) {
|
|
60
|
+
const query = document.toString();
|
|
61
|
+
const name = OPERATION_NAME.exec(query)?.[1];
|
|
62
|
+
const response = await this.#httpClient.post(this.#path, {
|
|
63
|
+
json: { query, operationName: name, variables: variables ?? {} },
|
|
64
|
+
});
|
|
65
|
+
const body = graphqlBodyOf(response);
|
|
66
|
+
return {
|
|
67
|
+
operationName: name ?? ANONYMOUS_OPERATION,
|
|
68
|
+
status: response.status,
|
|
69
|
+
data: (body.data ?? null),
|
|
70
|
+
errors: body.errors ?? [],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function graphqlBodyOf(response) {
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
const failed = graphqlResponseSchema.safeParse(response.errorJson());
|
|
77
|
+
if (failed.success && (failed.data.errors?.length ?? 0) > 0) {
|
|
78
|
+
return failed.data;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return response.parse(graphqlResponseSchema);
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=graphql-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql-client.js","sourceRoot":"","sources":["../../src/graphql/graphql-client.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAElD,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAE3E,MAAM,YAAY,GAAG,UAAU,CAAC;AAChC,MAAM,mBAAmB,GAAG,aAAa,CAAC;AAC1C,MAAM,cAAc,GAAG,oDAAoD,CAAC;AAE5E,MAAM,qBAAqB,GAAG,WAAW,CAAC;IACxC,IAAI,EAAE,OAAO,EAAE,CAAC,QAAQ,EAAE;IAC1B,MAAM,EAAE,KAAK,CAAC,uBAAuB,CAAC,CAAC,QAAQ,EAAE;CAClD,CAAC,CAAC;AA6CH;;;;;;;;;;GAUG;AACH,MAAM,OAAO,aAAa;IACf,WAAW,CAAa;IACxB,KAAK,CAAS;IAEvB;;;;;OAKG;IACH,YAAY,UAAsB,EAAE,IAAI,GAAW,YAAY;QAC7D,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,OAAO,CACX,QAA8C,EAC9C,GAAG,SAA+C;QAElD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,GAAG,SAAS,CAAC,CAAC;QAC7D,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YACrD,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QAC7E,CAAC;QACD,OAA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"@/core\";\nimport type { HttpClient, HttpResponse } from \"@/http\";\nimport type { GraphqlErrorEntry } from \"./graphql-error.ts\";\n\nimport { array, looseObject, unknown } from \"zod\";\n\nimport { GraphqlError, graphqlErrorEntrySchema } from \"./graphql-error.ts\";\n\nconst DEFAULT_PATH = \"/graphql\";\nconst ANONYMOUS_OPERATION = \"(anonymous)\";\nconst OPERATION_NAME = /\\b(?:query|mutation|subscription)\\s+([A-Za-z_]\\w*)/;\n\nconst graphqlResponseSchema = looseObject({\n data: unknown().optional(),\n errors: array(graphqlErrorEntrySchema).optional(),\n});\n\ntype GraphqlBody = output<typeof graphqlResponseSchema>;\n\n/**\n * An operation document typed with its result and variables, such as a document that GraphQL Code Generator creates\n * in the string document mode.\n *\n * @remarks\n * A test suite passes its generated documents directly. The framework reads only the text of the document, so it does\n * not need a GraphQL package.\n */\nexport interface GraphqlDocument<TResult, TVariables> {\n /** Carries the result and variable types of the document. It is never called. */\n readonly __apiType?: (variables: TVariables) => TResult;\n\n /**\n * Returns the text of the document.\n *\n * @returns The text of the document.\n */\n toString(): string;\n}\n\n/**\n * The variables argument of an operation. It is optional only when the operation has no required variables, and each\n * string in it can be a `Secret`.\n */\nexport type GraphqlVariablesArgument<TVariables> =\n Record<string, never> extends TVariables\n ? [variables?: SecretsAllowed<TVariables>]\n : [variables: SecretsAllowed<TVariables>];\n\n/** The complete result of a GraphQL operation, including its errors. */\nexport interface GraphqlResult<TResult> {\n /** The name of the operation, taken from the document, or `(anonymous)` for an operation without a name. */\n readonly operationName: string;\n /** The HTTP status of the response. */\n readonly status: number;\n /** The data of the response, or `null` when the response has no data. */\n readonly data: TResult | null;\n /** The error entries of the response. It is empty when the response has no errors. */\n readonly errors: readonly GraphqlErrorEntry[];\n}\n\n/**\n * Represents a client that runs typed GraphQL operations.\n *\n * @remarks\n * The client reads GraphQL errors from a response with any status, because a server can return them with a success\n * status or, for an invalid operation, with a 400 status. A failed status without GraphQL errors is an HTTP error.\n *\n * @example\n * const graphql = new GraphqlClient(signedInClient);\n * const { cart } = await graphql.execute(GetCartDocument, { storeId, userId, currencyCode, cultureName });\n */\nexport class GraphqlClient {\n readonly #httpClient: HttpClient;\n readonly #path: string;\n\n /**\n * Creates a GraphQL client.\n *\n * @param httpClient - The client that sends the operations, usually signed in.\n * @param path - The path of the GraphQL endpoint. The default is `/graphql`.\n */\n constructor(httpClient: HttpClient, path: string = DEFAULT_PATH) {\n this.#httpClient = httpClient;\n this.#path = path;\n }\n\n /**\n * Runs an operation and returns its data.\n *\n * @param document - The operation document.\n * @param variables - The variables of the operation.\n * @returns The data of the response.\n * @throws {GraphqlError} The response has GraphQL errors, even with partial data, or has no data.\n * @throws {HttpError} The status is a failed status without GraphQL errors, or the body is not a GraphQL response.\n */\n async execute<TResult, TVariables>(\n document: GraphqlDocument<TResult, TVariables>,\n ...variables: GraphqlVariablesArgument<TVariables>\n ): Promise<TResult> {\n const result = await this.executeRaw(document, ...variables);\n if (result.errors.length > 0 || result.data === null) {\n throw new GraphqlError(result.operationName, result.status, result.errors);\n }\n return result.data;\n }\n\n /**\n * Runs an operation and returns its complete result without throwing for GraphQL errors. Use it in tests that check\n * errors or partial data.\n *\n * @param document - The operation document.\n * @param variables - The variables of the operation.\n * @returns The data, the errors and the status of the response.\n * @throws {HttpError} The status is a failed status without GraphQL errors, or the body is not a GraphQL response.\n */\n async executeRaw<TResult, TVariables>(\n document: GraphqlDocument<TResult, TVariables>,\n ...[variables]: GraphqlVariablesArgument<TVariables>\n ): Promise<GraphqlResult<TResult>> {\n const query = document.toString();\n const name = OPERATION_NAME.exec(query)?.[1];\n const response = await this.#httpClient.post(this.#path, {\n json: { query, operationName: name, variables: variables ?? {} },\n });\n const body = graphqlBodyOf(response);\n return {\n operationName: name ?? ANONYMOUS_OPERATION,\n status: response.status,\n data: (body.data ?? null) as TResult | null,\n errors: body.errors ?? [],\n };\n }\n}\n\nfunction graphqlBodyOf(response: HttpResponse): GraphqlBody {\n if (!response.ok) {\n const failed = graphqlResponseSchema.safeParse(response.errorJson());\n if (failed.success && (failed.data.errors?.length ?? 0) > 0) {\n return failed.data;\n }\n }\n return response.parse(graphqlResponseSchema);\n}\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** The options of {@link graphqlCodegenConfig}. */
|
|
2
|
+
export interface GraphqlCodegenOptions {
|
|
3
|
+
/** The GraphQL schema: a schema file, a glob of schema files, or the URL of a GraphQL endpoint. */
|
|
4
|
+
readonly schema: string;
|
|
5
|
+
/** The operation documents, as one glob or several, such as `graphql/operations/*.graphql`. */
|
|
6
|
+
readonly documents: string | readonly string[];
|
|
7
|
+
/** The directory that receives the generated files, such as `graphql/generated/`. */
|
|
8
|
+
readonly output: string;
|
|
9
|
+
/**
|
|
10
|
+
* The TypeScript types of custom scalars by scalar name, such as `{ DateTime: "string" }`. Custom scalars that are
|
|
11
|
+
* not listed are `unknown`, unless `strictScalars` is set.
|
|
12
|
+
*/
|
|
13
|
+
readonly scalars?: Readonly<Record<string, string>> | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Whether the generation fails for a custom scalar that `scalars` does not list, so a new scalar of the schema is
|
|
16
|
+
* noticed. The default is `false`.
|
|
17
|
+
*/
|
|
18
|
+
readonly strictScalars?: boolean | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* The extension of the imports between the generated files. The default is `.ts`, which needs
|
|
21
|
+
* `allowImportingTsExtensions` or `rewriteRelativeImportExtensions` in the TypeScript configuration of the test
|
|
22
|
+
* suite.
|
|
23
|
+
*/
|
|
24
|
+
readonly importExtension?: ".ts" | ".js" | undefined;
|
|
25
|
+
}
|
|
26
|
+
/** A configuration for GraphQL Code Generator. */
|
|
27
|
+
export interface GraphqlCodegenConfig {
|
|
28
|
+
/** Whether to overwrite the generated files. */
|
|
29
|
+
readonly overwrite: boolean;
|
|
30
|
+
/** The GraphQL schema. */
|
|
31
|
+
readonly schema: string;
|
|
32
|
+
/** The operation documents. */
|
|
33
|
+
readonly documents: readonly string[];
|
|
34
|
+
/** The generated outputs by directory, with the preset and the settings of each. */
|
|
35
|
+
readonly generates: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns a GraphQL Code Generator configuration that generates typed documents for `GraphqlClient`.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* The configuration uses the client preset in the string document mode, so each operation becomes a typed document,
|
|
42
|
+
* such as `GetCartDocument`, that `GraphqlClient` accepts directly. Enums become string unions, and fragment masking is
|
|
43
|
+
* off.
|
|
44
|
+
*
|
|
45
|
+
* @param options - The schema, the operation documents, the output directory and the custom scalars.
|
|
46
|
+
* @returns The configuration to export from the codegen configuration file of the test suite.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* export default graphqlCodegenConfig({
|
|
50
|
+
* schema: "graphql/schema.graphql",
|
|
51
|
+
* documents: "graphql/operations/*.graphql",
|
|
52
|
+
* output: "graphql/generated/",
|
|
53
|
+
* scalars: { DateTime: "string", Decimal: "number" },
|
|
54
|
+
* });
|
|
55
|
+
*/
|
|
56
|
+
export declare function graphqlCodegenConfig(options: GraphqlCodegenOptions): GraphqlCodegenConfig;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a GraphQL Code Generator configuration that generates typed documents for `GraphqlClient`.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The configuration uses the client preset in the string document mode, so each operation becomes a typed document,
|
|
6
|
+
* such as `GetCartDocument`, that `GraphqlClient` accepts directly. Enums become string unions, and fragment masking is
|
|
7
|
+
* off.
|
|
8
|
+
*
|
|
9
|
+
* @param options - The schema, the operation documents, the output directory and the custom scalars.
|
|
10
|
+
* @returns The configuration to export from the codegen configuration file of the test suite.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* export default graphqlCodegenConfig({
|
|
14
|
+
* schema: "graphql/schema.graphql",
|
|
15
|
+
* documents: "graphql/operations/*.graphql",
|
|
16
|
+
* output: "graphql/generated/",
|
|
17
|
+
* scalars: { DateTime: "string", Decimal: "number" },
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
export function graphqlCodegenConfig(options) {
|
|
21
|
+
return {
|
|
22
|
+
overwrite: true,
|
|
23
|
+
schema: options.schema,
|
|
24
|
+
documents: typeof options.documents === "string" ? [options.documents] : [...options.documents],
|
|
25
|
+
generates: {
|
|
26
|
+
[options.output]: {
|
|
27
|
+
preset: "client",
|
|
28
|
+
presetConfig: { fragmentMasking: false },
|
|
29
|
+
config: {
|
|
30
|
+
documentMode: "string",
|
|
31
|
+
enumsAsTypes: true,
|
|
32
|
+
useTypeImports: true,
|
|
33
|
+
importExtension: options.importExtension ?? ".ts",
|
|
34
|
+
defaultScalarType: "unknown",
|
|
35
|
+
strictScalars: options.strictScalars ?? false,
|
|
36
|
+
scalars: { ...options.scalars },
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=graphql-codegen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql-codegen.js","sourceRoot":"","sources":["../../src/graphql/graphql-codegen.ts"],"names":[],"mappings":"AAsCA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAA8B;IACjE,OAAO;QACL,SAAS,EAAE,IAAI;QACf,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,SAAS,EAAE,OAAO,OAAO,CAAC,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC;QAC/F,SAAS,EAAE;YACT,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;gBAChB,MAAM,EAAE,QAAQ;gBAChB,YAAY,EAAE,EAAE,eAAe,EAAE,KAAK,EAAE;gBACxC,MAAM,EAAE;oBACN,YAAY,EAAE,QAAQ;oBACtB,YAAY,EAAE,IAAI;oBAClB,cAAc,EAAE,IAAI;oBACpB,eAAe,EAAE,OAAO,CAAC,eAAe,IAAI,KAAK;oBACjD,iBAAiB,EAAE,SAAS;oBAC5B,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,KAAK;oBAC7C,OAAO,EAAE,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE;iBAChC;aACF;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** The options of {@link graphqlCodegenConfig}. */\nexport interface GraphqlCodegenOptions {\n /** The GraphQL schema: a schema file, a glob of schema files, or the URL of a GraphQL endpoint. */\n readonly schema: string;\n /** The operation documents, as one glob or several, such as `graphql/operations/*.graphql`. */\n readonly documents: string | readonly string[];\n /** The directory that receives the generated files, such as `graphql/generated/`. */\n readonly output: string;\n /**\n * The TypeScript types of custom scalars by scalar name, such as `{ DateTime: \"string\" }`. Custom scalars that are\n * not listed are `unknown`, unless `strictScalars` is set.\n */\n readonly scalars?: Readonly<Record<string, string>> | undefined;\n /**\n * Whether the generation fails for a custom scalar that `scalars` does not list, so a new scalar of the schema is\n * noticed. The default is `false`.\n */\n readonly strictScalars?: boolean | undefined;\n /**\n * The extension of the imports between the generated files. The default is `.ts`, which needs\n * `allowImportingTsExtensions` or `rewriteRelativeImportExtensions` in the TypeScript configuration of the test\n * suite.\n */\n readonly importExtension?: \".ts\" | \".js\" | undefined;\n}\n\n/** A configuration for GraphQL Code Generator. */\nexport interface GraphqlCodegenConfig {\n /** Whether to overwrite the generated files. */\n readonly overwrite: boolean;\n /** The GraphQL schema. */\n readonly schema: string;\n /** The operation documents. */\n readonly documents: readonly string[];\n /** The generated outputs by directory, with the preset and the settings of each. */\n readonly generates: Readonly<Record<string, Readonly<Record<string, unknown>>>>;\n}\n\n/**\n * Returns a GraphQL Code Generator configuration that generates typed documents for `GraphqlClient`.\n *\n * @remarks\n * The configuration uses the client preset in the string document mode, so each operation becomes a typed document,\n * such as `GetCartDocument`, that `GraphqlClient` accepts directly. Enums become string unions, and fragment masking is\n * off.\n *\n * @param options - The schema, the operation documents, the output directory and the custom scalars.\n * @returns The configuration to export from the codegen configuration file of the test suite.\n *\n * @example\n * export default graphqlCodegenConfig({\n * schema: \"graphql/schema.graphql\",\n * documents: \"graphql/operations/*.graphql\",\n * output: \"graphql/generated/\",\n * scalars: { DateTime: \"string\", Decimal: \"number\" },\n * });\n */\nexport function graphqlCodegenConfig(options: GraphqlCodegenOptions): GraphqlCodegenConfig {\n return {\n overwrite: true,\n schema: options.schema,\n documents: typeof options.documents === \"string\" ? [options.documents] : [...options.documents],\n generates: {\n [options.output]: {\n preset: \"client\",\n presetConfig: { fragmentMasking: false },\n config: {\n documentMode: \"string\",\n enumsAsTypes: true,\n useTypeImports: true,\n importExtension: options.importExtension ?? \".ts\",\n defaultScalarType: \"unknown\",\n strictScalars: options.strictScalars ?? false,\n scalars: { ...options.scalars },\n },\n },\n },\n };\n}\n"]}
|