var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.js var src_exports = {}; __export(src_exports, { mockGqlQuery: () => mockGqlQuery, setupJestExtensions: () => setupJestExtensions }); module.exports = __toCommonJS(src_exports); // src/core/parseQuery.js var import_graphql = require("graphql"); // src/core/getSchema.js var import_graphql_config = require("graphql-config"); var _schema = null; var retrieveSchema = async () => { const config = await (0, import_graphql_config.loadConfig)({ rootDir: process.cwd() }); return await config.getDefault().getSchema(); }; var getSchema = async () => _schema ??= await retrieveSchema(); // src/core/parseQuery.js var GRAPHQL_FIELD = "Field"; var GRAPHQL_ROOT_SOURCE_BY_OPERATION = { query: "Query", mutation: "Mutation" }; var getFieldType = (_type) => _type instanceof import_graphql.GraphQLNonNull ? _type.ofType : _type; var getFieldTypeFromPossibleList = (_type) => getFieldType(_type instanceof import_graphql.GraphQLList ? _type.ofType : _type); var parseQuery = async ({ definitions }) => { const schema = await getSchema(); const typeMap = schema.getTypeMap(); return Object.assign( {}, ...definitions.map(({ operation, selectionSet: rootSelections }) => { const checkFields = (typeName, { selections = [] } = {}, parentPath = "") => selections.reduce( (fields, { kind, name: { value: fieldName }, selectionSet }) => { if (kind !== GRAPHQL_FIELD) { return fields; } const { [fieldName]: { type: fieldType } } = typeMap[typeName].getFields(); return Object.assign( fields, { [parentPath ? `${parentPath}.${fieldName}` : fieldName]: fieldType instanceof import_graphql.GraphQLNonNull }, selectionSet ? checkFields( getFieldTypeFromPossibleList(getFieldType(fieldType)), selectionSet, parentPath ? `${parentPath}.${fieldName}` : fieldName ) : {} ); }, {} ); return checkFields( GRAPHQL_ROOT_SOURCE_BY_OPERATION[operation], rootSelections ); }) ); }; // src/core/getKeys.js var getKeys = (obj, parent = "") => !obj || typeof obj !== "object" ? [parent].filter(Boolean) : [ ...new Set( Object.entries(obj).flatMap( ([key, value]) => Array.isArray(value) ? [ parent ? `${parent}.${key}` : key, ...value.flatMap( (item) => getKeys(item, parent ? `${parent}.${key}` : key) ) ] : [ parent ? `${parent}.${key}` : key, ...getKeys(value, parent ? `${parent}.${key}` : key) ] ) ) ]; // src/core/validateMockData.js var validateMockData = (fieldDefinitions, response, ignoreUnexpected = false) => { const errors = []; const responseFields = getKeys(response); if (!ignoreUnexpected) { for (const responseField of responseFields) { if (!Object.keys(fieldDefinitions).includes(responseField)) { errors.push(`Unexpected field "${responseField}" in mock data`); } } } for (const [keyPath, isRequired] of Object.entries(fieldDefinitions)) { if (!isRequired) { continue; } const keys = keyPath.split("."); let current = response; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (current[key] === void 0 || current[key] === null) { if (!fieldDefinitions[keys.slice(0, i + 1).join(".")]) { break; } if (i === keys.length - 1) { errors.push(`Missing required field "${keyPath}" in mock data`); } break; } if (Array.isArray(current[key])) { if (i === keys.length - 1) { break; } const remainingPath = keys.slice(i + 1).join("."); current[key].forEach((item, index) => { const subErrors = validateMockData( { [remainingPath]: true }, item, true ); errors.push( ...subErrors.map( (error) => `Array item validation failed at "${keys.slice(0, i + 1).join(".")}[${index}]" -> ${error}` ) ); }); break; } current = current[key]; } } return errors; }; // src/core/toMatchGqlMock.js var toMatchGqlMock = async (received, gql) => { const fieldDefinitions = await parseQuery(gql); const errors = validateMockData(fieldDefinitions, received); return { pass: errors.length === 0, message: errors.length === 0 ? () => `Expected data to match the GraphQL query structure, but it does not.` : () => `Mock data validation failed: ${errors.join("\n")}` }; }; // src/core/mockGqlQuery.js var mockGqlQuery = () => { const internalMock = jest.fn(); internalMock.mockResolvedGqlOnce = async (query, response) => { await expect(response).toMatchGqlMock(query); internalMock.mockResolvedValueOnce(response); }; return internalMock; }; // src/index.js var setupJestExtensions = () => { if (typeof jest === "undefined") { console.warn( "Jest is not available in the global context. Skipping configuration." ); return; } expect.extend({ toMatchGqlMock }); }; setupJestExtensions(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { mockGqlQuery, setupJestExtensions });