Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test when import documents with comments #3113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 63 additions & 46 deletions packages/import/tests/documents/import-documents.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import 'c.graphql'

# eslint-disable-next-line
query Foo {
...BarFragment
}
39 changes: 22 additions & 17 deletions packages/testing/to-be-similar-gql-doc.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ASTNode, parse, DocumentNode, DefinitionNode, print } from 'graphql';
import { compareNodes } from '@graphql-tools/utils';
import prettier from 'prettier';

declare global {
namespace jest {
interface Matchers<R, T> {
/**
* Normalizes whitespace and performs string comparisons
*/
toBeSimilarGqlDoc(expected: string): R;
toBeSimilarGqlDoc(expected: string, { skipComments }?: { skipComments: boolean }): R;
}
}
}
Expand All @@ -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,
};
},
});