Jest testing utilities for GraphQL Code Generator with custom matchers, TypeScript validation, and GraphQL server mocking
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Utilities for validating and compiling TypeScript code generated by GraphQL Codegen plugins, with comprehensive error reporting and customizable compiler options.
Validates TypeScript code by parsing and type-checking plugin output using the TypeScript compiler API. Essential for ensuring generated code is syntactically correct and type-safe.
/**
* Validates TypeScript code by parsing and type-checking plugin output
* @param pluginOutput - The plugin output content to validate (string or PluginOutput object)
* @param options - TypeScript compiler options (optional, has comprehensive defaults)
* @param isTsx - Whether to treat as TSX file (default: false)
* @param isStrict - Enable strict mode compilation (default: false)
* @param suspenseErrors - Array of error strings to suppress (default: [])
* @param compileProgram - Whether to create full TypeScript program for validation (default: false)
* @throws Error or AggregateError for validation failures
*/
function validateTs(
pluginOutput: Types.PluginOutput,
options?: CompilerOptions,
isTsx?: boolean,
isStrict?: boolean,
suspenseErrors?: string[],
compileProgram?: boolean
): void;Default Compiler Options:
noEmitOnError: true - Fail validation on compilation errorsnoImplicitAny: true - Require explicit type annotationsmoduleResolution: ModuleResolutionKind.NodeJs - Use Node.js module resolutionexperimentalDecorators: true - Enable decorator supporttarget: ScriptTarget.ES5 - Compile to ES5 for broad compatibilityjsx: JsxEmit.React - React JSX transformationallowJs: true - Allow JavaScript filesskipLibCheck: true - Skip type checking of declaration filesmodule: ModuleKind.ESNext - Use modern ES modulesUsage Examples:
import { validateTs } from "@graphql-codegen/testing";
import { Types } from '@graphql-codegen/plugin-helpers';
// Basic validation
const generatedCode = `
export interface User {
id: string;
name: string;
}
`;
validateTs(generatedCode);
// Validation with custom options
validateTs(generatedCode, {
strict: true,
target: ScriptTarget.ES2020
});
// Validate plugin output object
const pluginResult: Types.ComplexPluginOutput = {
prepend: ['import { GraphQLResolveInfo } from "graphql";'],
content: 'export type QueryResolvers = { users: Resolver<User[]>; };',
append: []
};
validateTs(pluginResult);
// TSX validation
const tsxCode = `
const Component = () => <div>Hello</div>;
`;
validateTs(tsxCode, {}, true); // isTsx = true
// Strict mode validation
validateTs(generatedCode, {}, false, true); // isStrict = trueCompiles TypeScript code and validates compilation with full program creation and emit checking. More comprehensive than validateTs for complex validation scenarios.
/**
* Compiles TypeScript code and validates compilation
* Creates a full TypeScript program and attempts compilation
* @param contents - TypeScript source code to compile
* @param options - TypeScript compiler options (optional, has defaults similar to validateTs)
* @param isTsx - Whether to treat as TSX (default: false)
* @param openPlayground - Open TypeScript playground on error for debugging (default: false)
* @throws Error for compilation failures
*/
function compileTs(
contents: string,
options?: CompilerOptions,
isTsx?: boolean,
openPlayground?: boolean
): void;Key Differences from validateTs:
Usage Examples:
import { compileTs } from "@graphql-codegen/testing";
// Basic compilation validation
const complexCode = `
import { GraphQLResolveInfo } from 'graphql';
export type QueryResolvers<ContextType = any> = {
users?: Resolver<User[], {}, ContextType>;
};
type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> =
| ResolverFn<TResult, TParent, TContext, TArgs>;
`;
compileTs(complexCode);
// With TypeScript playground debugging
try {
compileTs(problematicCode, {}, false, true);
} catch (error) {
// Playground will open if compilation fails
console.error('Compilation failed:', error.message);
}
// TSX compilation
const componentCode = `
import React from 'react';
interface Props {
users: User[];
}
export const UserList: React.FC<Props> = ({ users }) => (
<ul>
{users.map(user => <li key={user.id}>{user.name}</li>)}
</ul>
);
`;
compileTs(componentCode, {}, true); // isTsx = trueBoth validation functions provide detailed error reporting:
Error Filtering:
suspenseErrors parameterAggregateError when multiple issues existError objects with detailed TypeScript diagnostic informationError Information:
Environment Variables:
SKIP_VALIDATION: Set to any truthy value to skip validation entirely (useful for CI/debugging scenarios when you want to bypass TypeScript compilation checks)Usage with Generated Code:
// Validate resolver plugin output
const resolverOutput = await resolverPlugin(schema, [], config);
// Merge with TypeScript base types
const mergedContent = mergeOutputs([
await typescriptPlugin(schema, [], {}),
resolverOutput
]);
// Validate complete generated code
validateTs(mergedContent, {
strict: true,
noImplicitAny: true
});