graphql-data-generator 0.3.0-alpha.4 → 0.3.0-alpha.5
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 +38 -12
- package/package.json +11 -1
- package/script/jest.js +41 -15
- package/types/jest.d.ts +7 -1
- package/types/types.d.ts +1 -0
package/esm/jest.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as dntShim from "./_dnt.shims.js";
|
|
2
2
|
import React, { useMemo } from "react";
|
|
3
|
-
import { ApolloLink } from "@apollo/client";
|
|
4
|
-
import { ErrorLink } from "@apollo/client/link/error
|
|
5
|
-
import { MockedProvider, MockLink, } from "@apollo/client/testing
|
|
3
|
+
import { ApolloLink, useApolloClient, } from "@apollo/client";
|
|
4
|
+
import { ErrorLink } from "@apollo/client/link/error";
|
|
5
|
+
import { MockedProvider, MockLink, } from "@apollo/client/testing";
|
|
6
6
|
import { waitFor } from "@testing-library/dom";
|
|
7
7
|
import { Kind, print } from "graphql";
|
|
8
8
|
import { diff as jestDiff } from "jest-diff";
|
|
@@ -54,15 +54,38 @@ const getErrorMessage = (operation, mockLink) => {
|
|
|
54
54
|
: undefined,
|
|
55
55
|
};
|
|
56
56
|
};
|
|
57
|
-
let
|
|
58
|
-
|
|
57
|
+
let _failRefetchWarnings = false;
|
|
58
|
+
/**
|
|
59
|
+
* In older versions of `@apollo/client`, refetches with missing mocks trigger
|
|
60
|
+
* warnings instead of being treated as standard missing mocks.
|
|
61
|
+
* This utility converts those warnings into failures and ensures watch queries
|
|
62
|
+
* are installed for queries with `watch: true`.
|
|
63
|
+
*/
|
|
64
|
+
export const failRefetchWarnings = (value = true) => _failRefetchWarnings = value;
|
|
65
|
+
const AutoWatch = ({ mocks }) => {
|
|
66
|
+
const client = useApolloClient();
|
|
67
|
+
for (const mock of mocks)
|
|
68
|
+
if (mock.watch)
|
|
69
|
+
client.watchQuery(mock.request);
|
|
70
|
+
return null;
|
|
71
|
+
};
|
|
59
72
|
export const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
60
73
|
const observableMocks = useMemo(() => {
|
|
61
|
-
const observableMocks = mocks.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
const observableMocks = mocks.flatMap((m) => [
|
|
75
|
+
{
|
|
76
|
+
...m,
|
|
77
|
+
stack: m.stack,
|
|
78
|
+
result: Object.assign(jest.fn(() => m.result), m.result),
|
|
79
|
+
},
|
|
80
|
+
...(m.watch
|
|
81
|
+
? [{
|
|
82
|
+
...m,
|
|
83
|
+
stack: m.stack,
|
|
84
|
+
result: Object.assign(jest.fn(() => m.result), m.result),
|
|
85
|
+
watch: false,
|
|
86
|
+
}]
|
|
87
|
+
: []),
|
|
88
|
+
]);
|
|
66
89
|
afterTest.push(async () => {
|
|
67
90
|
if (currentSpecResult.failedExpectations.length)
|
|
68
91
|
return;
|
|
@@ -109,7 +132,7 @@ export const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
|
109
132
|
// @ts-ignore It's fine
|
|
110
133
|
return ApolloLink.from([errorLoggingLink, mockLink]);
|
|
111
134
|
}, [observableMocks, renderStack]);
|
|
112
|
-
if (
|
|
135
|
+
if (_failRefetchWarnings) {
|
|
113
136
|
const oldWarn = console.warn.bind(console.warn);
|
|
114
137
|
console.warn = (message, operation, ...etc) => {
|
|
115
138
|
if (message !==
|
|
@@ -131,5 +154,8 @@ export const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
|
131
154
|
console.warn = oldWarn;
|
|
132
155
|
});
|
|
133
156
|
}
|
|
134
|
-
return React.createElement(MockedProvider, { link: link },
|
|
157
|
+
return (React.createElement(MockedProvider, { link: link },
|
|
158
|
+
React.createElement(React.Fragment, null,
|
|
159
|
+
React.createElement(AutoWatch, { mocks: observableMocks }),
|
|
160
|
+
children)));
|
|
135
161
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-data-generator",
|
|
3
|
-
"version": "0.3.0-alpha.
|
|
3
|
+
"version": "0.3.0-alpha.5",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/voces/graphql-data-generator.git"
|
|
@@ -35,6 +35,16 @@
|
|
|
35
35
|
"bin": {
|
|
36
36
|
"graphql-data-generator": "./esm/cli.js"
|
|
37
37
|
},
|
|
38
|
+
"typesVersions": {
|
|
39
|
+
"*": {
|
|
40
|
+
"script/jest": [
|
|
41
|
+
"types/jest.d.ts"
|
|
42
|
+
],
|
|
43
|
+
"script/plugin": [
|
|
44
|
+
"types/plugin.d.ts"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
},
|
|
38
48
|
"dependencies": {
|
|
39
49
|
"@graphql-codegen/visitor-plugin-common": "*",
|
|
40
50
|
"fast-glob": "*",
|
package/script/jest.js
CHANGED
|
@@ -23,12 +23,12 @@ 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.
|
|
26
|
+
exports.MockProvider = 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");
|
|
30
|
-
const
|
|
31
|
-
const
|
|
30
|
+
const error_1 = require("@apollo/client/link/error");
|
|
31
|
+
const testing_1 = require("@apollo/client/testing");
|
|
32
32
|
const dom_1 = require("@testing-library/dom");
|
|
33
33
|
const graphql_1 = require("graphql");
|
|
34
34
|
const jest_diff_1 = require("jest-diff");
|
|
@@ -80,16 +80,39 @@ const getErrorMessage = (operation, mockLink) => {
|
|
|
80
80
|
: undefined,
|
|
81
81
|
};
|
|
82
82
|
};
|
|
83
|
-
let
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
let _failRefetchWarnings = false;
|
|
84
|
+
/**
|
|
85
|
+
* In older versions of `@apollo/client`, refetches with missing mocks trigger
|
|
86
|
+
* warnings instead of being treated as standard missing mocks.
|
|
87
|
+
* This utility converts those warnings into failures and ensures watch queries
|
|
88
|
+
* are installed for queries with `watch: true`.
|
|
89
|
+
*/
|
|
90
|
+
const failRefetchWarnings = (value = true) => _failRefetchWarnings = value;
|
|
91
|
+
exports.failRefetchWarnings = failRefetchWarnings;
|
|
92
|
+
const AutoWatch = ({ mocks }) => {
|
|
93
|
+
const client = (0, client_1.useApolloClient)();
|
|
94
|
+
for (const mock of mocks)
|
|
95
|
+
if (mock.watch)
|
|
96
|
+
client.watchQuery(mock.request);
|
|
97
|
+
return null;
|
|
98
|
+
};
|
|
86
99
|
const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
87
100
|
const observableMocks = (0, react_1.useMemo)(() => {
|
|
88
|
-
const observableMocks = mocks.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
const observableMocks = mocks.flatMap((m) => [
|
|
102
|
+
{
|
|
103
|
+
...m,
|
|
104
|
+
stack: m.stack,
|
|
105
|
+
result: Object.assign(jest.fn(() => m.result), m.result),
|
|
106
|
+
},
|
|
107
|
+
...(m.watch
|
|
108
|
+
? [{
|
|
109
|
+
...m,
|
|
110
|
+
stack: m.stack,
|
|
111
|
+
result: Object.assign(jest.fn(() => m.result), m.result),
|
|
112
|
+
watch: false,
|
|
113
|
+
}]
|
|
114
|
+
: []),
|
|
115
|
+
]);
|
|
93
116
|
afterTest.push(async () => {
|
|
94
117
|
if (currentSpecResult.failedExpectations.length)
|
|
95
118
|
return;
|
|
@@ -116,9 +139,9 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
|
116
139
|
return observableMocks;
|
|
117
140
|
}, [mocks]);
|
|
118
141
|
const link = (0, react_1.useMemo)(() => {
|
|
119
|
-
const mockLink = new
|
|
142
|
+
const mockLink = new testing_1.MockLink(observableMocks);
|
|
120
143
|
mockLink.showWarnings = false;
|
|
121
|
-
const errorLoggingLink = new
|
|
144
|
+
const errorLoggingLink = new error_1.ErrorLink(({ networkError, operation }) => {
|
|
122
145
|
if (!networkError?.message.includes("No more mocked responses"))
|
|
123
146
|
return;
|
|
124
147
|
const { message, stack: altStack } = getErrorMessage(operation, mockLink);
|
|
@@ -136,7 +159,7 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
|
136
159
|
// @ts-ignore It's fine
|
|
137
160
|
return client_1.ApolloLink.from([errorLoggingLink, mockLink]);
|
|
138
161
|
}, [observableMocks, renderStack]);
|
|
139
|
-
if (
|
|
162
|
+
if (_failRefetchWarnings) {
|
|
140
163
|
const oldWarn = console.warn.bind(console.warn);
|
|
141
164
|
console.warn = (message, operation, ...etc) => {
|
|
142
165
|
if (message !==
|
|
@@ -158,6 +181,9 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
|
158
181
|
console.warn = oldWarn;
|
|
159
182
|
});
|
|
160
183
|
}
|
|
161
|
-
return react_1.default.createElement(
|
|
184
|
+
return (react_1.default.createElement(testing_1.MockedProvider, { link: link },
|
|
185
|
+
react_1.default.createElement(react_1.default.Fragment, null,
|
|
186
|
+
react_1.default.createElement(AutoWatch, { mocks: observableMocks }),
|
|
187
|
+
children)));
|
|
162
188
|
};
|
|
163
189
|
exports.MockProvider = MockProvider;
|
package/types/jest.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { OperationMock } from "./types.js";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* In older versions of `@apollo/client`, refetches with missing mocks trigger
|
|
5
|
+
* warnings instead of being treated as standard missing mocks.
|
|
6
|
+
* This utility converts those warnings into failures and ensures watch queries
|
|
7
|
+
* are installed for queries with `watch: true`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const failRefetchWarnings: (value?: boolean) => boolean;
|
|
4
10
|
export declare const MockProvider: ({ mocks, stack: renderStack, children }: {
|
|
5
11
|
mocks: OperationMock[];
|
|
6
12
|
stack?: string;
|
package/types/types.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export type OperationMock<Data extends Record<string, unknown> = Record<string,
|
|
|
28
28
|
};
|
|
29
29
|
error?: Error;
|
|
30
30
|
stack?: string;
|
|
31
|
+
watch?: boolean;
|
|
31
32
|
};
|
|
32
33
|
export type SimpleOperationMock<Data extends Record<string, unknown> = Record<string, unknown>, Variables = Record<string, unknown> | never> = {
|
|
33
34
|
data: Data;
|