diff --git a/packages/import/tests/documents/import-documents.spec.ts b/packages/import/tests/documents/import-documents.spec.ts index 915c67609da..2f81172f5cb 100644 --- a/packages/import/tests/documents/import-documents.spec.ts +++ b/packages/import/tests/documents/import-documents.spec.ts @@ -1,65 +1,82 @@ import '../../../testing/to-be-similar-gql-doc'; import { processImport, VisitedFilesMap } from '../../src'; import { print } from 'graphql'; -import { relative } from 'path' +import { relative } from 'path'; const importDocuments = (documentPath: string) => print(processImport(documentPath, __dirname)); describe('import in documents', () => { - it('should get documents with default imports properly', async () => { - const document = importDocuments('./import-test/default/a.graphql'); + it('should get documents with default imports properly', () => { + const document = importDocuments('./import-test/default/a.graphql'); - expect(document).toBeSimilarGqlDoc(/* GraphQL */` - query FooQuery { - foo { - ...FooFragment - } - } + expect(document).toBeSimilarGqlDoc(/* GraphQL */ ` + query FooQuery { + foo { + ...FooFragment + } + } - fragment FooFragment on Foo { - bar { - ...BarFragment - } - } + fragment FooFragment on Foo { + bar { + ...BarFragment + } + } - fragment BarFragment on Bar { - baz - } - `); - }); + fragment BarFragment on Bar { + baz + } + `); + }); - it('should get documents with specific imports properly', async () => { - const document = importDocuments('./import-test/specific/a.graphql'); + it('should get documents with default imports properly with comments', () => { + const document = importDocuments('./import-test/default/with-comments.graphql'); + expect(document).toBeSimilarGqlDoc( + /* GraphQL */ ` + # eslint-disable-next-line + query Foo { + ...BarFragment + } - expect(document).toBeSimilarGqlDoc(/* GraphQL */` - query FooQuery { - foo { - ...FooFragment - } + fragment BarFragment on Bar { + baz } + `, + { skipComments: false } + ); + }); - fragment FooFragment on Foo { - bar { - ...BarFragment - } + it('should get documents with specific imports properly', () => { + const document = importDocuments('./import-test/specific/a.graphql'); + + expect(document).toBeSimilarGqlDoc(/* GraphQL */ ` + query FooQuery { + foo { + ...FooFragment } + } - fragment BarFragment on Bar { - baz + fragment FooFragment on Foo { + bar { + ...BarFragment } - `); - }); + } + + fragment BarFragment on Bar { + baz + } + `); + }); - it('should accept a map as fourth argument for users to get visited file paths with details', () => { - const visitedFiles: VisitedFilesMap = new Map(); - processImport('./import-test/default/a.graphql', __dirname, undefined, visitedFiles); - const relativePaths = Array.from(visitedFiles.keys()) - .map(absPath => relative(__dirname, absPath)) - .map(relPath => relPath.replace(/\\/g, '\/')) - expect(relativePaths).toStrictEqual([ - "import-test/default/a.graphql", - "import-test/default/b.graphql", - "import-test/default/c.graphql" - ]); - }) + it('should accept a map as fourth argument for users to get visited file paths with details', () => { + const visitedFiles: VisitedFilesMap = new Map(); + processImport('./import-test/default/a.graphql', __dirname, undefined, visitedFiles); + const relativePaths = Array.from(visitedFiles.keys()) + .map(absPath => relative(__dirname, absPath)) + .map(relPath => relPath.replace(/\\/g, '\/')); + expect(relativePaths).toStrictEqual([ + 'import-test/default/a.graphql', + 'import-test/default/b.graphql', + 'import-test/default/c.graphql', + ]); + }); }); diff --git a/packages/import/tests/documents/import-test/default/with-comments.graphql b/packages/import/tests/documents/import-test/default/with-comments.graphql new file mode 100644 index 00000000000..ba92181c3cf --- /dev/null +++ b/packages/import/tests/documents/import-test/default/with-comments.graphql @@ -0,0 +1,6 @@ +#import 'c.graphql' + +# eslint-disable-next-line +query Foo { + ...BarFragment +} diff --git a/packages/testing/to-be-similar-gql-doc.ts b/packages/testing/to-be-similar-gql-doc.ts index f170e9196ce..34d0d8a7417 100644 --- a/packages/testing/to-be-similar-gql-doc.ts +++ b/packages/testing/to-be-similar-gql-doc.ts @@ -1,5 +1,6 @@ import { ASTNode, parse, DocumentNode, DefinitionNode, print } from 'graphql'; import { compareNodes } from '@graphql-tools/utils'; +import prettier from 'prettier'; declare global { namespace jest { @@ -7,7 +8,7 @@ declare global { /** * Normalizes whitespace and performs string comparisons */ - toBeSimilarGqlDoc(expected: string): R; + toBeSimilarGqlDoc(expected: string, { skipComments }?: { skipComments: boolean }): R; } } } @@ -27,35 +28,39 @@ function sortRecursive(a: ASTNode) { } } -function normalizeDocumentString(docStr: string) { +function normalizeDocumentString(docStr: string, skipComments: boolean) { + if (!skipComments) { + return prettier.format(docStr, { parser: 'graphql' }); + } + const doc = parse(docStr, { noLocation: true }) as DocumentNode & { definitions: DefinitionNode[] }; sortRecursive(doc); return print(doc); } expect.extend({ - toBeSimilarGqlDoc(received: string, expected: string) { - const strippedReceived = normalizeDocumentString(received); - const strippedExpected = normalizeDocumentString(expected); + toBeSimilarGqlDoc(received: string, expected: string, { skipComments } = { skipComments: true }) { + const strippedReceived = normalizeDocumentString(received, skipComments); + const strippedExpected = normalizeDocumentString(expected, skipComments); if (strippedReceived.trim() === strippedExpected.trim()) { return { message: () => `expected - ${received} - not to be a string containing (ignoring indents) - ${expected}`, +${received} +not to be a string containing (ignoring indents) +${expected}`, pass: true, }; - } else { - return { - message: () => - `expected - ${received} - to be a string containing (ignoring indents) - ${expected}`, - pass: false, - }; } + + return { + message: () => + `expected +${received} +to be a string containing (ignoring indents) +${expected}`, + pass: false, + }; }, });