-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathintegration.spec.ts
More file actions
204 lines (174 loc) · 7.62 KB
/
integration.spec.ts
File metadata and controls
204 lines (174 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import '@graphql-codegen/testing';
import { normalize } from 'path';
import { executeCodegen } from '@graphql-codegen/cli';
const options = {
generates: {
'./tests/test-files/modules': {
schema: './tests/test-files/modules/*/types/*.graphql',
plugins: ['typescript', 'typescript-resolvers'],
preset: 'graphql-modules' as const,
presetConfig: {
baseTypesPath: 'global-types.ts',
filename: 'module-types.ts',
encapsulateModuleTypes: 'none',
},
},
},
};
describe('Integration', () => {
// In this test, we make sure executeCodegen passes on a list of Sources as an extension
// This is very important
test('should generate a base output and 4 for modules', async () => {
const { result } = await executeCodegen(options);
expect(result.length).toBe(5);
expect(normalize(result[0].filename)).toMatch(normalize(`/modules/global-types.ts`));
expect(normalize(result[1].filename)).toMatch(normalize(`/modules/blog/module-types.ts`));
expect(normalize(result[2].filename)).toMatch(normalize(`/modules/common/module-types.ts`));
expect(normalize(result[3].filename)).toMatch(normalize(`/modules/dotanions/module-types.ts`));
expect(normalize(result[4].filename)).toMatch(normalize(`/modules/users/module-types.ts`));
});
test('should not duplicate type even if type and extend type are in the same module', async () => {
const { result } = await executeCodegen(options);
const userResolversStr = `export type UserResolvers = Pick<Types.UserResolvers, DefinedFields['User']>;`;
const nbOfTimeUserResolverFound = result[4].content.split(userResolversStr).length - 1;
expect(nbOfTimeUserResolverFound).toBe(1);
});
test('should allow to override importBaseTypesFrom correctly', async () => {
const { result } = await executeCodegen({
generates: {
'./tests/test-files/modules': {
schema: './tests/test-files/modules/*/types/*.graphql',
plugins: ['typescript', 'typescript-resolvers'],
preset: 'graphql-modules',
presetConfig: {
importBaseTypesFrom: '@types',
baseTypesPath: 'global-types.ts',
filename: 'module-types.ts',
encapsulateModuleTypes: 'none',
},
},
},
});
const importStatement = `import * as Types from "@types";`;
expect(result.length).toBe(5);
expect(result[1].content).toMatch(importStatement);
expect(result[2].content).toMatch(importStatement);
expect(result[3].content).toMatch(importStatement);
expect(result[4].content).toMatch(importStatement);
});
test('should import with respect of useTypeImports config correctly', async () => {
const { result } = await executeCodegen({
generates: {
'./tests/test-files/modules': {
schema: './tests/test-files/modules/*/types/*.graphql',
plugins: ['typescript', 'typescript-resolvers'],
preset: 'graphql-modules',
presetConfig: {
importBaseTypesFrom: '@types',
baseTypesPath: 'global-types.ts',
filename: 'module-types.ts',
encapsulateModuleTypes: 'none',
},
},
},
config: {
useTypeImports: true,
},
});
const importStatement = `import type * as Types from "@types";`;
expect(result.length).toBe(5);
expect(result[1].content).toMatch(importStatement);
expect(result[2].content).toMatch(importStatement);
expect(result[3].content).toMatch(importStatement);
expect(result[4].content).toMatch(importStatement);
});
test('should allow to disable graphql-modules', async () => {
const { result } = await executeCodegen({
generates: {
'./tests/test-files/modules': {
schema: './tests/test-files/modules/*/types/*.graphql',
plugins: ['typescript', 'typescript-resolvers'],
preset: 'graphql-modules',
presetConfig: {
importBaseTypesFrom: '@types',
baseTypesPath: 'global-types.ts',
filename: 'module-types.ts',
encapsulateModuleTypes: 'none',
useGraphQLModules: false,
},
},
},
});
for (const record of result) {
expect(record).not.toContain(`graphql-modules`);
expect(record).not.toContain(`gm.`);
}
});
test('each module-types should include a relative import to glob-types module', async () => {
const { result } = await executeCodegen(options);
const importStatement = `import * as Types from "../global-types";`;
expect(result.length).toBe(5);
expect(result[1].content).toMatch(importStatement);
expect(result[2].content).toMatch(importStatement);
expect(result[3].content).toMatch(importStatement);
expect(result[4].content).toMatch(importStatement);
});
test('each module-types should export Resolvers', async () => {
const { result } = await executeCodegen(options);
const exportStatemment = `export interface Resolvers `;
expect(result.length).toBe(5);
expect(result[1].content).toMatch(exportStatemment);
expect(result[2].content).toMatch(exportStatemment);
expect(result[3].content).toMatch(exportStatemment);
expect(result[4].content).toMatch(exportStatemment);
});
test('dotanions module should export DefinedFields, Schema Types with Picks and resolvers', async () => {
const { result } = await executeCodegen(options);
expect(result.length).toBe(5);
expect(result[3].content).toMatchSnapshot();
});
test('should NOT produce required root-level resolvers in Resolvers interface by default', async () => {
const { result } = await executeCodegen(options);
const usersModuleOutput = result.find(o => o.filename.includes('users'))!;
expect(usersModuleOutput).toBeDefined();
expect(usersModuleOutput.content).toContain(
`export type QueryResolvers = Pick<Types.QueryResolvers, DefinedFields['Query']>;`
);
expect(usersModuleOutput.content).toContain('Query?: QueryResolvers;');
});
test('should produce required root-level resolvers in Resolvers interface when requireRootResolvers flag is enabled', async () => {
const optionsCopy = Object.assign({} as any, options);
optionsCopy.generates['./tests/test-files/modules'].presetConfig = {
...optionsCopy.generates['./tests/test-files/modules'].presetConfig,
requireRootResolvers: true,
useGraphQLModules: false,
};
const { result } = await executeCodegen(optionsCopy);
const usersModuleOutput = result.find(o => o.filename.includes('users'))!;
expect(usersModuleOutput).toBeDefined();
// Only Query related properties should be required
expect(usersModuleOutput.content).toBeSimilarStringTo(`
export type UserResolvers = Pick<Types.UserResolvers, DefinedFields['User']>;
export type QueryResolvers = Required<Pick<Types.QueryResolvers, DefinedFields['Query']>>;
`);
expect(usersModuleOutput.content).toBeSimilarStringTo(`
export interface Resolvers {
User?: UserResolvers;
Query: QueryResolvers;
};
`);
});
test('import paths for ESM should have correct extension', async () => {
const emitLegacyCommonJSImports = {
...options,
emitLegacyCommonJSImports: false,
};
const { result } = await executeCodegen(emitLegacyCommonJSImports);
const esmImportStatement = `import * as Types from "../global-types.js";`;
expect(result.length).toBe(5);
expect(result[1].content).toMatch(esmImportStatement);
expect(result[2].content).toMatch(esmImportStatement);
expect(result[3].content).toMatch(esmImportStatement);
expect(result[4].content).toMatch(esmImportStatement);
});
});