graphql-data-generator 0.4.0-alpha.7 → 0.4.0-alpha.8

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 CHANGED
@@ -279,10 +279,38 @@ be helpful to disable asserting all requests are mocked. A helper,
279
279
  `allowMissingMocks`, exists to disable these assertions and can be called before
280
280
  any tests.
281
281
 
282
+ ### Issues
283
+
284
+ #### Refetch warnings
285
+
282
286
  If you are using an old version of `@apollo/client`, missing refetch requests
283
287
  will emit warnings instead of errors. You can use `failRefetchWarnings` to
284
288
  convert these warnings to errors.
285
289
 
290
+ #### Early unmounts
291
+
292
+ `@testing-library/react` automatically registers an `afterEach` hook to clean up
293
+ the DOM after each test. Similarly, `graphql-data-generator` adds its own
294
+ `afterEach` hook to verify that all mocks are consumed by the end of the test.
295
+
296
+ This can lead to unexpected errors or noisy logs if the DOM is unmounted before
297
+ `graphql-data-generator` runs its checks.
298
+
299
+ To avoid this, `graphql-data-generator` disables `@testing-library/react`'s
300
+ cleanup by default — **but only if it is loaded first**. If you can’t guarantee
301
+ the load order, you should manually disable the automatic cleanup:
302
+
303
+ ```ts
304
+ import "npm:@testing-library/react/dont-cleanup-after-each";
305
+ ```
306
+
307
+ If instead you want to disable `graphql-data-generator`'s cleanup behavior,
308
+ call:
309
+
310
+ ```ts
311
+ import { skipCleanupAfterEach } from "graphql-data-generator";
312
+ ```
313
+
286
314
  ## Extra
287
315
 
288
316
  The `init` function supports a 6th optional generic parameter, `Extra`, which
package/esm/jest.js CHANGED
@@ -8,6 +8,7 @@ import { Kind, print } from "graphql";
8
8
  import { diff as jestDiff } from "jest-diff";
9
9
  import "@testing-library/react/dont-cleanup-after-each";
10
10
  import { cleanup } from "@testing-library/react";
11
+ import { addTypenameToDocument } from "@apollo/client/utilities";
11
12
  let currentSpecResult;
12
13
  globalThis.jasmine
13
14
  ?.getEnv()
@@ -57,7 +58,7 @@ const getOperationInfo = (document) => {
57
58
  const getErrorMessage = (operation, mockLink) => {
58
59
  const operationType = getOperationType(operation);
59
60
  const key = JSON.stringify({
60
- query: print(operation.query),
61
+ query: print(addTypenameToDocument(operation.query)),
61
62
  });
62
63
  // Bypassing private variable
63
64
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -172,6 +173,15 @@ export const waitForMocks = async (mock = lastMocks.length, { timeout, offset =
172
173
  cause: getStack(waitForMocks),
173
174
  });
174
175
  };
176
+ const isDocument = (thing) => !!thing && typeof thing === "object" && "kind" in thing &&
177
+ thing.kind === "Document";
178
+ const getBareOperationName = (operation) => {
179
+ if (typeof operation === "string")
180
+ return operation;
181
+ if (isDocument(operation))
182
+ return getOperationName(operation) ?? "<unknown>";
183
+ return "<unknown>";
184
+ };
175
185
  /**
176
186
  * A wrapper for `@apollo/client/testing`, this component will assert all
177
187
  * requests have matching mocks and all defined mocks are used unless marked
@@ -218,9 +228,9 @@ export const MockedProvider = ({ mocks, stack: renderStack, children, link: pass
218
228
  const oldWarn = console.warn.bind(console.warn);
219
229
  console.warn = (message, operation, ...etc) => {
220
230
  if (typeof message !== "string" ||
221
- !message.match(/Unknown query named.*refetchQueries/))
231
+ !message.match(/Unknown query.*refetchQueries/))
222
232
  return oldWarn(message, operation, ...etc);
223
- const error = new Error(`Expected query ${operation} requested in refetchQueries options.include array to have been mocked`);
233
+ const error = new Error(`Expected query ${getBareOperationName(operation)} requested in refetchQueries options.include array to have been mocked`);
224
234
  error.stack = error.message + "\n" +
225
235
  renderStack?.split("\n").slice(1).join("\n");
226
236
  expect.getState().suppressedErrors.push(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-data-generator",
3
- "version": "0.4.0-alpha.7",
3
+ "version": "0.4.0-alpha.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/voces/graphql-data-generator.git"
package/script/jest.js CHANGED
@@ -34,6 +34,7 @@ const graphql_1 = require("graphql");
34
34
  const jest_diff_1 = require("jest-diff");
35
35
  require("@testing-library/react/dont-cleanup-after-each");
36
36
  const react_2 = require("@testing-library/react");
37
+ const utilities_1 = require("@apollo/client/utilities");
37
38
  let currentSpecResult;
38
39
  globalThis.jasmine
39
40
  ?.getEnv()
@@ -84,7 +85,7 @@ const getOperationInfo = (document) => {
84
85
  const getErrorMessage = (operation, mockLink) => {
85
86
  const operationType = getOperationType(operation);
86
87
  const key = JSON.stringify({
87
- query: (0, graphql_1.print)(operation.query),
88
+ query: (0, graphql_1.print)((0, utilities_1.addTypenameToDocument)(operation.query)),
88
89
  });
89
90
  // Bypassing private variable
90
91
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -202,6 +203,15 @@ const waitForMocks = async (mock = lastMocks.length, { timeout, offset = 0 } = {
202
203
  });
203
204
  };
204
205
  exports.waitForMocks = waitForMocks;
206
+ const isDocument = (thing) => !!thing && typeof thing === "object" && "kind" in thing &&
207
+ thing.kind === "Document";
208
+ const getBareOperationName = (operation) => {
209
+ if (typeof operation === "string")
210
+ return operation;
211
+ if (isDocument(operation))
212
+ return getOperationName(operation) ?? "<unknown>";
213
+ return "<unknown>";
214
+ };
205
215
  /**
206
216
  * A wrapper for `@apollo/client/testing`, this component will assert all
207
217
  * requests have matching mocks and all defined mocks are used unless marked
@@ -248,9 +258,9 @@ const MockedProvider = ({ mocks, stack: renderStack, children, link: passedLink,
248
258
  const oldWarn = console.warn.bind(console.warn);
249
259
  console.warn = (message, operation, ...etc) => {
250
260
  if (typeof message !== "string" ||
251
- !message.match(/Unknown query named.*refetchQueries/))
261
+ !message.match(/Unknown query.*refetchQueries/))
252
262
  return oldWarn(message, operation, ...etc);
253
- const error = new Error(`Expected query ${operation} requested in refetchQueries options.include array to have been mocked`);
263
+ const error = new Error(`Expected query ${getBareOperationName(operation)} requested in refetchQueries options.include array to have been mocked`);
254
264
  error.stack = error.message + "\n" +
255
265
  renderStack?.split("\n").slice(1).join("\n");
256
266
  expect.getState().suppressedErrors.push(error);