UNPKG

@graphql-codegen/testing

Version:
54 lines (53 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const vitest_1 = require("vitest"); const common_tags_1 = require("common-tags"); const jest_diff_1 = require("jest-diff"); function compareStrings(a, b) { return a.includes(b); } /** Ignore whitespace, trailing commas, and leading pipes */ function similarize(str) { return ((0, common_tags_1.oneLine) `${str}` // Trim trailing commas .replace(/\s*,(\s*[)}])/g, '$1') // Remove leading pipes .replace(/([<:,=(])\s*(?:\|\s*)?/g, '$1') // Remove spaces around brackets and semicolons .replace(/\s*([[\](){}<>;])\s*/g, '$1') // Replace multiple spaces with a single space .replace(/\s\s+/g, ' ')); } vitest_1.expect.extend({ toBeSimilarStringTo(received, expected) { const strippedReceived = similarize(received); const strippedExpected = similarize(expected); if (compareStrings(strippedReceived, strippedExpected)) { return { message: () => `expected ${received} not to be a string containing (ignoring indents) ${expected}`, pass: true, }; } const diffString = (0, jest_diff_1.diff)((0, common_tags_1.stripIndent) `${expected}`, (0, common_tags_1.stripIndent) `${received}`, { expand: this.expand, }); const hasExpect = diffString?.includes('- Expect'); const message = hasExpect ? `Difference:\n\n${diffString}` : `expected ${received} to be a string containing (ignoring indents) ${expected}`; return { message: () => message, pass: false, }; }, }); tslib_1.__exportStar(require("./mock-graphql-server.js"), exports); tslib_1.__exportStar(require("./resolvers-common.js"), exports); tslib_1.__exportStar(require("./typescript.js"), exports);