From 5374e2e654649fa531667194ab7975306b7e7845 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 1 Jul 2021 12:56:08 +0300 Subject: [PATCH] Reproduction of printWithComments issue --- .../documents/documents-from-glob.spec.ts | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/load/tests/loaders/documents/documents-from-glob.spec.ts b/packages/load/tests/loaders/documents/documents-from-glob.spec.ts index f7fe2ce3532..ef6f5e6bfb1 100644 --- a/packages/load/tests/loaders/documents/documents-from-glob.spec.ts +++ b/packages/load/tests/loaders/documents/documents-from-glob.spec.ts @@ -1,9 +1,13 @@ import { loadDocuments, loadDocumentsSync } from '@graphql-tools/load'; import { join } from 'path'; -import { separateOperations } from 'graphql'; +import { parse, separateOperations } from 'graphql'; import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'; import { CodeFileLoader } from '@graphql-tools/code-file-loader'; import { runTests } from '../../../../testing/utils'; +import '../../../../testing/to-be-similar-string' +import globby from 'globby'; +import { readFileSync } from 'fs'; +import { removeLoc } from '@graphql-tools/optimize'; describe('documentsFromGlob', () => { runTests({ @@ -15,8 +19,15 @@ describe('documentsFromGlob', () => { const result = await load(glob, { loaders: [new GraphQLFileLoader()] }); - expect(result.length).toBe(1); - expect(result[0].document).toBeDefined(); + const expectedFiles = globby.sync(glob); + for (const expectedFileName of expectedFiles) { + const fileNameResult = result?.find(({ location }) => location === expectedFileName); + expect(fileNameResult).toBeDefined(); + const fileContent = readFileSync(expectedFileName, 'utf-8'); + const expectedDocument = parse(fileContent); + expect(removeLoc(fileNameResult!.document!)).toStrictEqual(removeLoc(expectedDocument)); + expect(fileNameResult!.rawSDL!).toBeSimilarString(fileContent); + } }); test(`Should load multiple GraphQL document from glob expression`, async () => { @@ -24,23 +35,27 @@ describe('documentsFromGlob', () => { const result = await load(glob, { loaders: [new GraphQLFileLoader()] }); - expect(result.length).toBe(2); - expect(result[0].document).toBeDefined(); - expect(result[1].document).toBeDefined(); + const expectedFiles = globby.sync(glob); + for (const expectedFileName of expectedFiles) { + const fileNameResult = result?.find(({ location }) => location === expectedFileName); + expect(fileNameResult).toBeDefined(); + const fileContent = readFileSync(expectedFileName, 'utf-8'); + const expectedDocument: any = parse(fileContent); + expect(removeLoc(fileNameResult!.document!)).toStrictEqual(removeLoc(expectedDocument)); + expect(fileNameResult!.rawSDL!).toBeSimilarString(fileContent); + } }); - test(`Should load two GraphQL documents both for gatsby and graphql-tag by default`, async () => { + test(`Should load two GraphQL operations both for gatsby and graphql-tag by default`, async () => { const glob = join(__dirname, './test-files/', 'tags.js'); const result = await load(glob, { loaders: [new CodeFileLoader()] }); - const { document } = result[0]; - const operations = document && separateOperations(document); - - expect(operations && Object.keys(operations)).toHaveLength(2); + expect(result[0]?.document).toBeDefined(); + expect(Object.keys(separateOperations(result[0].document!))).toHaveLength(2); }); - test(`Should load GraphQL documents that match custom settings`, async () => { + test(`Should load GraphQL operations that match custom settings`, async () => { const glob = join(__dirname, './test-files/', 'tags.js'); const result = await load(glob, { @@ -58,10 +73,8 @@ describe('documentsFromGlob', () => { ] }); - const { document } = result[0]; - const operations = document && separateOperations(document); - - expect(operations && Object.keys(operations)).toHaveLength(1); + expect(result[0]?.document).toBeDefined(); + expect(Object.keys(separateOperations(result[0].document!))).toHaveLength(1); }); test(`Should throw on syntax errors`, async () => {