graphql-data-generator 0.3.0-alpha.2 → 0.3.0-alpha.4
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/init.js +0 -4
- package/esm/jest.js +38 -22
- package/package.json +1 -1
- package/script/init.js +0 -4
- package/script/jest.js +39 -22
- package/types/jest.d.ts +2 -1
package/esm/init.js
CHANGED
|
@@ -104,7 +104,6 @@ export const init = (schema, queries, mutations, subscriptions, types, inputs, s
|
|
|
104
104
|
: variables,
|
|
105
105
|
});
|
|
106
106
|
Error.captureStackTrace(mock, obj.variables);
|
|
107
|
-
mock.stack = mock.stack?.slice(6);
|
|
108
107
|
return mock;
|
|
109
108
|
},
|
|
110
109
|
},
|
|
@@ -125,7 +124,6 @@ export const init = (schema, queries, mutations, subscriptions, types, inputs, s
|
|
|
125
124
|
: data,
|
|
126
125
|
});
|
|
127
126
|
Error.captureStackTrace(mock, obj.data);
|
|
128
|
-
mock.stack = mock.stack?.slice(6);
|
|
129
127
|
return mock;
|
|
130
128
|
},
|
|
131
129
|
},
|
|
@@ -146,7 +144,6 @@ export const init = (schema, queries, mutations, subscriptions, types, inputs, s
|
|
|
146
144
|
const builder = build[operation];
|
|
147
145
|
const mock = builder(prevInput, patch);
|
|
148
146
|
Error.captureStackTrace(mock, obj[name]);
|
|
149
|
-
mock.stack = mock.stack?.slice(6);
|
|
150
147
|
return mock;
|
|
151
148
|
},
|
|
152
149
|
}])));
|
|
@@ -165,7 +162,6 @@ export const init = (schema, queries, mutations, subscriptions, types, inputs, s
|
|
|
165
162
|
});
|
|
166
163
|
mock.request.query = parsedQuery;
|
|
167
164
|
Error.captureStackTrace(mock, builder);
|
|
168
|
-
mock.stack = mock.stack?.slice(6);
|
|
169
165
|
return addOperationTransforms(name, options?.finalizeOperation
|
|
170
166
|
? options.finalizeOperation(mock)
|
|
171
167
|
: mock);
|
package/esm/jest.js
CHANGED
|
@@ -4,7 +4,7 @@ import { ApolloLink } from "@apollo/client";
|
|
|
4
4
|
import { ErrorLink } from "@apollo/client/link/error/index.js";
|
|
5
5
|
import { MockedProvider, MockLink, } from "@apollo/client/testing/index.js";
|
|
6
6
|
import { waitFor } from "@testing-library/dom";
|
|
7
|
-
import { print } from "graphql";
|
|
7
|
+
import { Kind, print } from "graphql";
|
|
8
8
|
import { diff as jestDiff } from "jest-diff";
|
|
9
9
|
let currentSpecResult;
|
|
10
10
|
jasmine.getEnv().addReporter({
|
|
@@ -12,18 +12,11 @@ jasmine.getEnv().addReporter({
|
|
|
12
12
|
});
|
|
13
13
|
const afterTest = [];
|
|
14
14
|
afterEach(async () => {
|
|
15
|
-
|
|
15
|
+
const hooks = afterTest.splice(0);
|
|
16
|
+
for (const hook of hooks)
|
|
16
17
|
await hook();
|
|
17
|
-
afterTest.splice(0);
|
|
18
18
|
});
|
|
19
|
-
const diff = (a, b) => jestDiff(a, b, {
|
|
20
|
-
omitAnnotationLines: true,
|
|
21
|
-
// aColor: green,
|
|
22
|
-
// bColor: red,
|
|
23
|
-
// changeColor: inverse,
|
|
24
|
-
// commonColor: dim,
|
|
25
|
-
// patchColor: yellow,
|
|
26
|
-
})
|
|
19
|
+
const diff = (a, b) => jestDiff(a, b, { omitAnnotationLines: true })
|
|
27
20
|
?.replace(/\w+ \{/g, "{") // Remove class names
|
|
28
21
|
.replace(/\w+ \[/g, "["); // Remove array class names
|
|
29
22
|
const getErrorMessage = (operation, mockLink) => {
|
|
@@ -38,7 +31,7 @@ const getErrorMessage = (operation, mockLink) => {
|
|
|
38
31
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
32
|
const alts = (mockLink
|
|
40
33
|
.mockedResponsesByKey[key] ?? []);
|
|
41
|
-
let errorMessage = `Expected
|
|
34
|
+
let errorMessage = `Expected ${operationType} ${operation.operationName} to have been mocked`;
|
|
42
35
|
if (alts.length || Object.keys(operation.variables).length > 0) {
|
|
43
36
|
errorMessage += ` with variables ${dntShim.Deno.inspect(operation.variables, { depth: Infinity, colors: true })}`;
|
|
44
37
|
}
|
|
@@ -61,7 +54,9 @@ const getErrorMessage = (operation, mockLink) => {
|
|
|
61
54
|
: undefined,
|
|
62
55
|
};
|
|
63
56
|
};
|
|
64
|
-
|
|
57
|
+
let _interceptRefetchWarnings = false;
|
|
58
|
+
export const interceptRefetchWarnings = (value = true) => _interceptRefetchWarnings = value;
|
|
59
|
+
export const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
65
60
|
const observableMocks = useMemo(() => {
|
|
66
61
|
const observableMocks = mocks.map((m) => ({
|
|
67
62
|
...m,
|
|
@@ -78,13 +73,14 @@ export const MockProvider = ({ mocks, stack, children }) => {
|
|
|
78
73
|
if (currentSpecResult.failedExpectations.length)
|
|
79
74
|
return;
|
|
80
75
|
if (mock.result.mock.calls.length === 0) {
|
|
81
|
-
const
|
|
76
|
+
const operation = mock.request.query.definitions.find((d) => d.kind === Kind.OPERATION_DEFINITION);
|
|
77
|
+
const err = new Error(`Expected to have used ${operation?.operation} ${operation?.name?.value}${mock.request.variables
|
|
82
78
|
? ` with variables ${dntShim.Deno.inspect(mock.request.variables, {
|
|
83
79
|
depth: Infinity,
|
|
84
80
|
colors: true,
|
|
85
81
|
})}`
|
|
86
82
|
: ""}`);
|
|
87
|
-
err.stack =
|
|
83
|
+
err.stack = mock.stack ?? renderStack ?? err.stack;
|
|
88
84
|
throw err;
|
|
89
85
|
}
|
|
90
86
|
}, { onTimeout: (e) => e });
|
|
@@ -101,12 +97,10 @@ export const MockProvider = ({ mocks, stack, children }) => {
|
|
|
101
97
|
const { message, stack: altStack } = getErrorMessage(operation, mockLink);
|
|
102
98
|
try {
|
|
103
99
|
networkError.message = message;
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
networkError.stack = finalStack;
|
|
109
|
-
fail({ name: "Error", message, stack: finalStack });
|
|
100
|
+
const stack = altStack ?? renderStack;
|
|
101
|
+
if (stack)
|
|
102
|
+
networkError.stack = stack;
|
|
103
|
+
fail({ name: "Error", message, stack: networkError.stack });
|
|
110
104
|
}
|
|
111
105
|
catch {
|
|
112
106
|
// fail both throws and marks the test as failed in jest; we only need the latter
|
|
@@ -114,6 +108,28 @@ export const MockProvider = ({ mocks, stack, children }) => {
|
|
|
114
108
|
});
|
|
115
109
|
// @ts-ignore It's fine
|
|
116
110
|
return ApolloLink.from([errorLoggingLink, mockLink]);
|
|
117
|
-
}, [observableMocks,
|
|
111
|
+
}, [observableMocks, renderStack]);
|
|
112
|
+
if (_interceptRefetchWarnings) {
|
|
113
|
+
const oldWarn = console.warn.bind(console.warn);
|
|
114
|
+
console.warn = (message, operation, ...etc) => {
|
|
115
|
+
if (message !==
|
|
116
|
+
'Unknown query named "%s" requested in refetchQueries in options.include array') {
|
|
117
|
+
return oldWarn(message, operation, ...etc);
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
fail({
|
|
121
|
+
name: "Error",
|
|
122
|
+
message: `Expected query ${operation} requested in refetchQueries options.include array to have bene mocked`,
|
|
123
|
+
stack: renderStack,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// eat
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
afterTest.push(() => {
|
|
131
|
+
console.warn = oldWarn;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
118
134
|
return React.createElement(MockedProvider, { link: link }, children);
|
|
119
135
|
};
|
package/package.json
CHANGED
package/script/init.js
CHANGED
|
@@ -107,7 +107,6 @@ const init = (schema, queries, mutations, subscriptions, types, inputs, scalars,
|
|
|
107
107
|
: variables,
|
|
108
108
|
});
|
|
109
109
|
Error.captureStackTrace(mock, obj.variables);
|
|
110
|
-
mock.stack = mock.stack?.slice(6);
|
|
111
110
|
return mock;
|
|
112
111
|
},
|
|
113
112
|
},
|
|
@@ -128,7 +127,6 @@ const init = (schema, queries, mutations, subscriptions, types, inputs, scalars,
|
|
|
128
127
|
: data,
|
|
129
128
|
});
|
|
130
129
|
Error.captureStackTrace(mock, obj.data);
|
|
131
|
-
mock.stack = mock.stack?.slice(6);
|
|
132
130
|
return mock;
|
|
133
131
|
},
|
|
134
132
|
},
|
|
@@ -149,7 +147,6 @@ const init = (schema, queries, mutations, subscriptions, types, inputs, scalars,
|
|
|
149
147
|
const builder = build[operation];
|
|
150
148
|
const mock = builder(prevInput, patch);
|
|
151
149
|
Error.captureStackTrace(mock, obj[name]);
|
|
152
|
-
mock.stack = mock.stack?.slice(6);
|
|
153
150
|
return mock;
|
|
154
151
|
},
|
|
155
152
|
}])));
|
|
@@ -168,7 +165,6 @@ const init = (schema, queries, mutations, subscriptions, types, inputs, scalars,
|
|
|
168
165
|
});
|
|
169
166
|
mock.request.query = parsedQuery;
|
|
170
167
|
Error.captureStackTrace(mock, builder);
|
|
171
|
-
mock.stack = mock.stack?.slice(6);
|
|
172
168
|
return addOperationTransforms(name, options?.finalizeOperation
|
|
173
169
|
? options.finalizeOperation(mock)
|
|
174
170
|
: mock);
|
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 = void 0;
|
|
26
|
+
exports.MockProvider = exports.interceptRefetchWarnings = 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");
|
|
@@ -38,18 +38,11 @@ jasmine.getEnv().addReporter({
|
|
|
38
38
|
});
|
|
39
39
|
const afterTest = [];
|
|
40
40
|
afterEach(async () => {
|
|
41
|
-
|
|
41
|
+
const hooks = afterTest.splice(0);
|
|
42
|
+
for (const hook of hooks)
|
|
42
43
|
await hook();
|
|
43
|
-
afterTest.splice(0);
|
|
44
44
|
});
|
|
45
|
-
const diff = (a, b) => (0, jest_diff_1.diff)(a, b, {
|
|
46
|
-
omitAnnotationLines: true,
|
|
47
|
-
// aColor: green,
|
|
48
|
-
// bColor: red,
|
|
49
|
-
// changeColor: inverse,
|
|
50
|
-
// commonColor: dim,
|
|
51
|
-
// patchColor: yellow,
|
|
52
|
-
})
|
|
45
|
+
const diff = (a, b) => (0, jest_diff_1.diff)(a, b, { omitAnnotationLines: true })
|
|
53
46
|
?.replace(/\w+ \{/g, "{") // Remove class names
|
|
54
47
|
.replace(/\w+ \[/g, "["); // Remove array class names
|
|
55
48
|
const getErrorMessage = (operation, mockLink) => {
|
|
@@ -64,7 +57,7 @@ const getErrorMessage = (operation, mockLink) => {
|
|
|
64
57
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
65
58
|
const alts = (mockLink
|
|
66
59
|
.mockedResponsesByKey[key] ?? []);
|
|
67
|
-
let errorMessage = `Expected
|
|
60
|
+
let errorMessage = `Expected ${operationType} ${operation.operationName} to have been mocked`;
|
|
68
61
|
if (alts.length || Object.keys(operation.variables).length > 0) {
|
|
69
62
|
errorMessage += ` with variables ${dntShim.Deno.inspect(operation.variables, { depth: Infinity, colors: true })}`;
|
|
70
63
|
}
|
|
@@ -87,7 +80,10 @@ const getErrorMessage = (operation, mockLink) => {
|
|
|
87
80
|
: undefined,
|
|
88
81
|
};
|
|
89
82
|
};
|
|
90
|
-
|
|
83
|
+
let _interceptRefetchWarnings = false;
|
|
84
|
+
const interceptRefetchWarnings = (value = true) => _interceptRefetchWarnings = value;
|
|
85
|
+
exports.interceptRefetchWarnings = interceptRefetchWarnings;
|
|
86
|
+
const MockProvider = ({ mocks, stack: renderStack, children }) => {
|
|
91
87
|
const observableMocks = (0, react_1.useMemo)(() => {
|
|
92
88
|
const observableMocks = mocks.map((m) => ({
|
|
93
89
|
...m,
|
|
@@ -104,13 +100,14 @@ const MockProvider = ({ mocks, stack, children }) => {
|
|
|
104
100
|
if (currentSpecResult.failedExpectations.length)
|
|
105
101
|
return;
|
|
106
102
|
if (mock.result.mock.calls.length === 0) {
|
|
107
|
-
const
|
|
103
|
+
const operation = mock.request.query.definitions.find((d) => d.kind === graphql_1.Kind.OPERATION_DEFINITION);
|
|
104
|
+
const err = new Error(`Expected to have used ${operation?.operation} ${operation?.name?.value}${mock.request.variables
|
|
108
105
|
? ` with variables ${dntShim.Deno.inspect(mock.request.variables, {
|
|
109
106
|
depth: Infinity,
|
|
110
107
|
colors: true,
|
|
111
108
|
})}`
|
|
112
109
|
: ""}`);
|
|
113
|
-
err.stack =
|
|
110
|
+
err.stack = mock.stack ?? renderStack ?? err.stack;
|
|
114
111
|
throw err;
|
|
115
112
|
}
|
|
116
113
|
}, { onTimeout: (e) => e });
|
|
@@ -127,12 +124,10 @@ const MockProvider = ({ mocks, stack, children }) => {
|
|
|
127
124
|
const { message, stack: altStack } = getErrorMessage(operation, mockLink);
|
|
128
125
|
try {
|
|
129
126
|
networkError.message = message;
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
networkError.stack = finalStack;
|
|
135
|
-
fail({ name: "Error", message, stack: finalStack });
|
|
127
|
+
const stack = altStack ?? renderStack;
|
|
128
|
+
if (stack)
|
|
129
|
+
networkError.stack = stack;
|
|
130
|
+
fail({ name: "Error", message, stack: networkError.stack });
|
|
136
131
|
}
|
|
137
132
|
catch {
|
|
138
133
|
// fail both throws and marks the test as failed in jest; we only need the latter
|
|
@@ -140,7 +135,29 @@ const MockProvider = ({ mocks, stack, children }) => {
|
|
|
140
135
|
});
|
|
141
136
|
// @ts-ignore It's fine
|
|
142
137
|
return client_1.ApolloLink.from([errorLoggingLink, mockLink]);
|
|
143
|
-
}, [observableMocks,
|
|
138
|
+
}, [observableMocks, renderStack]);
|
|
139
|
+
if (_interceptRefetchWarnings) {
|
|
140
|
+
const oldWarn = console.warn.bind(console.warn);
|
|
141
|
+
console.warn = (message, operation, ...etc) => {
|
|
142
|
+
if (message !==
|
|
143
|
+
'Unknown query named "%s" requested in refetchQueries in options.include array') {
|
|
144
|
+
return oldWarn(message, operation, ...etc);
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
fail({
|
|
148
|
+
name: "Error",
|
|
149
|
+
message: `Expected query ${operation} requested in refetchQueries options.include array to have bene mocked`,
|
|
150
|
+
stack: renderStack,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// eat
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
afterTest.push(() => {
|
|
158
|
+
console.warn = oldWarn;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
144
161
|
return react_1.default.createElement(index_js_2.MockedProvider, { link: link }, children);
|
|
145
162
|
};
|
|
146
163
|
exports.MockProvider = MockProvider;
|
package/types/jest.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { OperationMock } from "./types.js";
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const interceptRefetchWarnings: (value?: boolean) => boolean;
|
|
4
|
+
export declare const MockProvider: ({ mocks, stack: renderStack, children }: {
|
|
4
5
|
mocks: OperationMock[];
|
|
5
6
|
stack?: string;
|
|
6
7
|
children?: React.ReactNode;
|