graphql-data-generator 0.3.0-alpha.10 → 0.3.0-alpha.12

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/esm/jest.js CHANGED
@@ -19,11 +19,19 @@ afterEach(async () => {
19
19
  const diff = (a, b) => jestDiff(a, b, { omitAnnotationLines: true })
20
20
  ?.replace(/\w+ \{/g, "{") // Remove class names
21
21
  .replace(/\w+ \[/g, "["); // Remove array class names
22
+ const getOperationDefinition = (document) => document.definitions.find((d) => d.kind === Kind.OPERATION_DEFINITION);
23
+ const getOperationType = (operation) => getOperationDefinition(operation.query)?.operation ??
24
+ "<unknown operation type>";
25
+ const getOperationName = (document) => getOperationDefinition(document)?.name?.value;
26
+ const getOperationInfo = (document) => {
27
+ const def = getOperationDefinition(document);
28
+ return {
29
+ name: def?.name?.value ?? "<unknown operation>",
30
+ operationType: def?.operation ?? "<unknown operation type>",
31
+ };
32
+ };
22
33
  const getErrorMessage = (operation, mockLink) => {
23
- const definition = operation.query.definitions[0];
24
- const operationType = definition.kind === "OperationDefinition"
25
- ? definition.operation
26
- : "<unknown operation type>";
34
+ const operationType = getOperationType(operation);
27
35
  const key = JSON.stringify({
28
36
  query: print(operation.query),
29
37
  });
@@ -78,6 +86,55 @@ const AutoWatch = ({ mocks }) => {
78
86
  client.watchQuery(mock.request);
79
87
  return null;
80
88
  };
89
+ let lastMocks = [];
90
+ // deno-lint-ignore ban-types
91
+ const getStack = (to) => {
92
+ const obj = {};
93
+ Error.captureStackTrace(obj, to);
94
+ return obj.stack;
95
+ };
96
+ const _waitForMocks = async (mocks, cause) => {
97
+ for (const mock of mocks) {
98
+ if (mock.optional || mock.error)
99
+ continue;
100
+ await waitFor(() => {
101
+ if (currentSpecResult.failedExpectations.length)
102
+ return;
103
+ if (mock.result.mock.calls.length === 0) {
104
+ const { name, operationType } = getOperationInfo(mock.request.query);
105
+ const err = new Error(`Expected to have used ${operationType} ${name}${mock.request.variables
106
+ ? ` with variables ${dntShim.Deno.inspect(mock.request.variables, {
107
+ depth: Infinity,
108
+ colors: true,
109
+ })}`
110
+ : ""}`);
111
+ err.stack = `${mock.stack}${cause ? `\nCaused by: ${cause}` : ""}`;
112
+ throw err;
113
+ }
114
+ });
115
+ }
116
+ };
117
+ /**
118
+ * Wait for mocks to have been used.
119
+ * @param mock If `undefined`, waits for all mocks. If a number, waits fort he first `mocks` mocks. If a string, waits for all mocks up until and including that mock.
120
+ * @param offset If `mocks` is a string, grabs the `offset`th mock of that name (e.g., the third `getReport` mock)
121
+ */
122
+ export const waitForMocks = async (mock = lastMocks.length, offset = 0) => {
123
+ if (typeof mock === "string") {
124
+ const matches = lastMocks.map((m, i) => [m, i])
125
+ .filter(([m]) => getOperationName(m.request.query) === mock);
126
+ if (matches.length <= offset) {
127
+ fail({
128
+ name: "Error",
129
+ message: `Expected mock ${mock} to have been mocked`,
130
+ stack: getStack(waitForMocks),
131
+ });
132
+ }
133
+ expect(matches.length).toBeGreaterThan(offset);
134
+ mock = matches[offset][1] + 1;
135
+ }
136
+ await _waitForMocks(lastMocks.slice(0, mock), getStack(waitForMocks));
137
+ };
81
138
  /**
82
139
  * A wrapper for `@apollo/client/testing`, this component will assert all
83
140
  * requests have matching mocks and all defined mocks are used unless marked
@@ -100,29 +157,8 @@ export const MockProvider = ({ mocks, stack: renderStack, children, link: passed
100
157
  }]
101
158
  : []),
102
159
  ]);
103
- afterTest.push(async () => {
104
- if (currentSpecResult.failedExpectations.length)
105
- return;
106
- for (const mock of observableMocks) {
107
- if (mock.optional || mock.error)
108
- continue;
109
- await waitFor(() => {
110
- if (currentSpecResult.failedExpectations.length)
111
- return;
112
- if (mock.result.mock.calls.length === 0) {
113
- const operation = mock.request.query.definitions.find((d) => d.kind === Kind.OPERATION_DEFINITION);
114
- const err = new Error(`Expected to have used ${operation?.operation} ${operation?.name?.value}${mock.request.variables
115
- ? ` with variables ${dntShim.Deno.inspect(mock.request.variables, {
116
- depth: Infinity,
117
- colors: true,
118
- })}`
119
- : ""}`);
120
- err.stack = mock.stack ?? renderStack ?? err.stack;
121
- throw err;
122
- }
123
- }, { onTimeout: (e) => e });
124
- }
125
- });
160
+ lastMocks = observableMocks;
161
+ afterTest.push(() => _waitForMocks(lastMocks, renderStack));
126
162
  return observableMocks;
127
163
  }, [mocks]);
128
164
  const link = useMemo(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-data-generator",
3
- "version": "0.3.0-alpha.10",
3
+ "version": "0.3.0-alpha.12",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/voces/graphql-data-generator.git"
package/script/jest.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.MockProvider = exports.allowMissingMocks = exports.failRefetchWarnings = void 0;
26
+ exports.MockProvider = exports.waitForMocks = exports.allowMissingMocks = exports.failRefetchWarnings = void 0;
27
27
  const dntShim = __importStar(require("./_dnt.shims.js"));
28
28
  const react_1 = __importStar(require("react"));
29
29
  const client_1 = require("@apollo/client");
@@ -45,11 +45,19 @@ afterEach(async () => {
45
45
  const diff = (a, b) => (0, jest_diff_1.diff)(a, b, { omitAnnotationLines: true })
46
46
  ?.replace(/\w+ \{/g, "{") // Remove class names
47
47
  .replace(/\w+ \[/g, "["); // Remove array class names
48
+ const getOperationDefinition = (document) => document.definitions.find((d) => d.kind === graphql_1.Kind.OPERATION_DEFINITION);
49
+ const getOperationType = (operation) => getOperationDefinition(operation.query)?.operation ??
50
+ "<unknown operation type>";
51
+ const getOperationName = (document) => getOperationDefinition(document)?.name?.value;
52
+ const getOperationInfo = (document) => {
53
+ const def = getOperationDefinition(document);
54
+ return {
55
+ name: def?.name?.value ?? "<unknown operation>",
56
+ operationType: def?.operation ?? "<unknown operation type>",
57
+ };
58
+ };
48
59
  const getErrorMessage = (operation, mockLink) => {
49
- const definition = operation.query.definitions[0];
50
- const operationType = definition.kind === "OperationDefinition"
51
- ? definition.operation
52
- : "<unknown operation type>";
60
+ const operationType = getOperationType(operation);
53
61
  const key = JSON.stringify({
54
62
  query: (0, graphql_1.print)(operation.query),
55
63
  });
@@ -106,6 +114,56 @@ const AutoWatch = ({ mocks }) => {
106
114
  client.watchQuery(mock.request);
107
115
  return null;
108
116
  };
117
+ let lastMocks = [];
118
+ // deno-lint-ignore ban-types
119
+ const getStack = (to) => {
120
+ const obj = {};
121
+ Error.captureStackTrace(obj, to);
122
+ return obj.stack;
123
+ };
124
+ const _waitForMocks = async (mocks, cause) => {
125
+ for (const mock of mocks) {
126
+ if (mock.optional || mock.error)
127
+ continue;
128
+ await (0, dom_1.waitFor)(() => {
129
+ if (currentSpecResult.failedExpectations.length)
130
+ return;
131
+ if (mock.result.mock.calls.length === 0) {
132
+ const { name, operationType } = getOperationInfo(mock.request.query);
133
+ const err = new Error(`Expected to have used ${operationType} ${name}${mock.request.variables
134
+ ? ` with variables ${dntShim.Deno.inspect(mock.request.variables, {
135
+ depth: Infinity,
136
+ colors: true,
137
+ })}`
138
+ : ""}`);
139
+ err.stack = `${mock.stack}${cause ? `\nCaused by: ${cause}` : ""}`;
140
+ throw err;
141
+ }
142
+ });
143
+ }
144
+ };
145
+ /**
146
+ * Wait for mocks to have been used.
147
+ * @param mock If `undefined`, waits for all mocks. If a number, waits fort he first `mocks` mocks. If a string, waits for all mocks up until and including that mock.
148
+ * @param offset If `mocks` is a string, grabs the `offset`th mock of that name (e.g., the third `getReport` mock)
149
+ */
150
+ const waitForMocks = async (mock = lastMocks.length, offset = 0) => {
151
+ if (typeof mock === "string") {
152
+ const matches = lastMocks.map((m, i) => [m, i])
153
+ .filter(([m]) => getOperationName(m.request.query) === mock);
154
+ if (matches.length <= offset) {
155
+ fail({
156
+ name: "Error",
157
+ message: `Expected mock ${mock} to have been mocked`,
158
+ stack: getStack(exports.waitForMocks),
159
+ });
160
+ }
161
+ expect(matches.length).toBeGreaterThan(offset);
162
+ mock = matches[offset][1] + 1;
163
+ }
164
+ await _waitForMocks(lastMocks.slice(0, mock), getStack(exports.waitForMocks));
165
+ };
166
+ exports.waitForMocks = waitForMocks;
109
167
  /**
110
168
  * A wrapper for `@apollo/client/testing`, this component will assert all
111
169
  * requests have matching mocks and all defined mocks are used unless marked
@@ -128,29 +186,8 @@ const MockProvider = ({ mocks, stack: renderStack, children, link: passedLink, .
128
186
  }]
129
187
  : []),
130
188
  ]);
131
- afterTest.push(async () => {
132
- if (currentSpecResult.failedExpectations.length)
133
- return;
134
- for (const mock of observableMocks) {
135
- if (mock.optional || mock.error)
136
- continue;
137
- await (0, dom_1.waitFor)(() => {
138
- if (currentSpecResult.failedExpectations.length)
139
- return;
140
- if (mock.result.mock.calls.length === 0) {
141
- const operation = mock.request.query.definitions.find((d) => d.kind === graphql_1.Kind.OPERATION_DEFINITION);
142
- const err = new Error(`Expected to have used ${operation?.operation} ${operation?.name?.value}${mock.request.variables
143
- ? ` with variables ${dntShim.Deno.inspect(mock.request.variables, {
144
- depth: Infinity,
145
- colors: true,
146
- })}`
147
- : ""}`);
148
- err.stack = mock.stack ?? renderStack ?? err.stack;
149
- throw err;
150
- }
151
- }, { onTimeout: (e) => e });
152
- }
153
- });
189
+ lastMocks = observableMocks;
190
+ afterTest.push(() => _waitForMocks(lastMocks, renderStack));
154
191
  return observableMocks;
155
192
  }, [mocks]);
156
193
  const link = (0, react_1.useMemo)(() => {
package/types/jest.d.ts CHANGED
@@ -16,6 +16,12 @@ export declare const failRefetchWarnings: (value?: boolean) => boolean;
16
16
  * migration.
17
17
  */
18
18
  export declare const allowMissingMocks: (value: true) => true;
19
+ /**
20
+ * Wait for mocks to have been used.
21
+ * @param mock If `undefined`, waits for all mocks. If a number, waits fort he first `mocks` mocks. If a string, waits for all mocks up until and including that mock.
22
+ * @param offset If `mocks` is a string, grabs the `offset`th mock of that name (e.g., the third `getReport` mock)
23
+ */
24
+ export declare const waitForMocks: (mock?: number | string, offset?: number) => Promise<void>;
19
25
  /**
20
26
  * A wrapper for `@apollo/client/testing`, this component will assert all
21
27
  * requests have matching mocks and all defined mocks are used unless marked