-
-
Notifications
You must be signed in to change notification settings - Fork 797
/
index.ts
44 lines (37 loc) · 1.11 KB
/
index.ts
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
import loader from '@graphql-tools/webpack-loader';
import { SyncTransformer, TransformOptions } from '@jest/transform';
export type GraphQLGlobalOptions = ThisParameterType<typeof loader>['query'];
declare module '@jest/types' {
namespace Config {
interface ConfigGlobals {
graphql: GraphQLGlobalOptions;
}
}
}
class GraphQLTransformer implements SyncTransformer {
process(input: string, _filePath: string, jestConfig: TransformOptions) {
const config = jestConfig.config.globals?.['graphql'] || {};
// call directly the webpack loader with a mocked context
// as the loader leverages `this.cacheable()`
return {
code: loader.call(
{
cacheable() {},
query: config,
} as any,
input,
),
};
}
}
let transformer!: GraphQLTransformer;
function defaultTransformer(): GraphQLTransformer {
return transformer || (transformer = new GraphQLTransformer());
}
export function process(...args: any[]): any {
return (defaultTransformer().process as any)(...args);
}
const transformerFactory = {
process,
};
export default transformerFactory;