@gitlab/frontend-tests 0.0.1 → 0.1.2
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/README.md +112 -1
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/integration/constants.d.cts +3 -0
- package/dist/integration/constants.d.ts +3 -0
- package/dist/integration/fixture_utils.d.cts +107 -0
- package/dist/integration/fixture_utils.d.ts +107 -0
- package/dist/integration/fixture_variant_schema.d.cts +74 -0
- package/dist/integration/fixture_variant_schema.d.ts +74 -0
- package/dist/integration/handlers.d.cts +10 -0
- package/dist/integration/handlers.d.ts +10 -0
- package/dist/integration/index.d.cts +7 -0
- package/dist/integration/index.d.ts +7 -0
- package/dist/integration/jest_config.cjs +31 -0
- package/dist/integration/jest_config.d.cts +11 -0
- package/dist/integration/jest_config.d.ts +11 -0
- package/dist/integration/jest_config.js +27 -0
- package/dist/integration/operation_helpers.d.cts +70 -0
- package/dist/integration/operation_helpers.d.ts +70 -0
- package/dist/integration/polyfills.cjs +21 -0
- package/dist/integration/polyfills.d.cts +1 -0
- package/dist/integration/polyfills.d.ts +1 -0
- package/dist/integration/polyfills.js +19 -0
- package/dist/integration/polyfills_text_encoding.cjs +8 -0
- package/dist/integration/polyfills_text_encoding.d.cts +1 -0
- package/dist/integration/polyfills_text_encoding.d.ts +1 -0
- package/dist/integration/polyfills_text_encoding.js +6 -0
- package/dist/integration/server.d.cts +3 -0
- package/dist/integration/server.d.ts +3 -0
- package/dist/integration/types.d.cts +82 -0
- package/dist/integration/types.d.ts +82 -0
- package/dist/integration.cjs +38 -0
- package/dist/integration.js +5 -0
- package/dist/server-C2Drh977.js +3970 -0
- package/dist/server-hzT7AwDi.cjs +3999 -0
- package/integration/jest-config.js +2 -0
- package/integration/polyfills/text-encoding.js +2 -0
- package/integration/polyfills.js +2 -0
- package/integration.js +2 -0
- package/package.json +138 -5
package/README.md
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
1
|
# @gitlab/frontend-tests
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reusable test infrastructure for GitLab-owned frontend packages. It includes a shared Jest configuration for unit tests and a frontend integration test setup factory. Each package can set up its test infrastructure without copying files or following many manual steps.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn add --dev @gitlab/frontend-tests
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Development
|
|
12
|
+
|
|
13
|
+
The package is written in TypeScript and built with rollup. Tests use vitest. Linting uses ESLint 9 with a flat config and typescript-eslint.
|
|
14
|
+
|
|
15
|
+
`tsconfig.json` is the editor and `yarn typecheck` config and covers the tests. `tsconfig.build.json` extends it for rollup and excludes the tests so they stay out of `dist/`.
|
|
16
|
+
|
|
17
|
+
Relative imports in `src/` carry a `.js` extension (`./types.js`), which TypeScript maps to the `.ts` source. The emitted `.d.ts` files keep those specifiers, which `node16` module resolution requires. After rollup, `scripts/emit_cjs_types.mjs` copies every `.d.ts` to a `.d.cts` twin for the `require` export condition, so CommonJS TypeScript consumers resolve types too. `lodash-es` is bundled into `dist/` because it ships ESM only; the only runtime dependency is `@whatwg-node/fetch`.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
yarn install # install dependencies
|
|
21
|
+
yarn test # run tests
|
|
22
|
+
yarn build # build dist/ (ESM, CJS and .d.ts)
|
|
23
|
+
yarn lint # run ESLint
|
|
24
|
+
yarn typecheck # run tsc --noEmit
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Adding a module
|
|
28
|
+
|
|
29
|
+
1. Add the module under `src/`.
|
|
30
|
+
2. Add tests next to it as `*.test.ts`.
|
|
31
|
+
3. Re-export the public API from `src/index.ts`.
|
|
32
|
+
Put data shapes that several modules share, or that consumers write themselves, in `src/integration/types.ts`. Keep a type that describes one function's parameters or return value next to that function. Do not export implementation details. Name generic parameters with a single letter (`T`, `K`, `V`) or descriptively with a `Type` suffix (`DataType`), never with a `T` prefix; ESLint enforces this.
|
|
33
|
+
4. Add the new entry to the `input` map in `rollup.config.js`, to `exports` and `typesVersions` in `package.json`, and add a root resolver stub (see Gotchas). This publishes it as the subpath `@gitlab/frontend-tests/<name>`.
|
|
34
|
+
|
|
35
|
+
## Integration test core
|
|
36
|
+
|
|
37
|
+
This package holds the framework core of GitLab's frontend integration tests. These tests mount a real Vue app in jsdom. They intercept GraphQL and REST requests at the network layer with Mock Service Worker (MSW). The core provides fixture loading, fixture variants, request capture and the MSW server wiring. It also provides a Jest config factory and the polyfills MSW needs.
|
|
38
|
+
|
|
39
|
+
### Modules
|
|
40
|
+
|
|
41
|
+
`@gitlab/frontend-tests/integration` is the core. It exports:
|
|
42
|
+
|
|
43
|
+
- Fixture utilities: `loadFixturesMap(basePath)`, `matchFixture`, `isValidGraphqlFixture`, `setFixtureData`, `setFixtureErrors`, `setFixtureItemsCount`, `createFixturePaginator`. `loadFixturesMap` reads every `*.json` file in a directory and keys the results by camelCased operation name.
|
|
44
|
+
- Fixture variants: `defineFixtureVariants`, `defineRestFixtureVariants`, `setQueryVariant`, `activateVariant`, `getActiveVariant`, `getVariant`, `resetAllVariants`, `dumpRegistry`.
|
|
45
|
+
- Request capture: `captureRequest`, `capturedRequests`, `resetCapturedRequests`, `snapshotRequests`, `expectGraphQLCalls`, `lastRequestVariables`, `captureMissingOperation`, `missingOperations`, `clearMissingOperations`.
|
|
46
|
+
- MSW wiring: `buildHandlers(featureHandlers, restEndpoints)` and `createServer(featureHandlers, restEndpoints)`. `buildHandlers` creates one `graphql.operation` handler that tries each feature resolver in order. It also adds the REST endpoint handlers and a catch-all that answers 400. `createServer` builds an `msw/node` server from those handlers.
|
|
47
|
+
- `baseMetadata`.
|
|
48
|
+
- Types for consumers: `GraphqlFixture`, `GraphqlFixtureInput`, `GraphqlVariables`, `FeatureHandler`, `RestEndpoint`, `RestFixture`, `FixtureVariants`, `CapturedRequest` and `RequestCounts`. `setQueryVariant` derives its activator names from the variant keys, so `setQueryVariant(constant).withError()` is checked against the keys passed to `defineFixtureVariants`.
|
|
49
|
+
|
|
50
|
+
`@gitlab/frontend-tests/integration/polyfills/text-encoding` and `@gitlab/frontend-tests/integration/polyfills` are Jest `setupFiles` entries, in that order. The first puts `TextEncoder` and `TextDecoder` on `global`. The second adds the web streams, `BroadcastChannel`, and `fetch`, `Request`, `Response` and `Headers` from `@whatwg-node/fetch`.
|
|
51
|
+
|
|
52
|
+
`@gitlab/frontend-tests/integration/jest-config` exports `createFrontendIntegrationConfig(config)` and `MSW_ESM_DEPENDENCIES`.
|
|
53
|
+
|
|
54
|
+
### Jest configuration
|
|
55
|
+
|
|
56
|
+
Pass an existing Jest config to the factory. It returns a copy with the integration overrides applied.
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
const { createFrontendIntegrationConfig } = require('@gitlab/frontend-tests/integration/jest-config');
|
|
60
|
+
const baseConfig = require('./jest.config.base');
|
|
61
|
+
|
|
62
|
+
module.exports = createFrontendIntegrationConfig({
|
|
63
|
+
...baseConfig,
|
|
64
|
+
setupFilesAfterEnv: ['<rootDir>/spec/integration/test_setup.js'],
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The factory changes these things:
|
|
69
|
+
|
|
70
|
+
- Adds the ESM-only dependencies of msw (`rettime`, `until-async`, `@open-draft/*`) to the `transformIgnorePatterns` allowlist.
|
|
71
|
+
- Appends the two polyfills entries to `setupFiles`, text encoding first.
|
|
72
|
+
- Clears `testPathIgnorePatterns`.
|
|
73
|
+
- Disables global fake timers.
|
|
74
|
+
- Sets `testTimeout` to 20000 in CI and 7000 locally.
|
|
75
|
+
|
|
76
|
+
It does not touch `setupFilesAfterEnv`, module aliases or the test environment. Those stay with the consumer.
|
|
77
|
+
|
|
78
|
+
`msw` v2 is a peer dependency, and `@jest/types` is an optional peer that only TypeScript consumers of `jest-config` need. `@whatwg-node/fetch` is the only regular dependency; `lodash-es` is bundled.
|
|
79
|
+
|
|
80
|
+
### Gotchas
|
|
81
|
+
|
|
82
|
+
- Load the polyfills through `setupFiles`, not `setupFilesAfterEnv`. `msw/node` reads these globals while its module body evaluates, so they must exist before it loads. Keep the text-encoding entry before the main one: `@whatwg-node/fetch` needs `TextDecoder` at load time, and bundlers hoist imports, so a single file cannot guarantee the order.
|
|
83
|
+
- The root-level `integration.js` and `integration/*.js` files are stubs for resolvers that ignore the `exports` field, such as webpack 4 and the ESLint import resolver built on it. Node and Jest never load them. Add one for every new subpath.
|
|
84
|
+
- Make sure every import resolves to the same copy of this package. The variant registry and the captured-requests store are module-level singletons. With two copies, a variant activated in a spec is invisible to the handler.
|
|
85
|
+
|
|
86
|
+
## Publishing
|
|
87
|
+
|
|
88
|
+
The package is published to the public npm registry (registry.npmjs.org), not the GitLab package registry. Only `dist/` is published.
|
|
89
|
+
|
|
90
|
+
Publishing uses npm trusted publishing with OIDC. The `publish` job gets a short-lived ID token from GitLab CI (`id_tokens`, audience `npm:registry.npmjs.org`). No npm token is stored in CI variables. Packages are published with provenance attestations.
|
|
91
|
+
|
|
92
|
+
The `publish` job runs only on git tags that match `v<digits>`, for example `v0.1.0`. It runs after lint, typecheck, test and build pass.
|
|
93
|
+
|
|
94
|
+
To release:
|
|
95
|
+
|
|
96
|
+
1. Bump `version` in `package.json` in a merge request.
|
|
97
|
+
2. Merge the merge request.
|
|
98
|
+
3. Create and push a tag `vX.Y.Z` on `main`. The tag must match the version.
|
|
99
|
+
|
|
100
|
+
The tag pipeline publishes the package.
|
|
101
|
+
|
|
102
|
+
### One-time setup
|
|
103
|
+
|
|
104
|
+
This is required before the first CI publish. Someone with publish rights on the `@gitlab` npm organization must do it.
|
|
105
|
+
|
|
106
|
+
1. Publish the first version by hand from a logged-in machine with `npm publish --access public`. The trusted publisher is set on an existing package's settings on npmjs.com, so the package must exist first.
|
|
107
|
+
2. On npmjs.com, open the package Settings and find the Trusted Publisher section. Choose GitLab CI/CD and enter:
|
|
108
|
+
- Namespace: `gitlab-org/frontend`
|
|
109
|
+
- Project name: `tests`
|
|
110
|
+
- Top-level CI file path: `.gitlab-ci.yml`
|
|
111
|
+
- Allow `npm publish`
|
|
112
|
+
3. Optional: under Publishing access, choose "Require two-factor authentication and disallow tokens".
|
|
113
|
+
|
|
114
|
+
After this, tag pipelines publish on their own.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var server = require('./server-hzT7AwDi.cjs');
|
|
4
|
+
require('msw/node');
|
|
5
|
+
require('msw');
|
|
6
|
+
require('node:fs');
|
|
7
|
+
require('node:path');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.activateVariant = server.activateVariant;
|
|
12
|
+
exports.baseMetadata = server.baseMetadata;
|
|
13
|
+
exports.buildHandlers = server.buildHandlers;
|
|
14
|
+
exports.captureMissingOperation = server.captureMissingOperation;
|
|
15
|
+
exports.captureRequest = server.captureRequest;
|
|
16
|
+
exports.capturedRequests = server.capturedRequests;
|
|
17
|
+
exports.clearMissingOperations = server.clearMissingOperations;
|
|
18
|
+
exports.createFixturePaginator = server.createFixturePaginator;
|
|
19
|
+
exports.createServer = server.createServer;
|
|
20
|
+
exports.defineFixtureVariants = server.defineFixtureVariants;
|
|
21
|
+
exports.defineRestFixtureVariants = server.defineRestFixtureVariants;
|
|
22
|
+
exports.dumpRegistry = server.dumpRegistry;
|
|
23
|
+
exports.expectGraphQLCalls = server.expectGraphQLCalls;
|
|
24
|
+
exports.getActiveVariant = server.getActiveVariant;
|
|
25
|
+
exports.getVariant = server.getVariant;
|
|
26
|
+
exports.isValidGraphqlFixture = server.isValidGraphqlFixture;
|
|
27
|
+
exports.lastRequestVariables = server.lastRequestVariables;
|
|
28
|
+
exports.loadFixturesMap = server.loadFixturesMap;
|
|
29
|
+
exports.matchFixture = server.matchFixture;
|
|
30
|
+
exports.missingOperations = server.missingOperations;
|
|
31
|
+
exports.normaliseGraphqlFixture = server.normaliseGraphqlFixture;
|
|
32
|
+
exports.resetAllVariants = server.resetAllVariants;
|
|
33
|
+
exports.resetCapturedRequests = server.resetCapturedRequests;
|
|
34
|
+
exports.setFixtureData = server.setFixtureData;
|
|
35
|
+
exports.setFixtureErrors = server.setFixtureErrors;
|
|
36
|
+
exports.setFixtureItemsCount = server.setFixtureItemsCount;
|
|
37
|
+
exports.setQueryVariant = server.setQueryVariant;
|
|
38
|
+
exports.snapshotRequests = server.snapshotRequests;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './integration/index.cjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './integration/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { a as activateVariant, b as baseMetadata, c as buildHandlers, d as captureMissingOperation, e as captureRequest, f as capturedRequests, g as clearMissingOperations, h as createFixturePaginator, i as createServer, j as defineFixtureVariants, k as defineRestFixtureVariants, l as dumpRegistry, m as expectGraphQLCalls, n as getActiveVariant, o as getVariant, p as isValidGraphqlFixture, q as lastRequestVariables, r as loadFixturesMap, s as matchFixture, t as missingOperations, u as normaliseGraphqlFixture, v as resetAllVariants, w as resetCapturedRequests, x as setFixtureData, y as setFixtureErrors, z as setFixtureItemsCount, A as setQueryVariant, B as snapshotRequests } from './server-C2Drh977.js';
|
|
2
|
+
import 'msw/node';
|
|
3
|
+
import 'msw';
|
|
4
|
+
import 'node:fs';
|
|
5
|
+
import 'node:path';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { FixtureTable, FixtureTableEntry, GraphqlFixture, GraphqlFixtureInput, GraphqlVariables, RestFixture } from './types.cjs';
|
|
2
|
+
type UnknownRecord = Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Picks which recorded fixture to serve for a request, based on its variables. An
|
|
5
|
+
* operation recorded under several filters or pages keeps one row per recording in
|
|
6
|
+
* a fixture table, and this returns the row that fits the request.
|
|
7
|
+
*
|
|
8
|
+
* A request no row fits is a missing fixture: it throws, names the table to add a
|
|
9
|
+
* row to, and records the miss for the run's summary.
|
|
10
|
+
*
|
|
11
|
+
* @param label - How the error message refers to the table, usually the operation name.
|
|
12
|
+
*/
|
|
13
|
+
export declare function matchFixture<OperationVariables, Fixture>(variables: OperationVariables, table: FixtureTable<OperationVariables, Fixture>, label: string): FixtureTableEntry<OperationVariables, Fixture>;
|
|
14
|
+
/**
|
|
15
|
+
* Fills in the empty `errors` array a recorded success leaves out, so handlers can
|
|
16
|
+
* serve the fixture as a complete GraphQL response.
|
|
17
|
+
*/
|
|
18
|
+
export declare function normaliseGraphqlFixture(input: GraphqlFixtureInput): GraphqlFixture;
|
|
19
|
+
/**
|
|
20
|
+
* Loads a directory of recorded responses so a handler file can refer to each one
|
|
21
|
+
* by operation name instead of reading JSON by hand. File names become camelCased
|
|
22
|
+
* keys. GraphQL responses always come back with an `errors` array so handlers can
|
|
23
|
+
* serve them without checking for it. REST payloads are stored untouched.
|
|
24
|
+
*/
|
|
25
|
+
export declare function loadFixturesMap<T extends UnknownRecord = Record<string, GraphqlFixture | RestFixture>>(basePath: string): T;
|
|
26
|
+
/** Whether a value has the shape of a recorded GraphQL response: an object `data` and, if present, an array `errors`. */
|
|
27
|
+
export declare function isValidGraphqlFixture(fixture: unknown): fixture is GraphqlFixture;
|
|
28
|
+
type PageVariableName = 'after' | 'before' | 'first' | 'last';
|
|
29
|
+
type PageVariableNames = Record<PageVariableName, string>;
|
|
30
|
+
/**
|
|
31
|
+
* Turns a recorded success into an error response, for specs that cover how the UI
|
|
32
|
+
* reacts when the backend rejects a request. The result carries the given messages
|
|
33
|
+
* and no data. The original fixture is left untouched.
|
|
34
|
+
*
|
|
35
|
+
* @throws {Error} If `errors` is not an array of strings.
|
|
36
|
+
*/
|
|
37
|
+
export declare function setFixtureErrors(fixture: GraphqlFixture, errors: string[]): GraphqlFixture;
|
|
38
|
+
/** A key name, or `{ path }` for an exact lodash path relative to `fixture.data`. */
|
|
39
|
+
export type FixtureSelector = string | {
|
|
40
|
+
path: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Produces a copy of a fixture with one value changed, so a spec can describe the
|
|
44
|
+
* state it needs ("this item is confidential") instead of maintaining a second
|
|
45
|
+
* recording.
|
|
46
|
+
*
|
|
47
|
+
* Name the key to change and the first occurrence in the data is updated. When the
|
|
48
|
+
* same key appears in more than one place (an `awardEmoji` connection and an
|
|
49
|
+
* `awardEmoji` permission, say), pass `{ path }` with a lodash path from
|
|
50
|
+
* `fixture.data` to pick one exactly.
|
|
51
|
+
*
|
|
52
|
+
* @throws {Error} If `selector` is not a non-empty string or a `{ path }` object.
|
|
53
|
+
*/
|
|
54
|
+
export declare function setFixtureData<T extends GraphqlFixture>(fixture: T, selector: FixtureSelector, data: unknown): T;
|
|
55
|
+
export interface SetFixtureItemsCountOptions<T extends GraphqlFixture> {
|
|
56
|
+
/** The fixture object to modify. */
|
|
57
|
+
fixture: T;
|
|
58
|
+
/** The key of the connection to update. */
|
|
59
|
+
lookupKey: string;
|
|
60
|
+
/** The desired count of items in the connection. */
|
|
61
|
+
itemCount: number;
|
|
62
|
+
/** The name of the scalar count field, if any. */
|
|
63
|
+
countKey?: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Produces a copy of a fixture whose connection holds exactly `itemCount` items, for
|
|
67
|
+
* specs that need an empty list, a single row or more rows than were recorded.
|
|
68
|
+
* Extra items are cloned from the existing ones, and a scalar count field such as
|
|
69
|
+
* `count` is kept in step. Count-only connections just get the new count.
|
|
70
|
+
*
|
|
71
|
+
* @throws {Error} If the fixture, key or count is invalid, or the connection has no
|
|
72
|
+
* item to clone from.
|
|
73
|
+
*/
|
|
74
|
+
export declare function setFixtureItemsCount<T extends GraphqlFixture>({ fixture, lookupKey, itemCount, countKey, }: SetFixtureItemsCountOptions<T>): T;
|
|
75
|
+
export interface FixturePaginatorOptions {
|
|
76
|
+
/** The key of the connection to paginate. */
|
|
77
|
+
lookupKey: string;
|
|
78
|
+
/**
|
|
79
|
+
* Request variable names for `after`, `before`, `first` and `last`, when the
|
|
80
|
+
* operation names them differently.
|
|
81
|
+
*/
|
|
82
|
+
variableNames?: Partial<PageVariableNames>;
|
|
83
|
+
/** Page size when the request sends no `first`/`last`. Defaults to 20. */
|
|
84
|
+
pageSize?: number;
|
|
85
|
+
/** The scalar count field, kept at the total. Defaults to `count`. */
|
|
86
|
+
countKey?: string;
|
|
87
|
+
}
|
|
88
|
+
/** Serves one page of `fixture`'s connection for the request `variables`. */
|
|
89
|
+
export type FixturePaginator = <T extends GraphqlFixture>(fixture: T, variables?: GraphqlVariables) => T;
|
|
90
|
+
/**
|
|
91
|
+
* Lets one recording stand in for every page of a paginated list, so a spec can
|
|
92
|
+
* exercise "load more" and cursor navigation without recording each page. The
|
|
93
|
+
* returned function takes the fixture and the request variables and serves the
|
|
94
|
+
* page the request asked for. Use `setFixtureItemsCount` first to decide how many
|
|
95
|
+
* items the full list holds.
|
|
96
|
+
*
|
|
97
|
+
* Cursors are made up here, so a cursor copied from a real recording is rejected
|
|
98
|
+
* instead of quietly serving the first page.
|
|
99
|
+
*
|
|
100
|
+
* @throws {Error} If `lookupKey` is invalid.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* const paginateWorkItems = createFixturePaginator({ lookupKey: 'workItems' });
|
|
104
|
+
* paginateWorkItems(getActiveVariant('getWorkItemsFullEE') ?? base, variables);
|
|
105
|
+
*/
|
|
106
|
+
export declare function createFixturePaginator({ lookupKey, variableNames, pageSize, countKey, }: FixturePaginatorOptions): FixturePaginator;
|
|
107
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { FixtureTable, FixtureTableEntry, GraphqlFixture, GraphqlFixtureInput, GraphqlVariables, RestFixture } from './types.js';
|
|
2
|
+
type UnknownRecord = Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Picks which recorded fixture to serve for a request, based on its variables. An
|
|
5
|
+
* operation recorded under several filters or pages keeps one row per recording in
|
|
6
|
+
* a fixture table, and this returns the row that fits the request.
|
|
7
|
+
*
|
|
8
|
+
* A request no row fits is a missing fixture: it throws, names the table to add a
|
|
9
|
+
* row to, and records the miss for the run's summary.
|
|
10
|
+
*
|
|
11
|
+
* @param label - How the error message refers to the table, usually the operation name.
|
|
12
|
+
*/
|
|
13
|
+
export declare function matchFixture<OperationVariables, Fixture>(variables: OperationVariables, table: FixtureTable<OperationVariables, Fixture>, label: string): FixtureTableEntry<OperationVariables, Fixture>;
|
|
14
|
+
/**
|
|
15
|
+
* Fills in the empty `errors` array a recorded success leaves out, so handlers can
|
|
16
|
+
* serve the fixture as a complete GraphQL response.
|
|
17
|
+
*/
|
|
18
|
+
export declare function normaliseGraphqlFixture(input: GraphqlFixtureInput): GraphqlFixture;
|
|
19
|
+
/**
|
|
20
|
+
* Loads a directory of recorded responses so a handler file can refer to each one
|
|
21
|
+
* by operation name instead of reading JSON by hand. File names become camelCased
|
|
22
|
+
* keys. GraphQL responses always come back with an `errors` array so handlers can
|
|
23
|
+
* serve them without checking for it. REST payloads are stored untouched.
|
|
24
|
+
*/
|
|
25
|
+
export declare function loadFixturesMap<T extends UnknownRecord = Record<string, GraphqlFixture | RestFixture>>(basePath: string): T;
|
|
26
|
+
/** Whether a value has the shape of a recorded GraphQL response: an object `data` and, if present, an array `errors`. */
|
|
27
|
+
export declare function isValidGraphqlFixture(fixture: unknown): fixture is GraphqlFixture;
|
|
28
|
+
type PageVariableName = 'after' | 'before' | 'first' | 'last';
|
|
29
|
+
type PageVariableNames = Record<PageVariableName, string>;
|
|
30
|
+
/**
|
|
31
|
+
* Turns a recorded success into an error response, for specs that cover how the UI
|
|
32
|
+
* reacts when the backend rejects a request. The result carries the given messages
|
|
33
|
+
* and no data. The original fixture is left untouched.
|
|
34
|
+
*
|
|
35
|
+
* @throws {Error} If `errors` is not an array of strings.
|
|
36
|
+
*/
|
|
37
|
+
export declare function setFixtureErrors(fixture: GraphqlFixture, errors: string[]): GraphqlFixture;
|
|
38
|
+
/** A key name, or `{ path }` for an exact lodash path relative to `fixture.data`. */
|
|
39
|
+
export type FixtureSelector = string | {
|
|
40
|
+
path: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Produces a copy of a fixture with one value changed, so a spec can describe the
|
|
44
|
+
* state it needs ("this item is confidential") instead of maintaining a second
|
|
45
|
+
* recording.
|
|
46
|
+
*
|
|
47
|
+
* Name the key to change and the first occurrence in the data is updated. When the
|
|
48
|
+
* same key appears in more than one place (an `awardEmoji` connection and an
|
|
49
|
+
* `awardEmoji` permission, say), pass `{ path }` with a lodash path from
|
|
50
|
+
* `fixture.data` to pick one exactly.
|
|
51
|
+
*
|
|
52
|
+
* @throws {Error} If `selector` is not a non-empty string or a `{ path }` object.
|
|
53
|
+
*/
|
|
54
|
+
export declare function setFixtureData<T extends GraphqlFixture>(fixture: T, selector: FixtureSelector, data: unknown): T;
|
|
55
|
+
export interface SetFixtureItemsCountOptions<T extends GraphqlFixture> {
|
|
56
|
+
/** The fixture object to modify. */
|
|
57
|
+
fixture: T;
|
|
58
|
+
/** The key of the connection to update. */
|
|
59
|
+
lookupKey: string;
|
|
60
|
+
/** The desired count of items in the connection. */
|
|
61
|
+
itemCount: number;
|
|
62
|
+
/** The name of the scalar count field, if any. */
|
|
63
|
+
countKey?: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Produces a copy of a fixture whose connection holds exactly `itemCount` items, for
|
|
67
|
+
* specs that need an empty list, a single row or more rows than were recorded.
|
|
68
|
+
* Extra items are cloned from the existing ones, and a scalar count field such as
|
|
69
|
+
* `count` is kept in step. Count-only connections just get the new count.
|
|
70
|
+
*
|
|
71
|
+
* @throws {Error} If the fixture, key or count is invalid, or the connection has no
|
|
72
|
+
* item to clone from.
|
|
73
|
+
*/
|
|
74
|
+
export declare function setFixtureItemsCount<T extends GraphqlFixture>({ fixture, lookupKey, itemCount, countKey, }: SetFixtureItemsCountOptions<T>): T;
|
|
75
|
+
export interface FixturePaginatorOptions {
|
|
76
|
+
/** The key of the connection to paginate. */
|
|
77
|
+
lookupKey: string;
|
|
78
|
+
/**
|
|
79
|
+
* Request variable names for `after`, `before`, `first` and `last`, when the
|
|
80
|
+
* operation names them differently.
|
|
81
|
+
*/
|
|
82
|
+
variableNames?: Partial<PageVariableNames>;
|
|
83
|
+
/** Page size when the request sends no `first`/`last`. Defaults to 20. */
|
|
84
|
+
pageSize?: number;
|
|
85
|
+
/** The scalar count field, kept at the total. Defaults to `count`. */
|
|
86
|
+
countKey?: string;
|
|
87
|
+
}
|
|
88
|
+
/** Serves one page of `fixture`'s connection for the request `variables`. */
|
|
89
|
+
export type FixturePaginator = <T extends GraphqlFixture>(fixture: T, variables?: GraphqlVariables) => T;
|
|
90
|
+
/**
|
|
91
|
+
* Lets one recording stand in for every page of a paginated list, so a spec can
|
|
92
|
+
* exercise "load more" and cursor navigation without recording each page. The
|
|
93
|
+
* returned function takes the fixture and the request variables and serves the
|
|
94
|
+
* page the request asked for. Use `setFixtureItemsCount` first to decide how many
|
|
95
|
+
* items the full list holds.
|
|
96
|
+
*
|
|
97
|
+
* Cursors are made up here, so a cursor copied from a real recording is rejected
|
|
98
|
+
* instead of quietly serving the first page.
|
|
99
|
+
*
|
|
100
|
+
* @throws {Error} If `lookupKey` is invalid.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* const paginateWorkItems = createFixturePaginator({ lookupKey: 'workItems' });
|
|
104
|
+
* paginateWorkItems(getActiveVariant('getWorkItemsFullEE') ?? base, variables);
|
|
105
|
+
*/
|
|
106
|
+
export declare function createFixturePaginator({ lookupKey, variableNames, pageSize, countKey, }: FixturePaginatorOptions): FixturePaginator;
|
|
107
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { FixtureVariants, GraphqlError, GraphqlFixture, GraphqlFixtureInput, RestFixture, VariantActivators } from './types.cjs';
|
|
2
|
+
/**
|
|
3
|
+
* Switches an operation to one of its variants by name. Specs should use
|
|
4
|
+
* `setQueryVariant` instead, which has the names checked by the type system. This
|
|
5
|
+
* exists for the rare shared helper that receives the variant key as a runtime value.
|
|
6
|
+
*/
|
|
7
|
+
export declare function activateVariant(query: string, variantKey: string): void;
|
|
8
|
+
/** Variant maps must declare `BASE`; the other keys are free-form. */
|
|
9
|
+
type VariantsInput<T> = {
|
|
10
|
+
BASE: T;
|
|
11
|
+
} & Record<string, T>;
|
|
12
|
+
type NormalisedGraphqlVariants<V extends Record<string, GraphqlFixtureInput>> = {
|
|
13
|
+
[K in keyof V]: V[K] & {
|
|
14
|
+
errors: GraphqlError[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export interface DefineFixtureVariantsOptions<V> {
|
|
18
|
+
/** The camelCase GraphQL operation name (must match the MSW handler key). */
|
|
19
|
+
query: string;
|
|
20
|
+
/** A map of UPPER_SNAKE_CASE variant keys to fixtures. `BASE` is required. */
|
|
21
|
+
variants: V;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Declares the alternate responses an operation can serve, so a spec can switch
|
|
25
|
+
* between them with one call instead of wiring a handler per case. Every variant
|
|
26
|
+
* file calls this and exports the result as its default export. `BASE` is required:
|
|
27
|
+
* it is what the handler serves until a spec asks for something else, and the
|
|
28
|
+
* recording the other variants are derived from.
|
|
29
|
+
*
|
|
30
|
+
* @returns The validated variants object (frozen).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* export default defineFixtureVariants({
|
|
34
|
+
* query: 'namespaceWorkItem',
|
|
35
|
+
* variants: {
|
|
36
|
+
* BASE: namespaceWorkItemFixture,
|
|
37
|
+
* WITH_ERROR: setFixtureErrors(cloneDeep(namespaceWorkItemFixture), ['Something went wrong']),
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
*/
|
|
41
|
+
export declare function defineFixtureVariants<V extends VariantsInput<GraphqlFixtureInput>>({ query, variants, }: DefineFixtureVariantsOptions<V>): FixtureVariants<NormalisedGraphqlVariants<V>>;
|
|
42
|
+
/**
|
|
43
|
+
* The REST counterpart of `defineFixtureVariants`. Each variant is a raw JSON body
|
|
44
|
+
* served as is. Attach the result to the endpoint as its `variants` and
|
|
45
|
+
* `buildHandlers` serves whichever one is active. `query` is only the registry key.
|
|
46
|
+
* By convention it is the fixture's camelCase name, such as `restWorkItemsList`.
|
|
47
|
+
*
|
|
48
|
+
* @returns The validated variants object (frozen).
|
|
49
|
+
*/
|
|
50
|
+
export declare function defineRestFixtureVariants<V extends VariantsInput<RestFixture>>({ query, variants, }: DefineFixtureVariantsOptions<V>): FixtureVariants<V>;
|
|
51
|
+
/**
|
|
52
|
+
* Puts an operation into one of its states for the rest of the test, written as
|
|
53
|
+
* `setQueryVariant(namespaceWorkItem).archived()`. Pass the variant file's default
|
|
54
|
+
* export and you get one method per variant, named after its key, so a typo in a
|
|
55
|
+
* variant name is a type error and autocomplete lists only this operation's states.
|
|
56
|
+
*/
|
|
57
|
+
export declare function setQueryVariant<V extends FixtureVariants<Record<string, unknown>>>(queryConstant: V): VariantActivators<V>;
|
|
58
|
+
/** Looks up one of an operation's variants by key. Null when nothing is registered under that pair. */
|
|
59
|
+
export declare function getVariant<T = GraphqlFixture>(query: string, variantKey: string): T | null;
|
|
60
|
+
/**
|
|
61
|
+
* Tells a handler whether a spec has switched this operation to a variant. Returns
|
|
62
|
+
* that variant when it has, and null while the operation sits at `BASE`, so the
|
|
63
|
+
* handler can fall back to its own default, often a fixture table lookup.
|
|
64
|
+
*/
|
|
65
|
+
export declare function getActiveVariant<T = GraphqlFixture>(query: string): T | null;
|
|
66
|
+
/** Returns every operation to `BASE` so one spec's state never leaks into the next. Call it from `afterEach`. */
|
|
67
|
+
export declare function resetAllVariants(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Lists every registered operation and its variant keys, without the fixtures
|
|
70
|
+
* themselves. Meant for tooling that generates a manifest of available variants,
|
|
71
|
+
* not for use inside a test.
|
|
72
|
+
*/
|
|
73
|
+
export declare function dumpRegistry(): Record<string, string[]>;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { FixtureVariants, GraphqlError, GraphqlFixture, GraphqlFixtureInput, RestFixture, VariantActivators } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Switches an operation to one of its variants by name. Specs should use
|
|
4
|
+
* `setQueryVariant` instead, which has the names checked by the type system. This
|
|
5
|
+
* exists for the rare shared helper that receives the variant key as a runtime value.
|
|
6
|
+
*/
|
|
7
|
+
export declare function activateVariant(query: string, variantKey: string): void;
|
|
8
|
+
/** Variant maps must declare `BASE`; the other keys are free-form. */
|
|
9
|
+
type VariantsInput<T> = {
|
|
10
|
+
BASE: T;
|
|
11
|
+
} & Record<string, T>;
|
|
12
|
+
type NormalisedGraphqlVariants<V extends Record<string, GraphqlFixtureInput>> = {
|
|
13
|
+
[K in keyof V]: V[K] & {
|
|
14
|
+
errors: GraphqlError[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export interface DefineFixtureVariantsOptions<V> {
|
|
18
|
+
/** The camelCase GraphQL operation name (must match the MSW handler key). */
|
|
19
|
+
query: string;
|
|
20
|
+
/** A map of UPPER_SNAKE_CASE variant keys to fixtures. `BASE` is required. */
|
|
21
|
+
variants: V;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Declares the alternate responses an operation can serve, so a spec can switch
|
|
25
|
+
* between them with one call instead of wiring a handler per case. Every variant
|
|
26
|
+
* file calls this and exports the result as its default export. `BASE` is required:
|
|
27
|
+
* it is what the handler serves until a spec asks for something else, and the
|
|
28
|
+
* recording the other variants are derived from.
|
|
29
|
+
*
|
|
30
|
+
* @returns The validated variants object (frozen).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* export default defineFixtureVariants({
|
|
34
|
+
* query: 'namespaceWorkItem',
|
|
35
|
+
* variants: {
|
|
36
|
+
* BASE: namespaceWorkItemFixture,
|
|
37
|
+
* WITH_ERROR: setFixtureErrors(cloneDeep(namespaceWorkItemFixture), ['Something went wrong']),
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
*/
|
|
41
|
+
export declare function defineFixtureVariants<V extends VariantsInput<GraphqlFixtureInput>>({ query, variants, }: DefineFixtureVariantsOptions<V>): FixtureVariants<NormalisedGraphqlVariants<V>>;
|
|
42
|
+
/**
|
|
43
|
+
* The REST counterpart of `defineFixtureVariants`. Each variant is a raw JSON body
|
|
44
|
+
* served as is. Attach the result to the endpoint as its `variants` and
|
|
45
|
+
* `buildHandlers` serves whichever one is active. `query` is only the registry key.
|
|
46
|
+
* By convention it is the fixture's camelCase name, such as `restWorkItemsList`.
|
|
47
|
+
*
|
|
48
|
+
* @returns The validated variants object (frozen).
|
|
49
|
+
*/
|
|
50
|
+
export declare function defineRestFixtureVariants<V extends VariantsInput<RestFixture>>({ query, variants, }: DefineFixtureVariantsOptions<V>): FixtureVariants<V>;
|
|
51
|
+
/**
|
|
52
|
+
* Puts an operation into one of its states for the rest of the test, written as
|
|
53
|
+
* `setQueryVariant(namespaceWorkItem).archived()`. Pass the variant file's default
|
|
54
|
+
* export and you get one method per variant, named after its key, so a typo in a
|
|
55
|
+
* variant name is a type error and autocomplete lists only this operation's states.
|
|
56
|
+
*/
|
|
57
|
+
export declare function setQueryVariant<V extends FixtureVariants<Record<string, unknown>>>(queryConstant: V): VariantActivators<V>;
|
|
58
|
+
/** Looks up one of an operation's variants by key. Null when nothing is registered under that pair. */
|
|
59
|
+
export declare function getVariant<T = GraphqlFixture>(query: string, variantKey: string): T | null;
|
|
60
|
+
/**
|
|
61
|
+
* Tells a handler whether a spec has switched this operation to a variant. Returns
|
|
62
|
+
* that variant when it has, and null while the operation sits at `BASE`, so the
|
|
63
|
+
* handler can fall back to its own default, often a fixture table lookup.
|
|
64
|
+
*/
|
|
65
|
+
export declare function getActiveVariant<T = GraphqlFixture>(query: string): T | null;
|
|
66
|
+
/** Returns every operation to `BASE` so one spec's state never leaks into the next. Call it from `afterEach`. */
|
|
67
|
+
export declare function resetAllVariants(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Lists every registered operation and its variant keys, without the fixtures
|
|
70
|
+
* themselves. Meant for tooling that generates a manifest of available variants,
|
|
71
|
+
* not for use inside a test.
|
|
72
|
+
*/
|
|
73
|
+
export declare function dumpRegistry(): Record<string, string[]>;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type RequestHandler } from 'msw';
|
|
2
|
+
import type { FeatureHandler, RestEndpoint } from './types.cjs';
|
|
3
|
+
/**
|
|
4
|
+
* Assembles the MSW handlers for a test run from the feature handlers and REST
|
|
5
|
+
* endpoints a consumer registers. Every answered request is captured for
|
|
6
|
+
* assertions. A GraphQL operation nobody answers is recorded as missing and fails
|
|
7
|
+
* with a 400, and an unknown REST URL is logged and fails the same way, so a gap in
|
|
8
|
+
* the handlers surfaces as a loud test failure instead of a silent empty response.
|
|
9
|
+
*/
|
|
10
|
+
export declare function buildHandlers(allFeatureHandlers: FeatureHandler[], allRestEndpoints: RestEndpoint[]): RequestHandler[];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type RequestHandler } from 'msw';
|
|
2
|
+
import type { FeatureHandler, RestEndpoint } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Assembles the MSW handlers for a test run from the feature handlers and REST
|
|
5
|
+
* endpoints a consumer registers. Every answered request is captured for
|
|
6
|
+
* assertions. A GraphQL operation nobody answers is recorded as missing and fails
|
|
7
|
+
* with a 400, and an unknown REST URL is logged and fails the same way, so a gap in
|
|
8
|
+
* the handlers surfaces as a loud test failure instead of a silent empty response.
|
|
9
|
+
*/
|
|
10
|
+
export declare function buildHandlers(allFeatureHandlers: FeatureHandler[], allRestEndpoints: RestEndpoint[]): RequestHandler[];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// `msw` dependencies that ship ESM only, so Jest has to transform them.
|
|
4
|
+
const MSW_ESM_DEPENDENCIES = ['rettime', 'until-async', '@open-draft/.*'];
|
|
5
|
+
// Order matters: `@whatwg-node/fetch` needs TextEncoder/TextDecoder at load time.
|
|
6
|
+
const POLYFILLS_SETUP_FILES = [
|
|
7
|
+
'@gitlab/frontend-tests/integration/polyfills/text-encoding',
|
|
8
|
+
'@gitlab/frontend-tests/integration/polyfills',
|
|
9
|
+
];
|
|
10
|
+
/**
|
|
11
|
+
* Turns a consumer's Jest config into one that can run MSW integration specs. It
|
|
12
|
+
* lets Jest transform the ESM-only MSW dependencies, loads the polyfills MSW needs
|
|
13
|
+
* before anything else, keeps real timers and gives the specs a longer timeout.
|
|
14
|
+
* Module aliases, the test environment and `setupFilesAfterEnv` stay the
|
|
15
|
+
* consumer's responsibility.
|
|
16
|
+
*/
|
|
17
|
+
function createFrontendIntegrationConfig(config = {}) {
|
|
18
|
+
const { transformIgnorePatterns = [], setupFiles = [] } = config;
|
|
19
|
+
return {
|
|
20
|
+
...config,
|
|
21
|
+
transformIgnorePatterns: transformIgnorePatterns.map((pattern) => pattern.replace('node_modules/(?!(', `node_modules/(?!(${MSW_ESM_DEPENDENCIES.join('|')}|`)),
|
|
22
|
+
testPathIgnorePatterns: [],
|
|
23
|
+
setupFiles: [...setupFiles, ...POLYFILLS_SETUP_FILES],
|
|
24
|
+
fakeTimers: { enableGlobally: false },
|
|
25
|
+
testTimeout: process.env.CI ? 20000 : 7000,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.MSW_ESM_DEPENDENCIES = MSW_ESM_DEPENDENCIES;
|
|
30
|
+
exports.POLYFILLS_SETUP_FILES = POLYFILLS_SETUP_FILES;
|
|
31
|
+
exports.createFrontendIntegrationConfig = createFrontendIntegrationConfig;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Config } from '@jest/types';
|
|
2
|
+
export declare const MSW_ESM_DEPENDENCIES: string[];
|
|
3
|
+
export declare const POLYFILLS_SETUP_FILES: string[];
|
|
4
|
+
/**
|
|
5
|
+
* Turns a consumer's Jest config into one that can run MSW integration specs. It
|
|
6
|
+
* lets Jest transform the ESM-only MSW dependencies, loads the polyfills MSW needs
|
|
7
|
+
* before anything else, keeps real timers and gives the specs a longer timeout.
|
|
8
|
+
* Module aliases, the test environment and `setupFilesAfterEnv` stay the
|
|
9
|
+
* consumer's responsibility.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createFrontendIntegrationConfig(config?: Config.InitialOptions): Config.InitialOptions;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Config } from '@jest/types';
|
|
2
|
+
export declare const MSW_ESM_DEPENDENCIES: string[];
|
|
3
|
+
export declare const POLYFILLS_SETUP_FILES: string[];
|
|
4
|
+
/**
|
|
5
|
+
* Turns a consumer's Jest config into one that can run MSW integration specs. It
|
|
6
|
+
* lets Jest transform the ESM-only MSW dependencies, loads the polyfills MSW needs
|
|
7
|
+
* before anything else, keeps real timers and gives the specs a longer timeout.
|
|
8
|
+
* Module aliases, the test environment and `setupFilesAfterEnv` stay the
|
|
9
|
+
* consumer's responsibility.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createFrontendIntegrationConfig(config?: Config.InitialOptions): Config.InitialOptions;
|