Skip to content

Commit b933539

Browse files
author
dimitri
committed
feat: add failing test when import documents with comments
1 parent 647cf9f commit b933539

File tree

3 files changed

+91
-63
lines changed

3 files changed

+91
-63
lines changed
Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,82 @@
11
import '../../../testing/to-be-similar-gql-doc';
22
import { processImport, VisitedFilesMap } from '../../src';
33
import { print } from 'graphql';
4-
import { relative } from 'path'
4+
import { relative } from 'path';
55

66
const importDocuments = (documentPath: string) => print(processImport(documentPath, __dirname));
77

88
describe('import in documents', () => {
9-
it('should get documents with default imports properly', async () => {
10-
const document = importDocuments('./import-test/default/a.graphql');
9+
it('should get documents with default imports properly', () => {
10+
const document = importDocuments('./import-test/default/a.graphql');
1111

12-
expect(document).toBeSimilarGqlDoc(/* GraphQL */`
13-
query FooQuery {
14-
foo {
15-
...FooFragment
16-
}
17-
}
12+
expect(document).toBeSimilarGqlDoc(/* GraphQL */ `
13+
query FooQuery {
14+
foo {
15+
...FooFragment
16+
}
17+
}
1818
19-
fragment FooFragment on Foo {
20-
bar {
21-
...BarFragment
22-
}
23-
}
19+
fragment FooFragment on Foo {
20+
bar {
21+
...BarFragment
22+
}
23+
}
2424
25-
fragment BarFragment on Bar {
26-
baz
27-
}
28-
`);
29-
});
25+
fragment BarFragment on Bar {
26+
baz
27+
}
28+
`);
29+
});
3030

31-
it('should get documents with specific imports properly', async () => {
32-
const document = importDocuments('./import-test/specific/a.graphql');
31+
it('should get documents with default imports properly with comments', () => {
32+
const document = importDocuments('./import-test/default/with-comments.graphql');
33+
expect(document).toBeSimilarGqlDoc(
34+
/* GraphQL */ `
35+
# eslint-disable-next-line
36+
query Foo {
37+
...BarFragment
38+
}
3339
34-
expect(document).toBeSimilarGqlDoc(/* GraphQL */`
35-
query FooQuery {
36-
foo {
37-
...FooFragment
38-
}
40+
fragment BarFragment on Bar {
41+
baz
3942
}
43+
`,
44+
{ skipComments: false }
45+
);
46+
});
4047

41-
fragment FooFragment on Foo {
42-
bar {
43-
...BarFragment
44-
}
48+
it('should get documents with specific imports properly', () => {
49+
const document = importDocuments('./import-test/specific/a.graphql');
50+
51+
expect(document).toBeSimilarGqlDoc(/* GraphQL */ `
52+
query FooQuery {
53+
foo {
54+
...FooFragment
4555
}
56+
}
4657
47-
fragment BarFragment on Bar {
48-
baz
58+
fragment FooFragment on Foo {
59+
bar {
60+
...BarFragment
4961
}
50-
`);
51-
});
62+
}
63+
64+
fragment BarFragment on Bar {
65+
baz
66+
}
67+
`);
68+
});
5269

53-
it('should accept a map as fourth argument for users to get visited file paths with details', () => {
54-
const visitedFiles: VisitedFilesMap = new Map();
55-
processImport('./import-test/default/a.graphql', __dirname, undefined, visitedFiles);
56-
const relativePaths = Array.from(visitedFiles.keys())
57-
.map(absPath => relative(__dirname, absPath))
58-
.map(relPath => relPath.replace(/\\/g, '\/'))
59-
expect(relativePaths).toStrictEqual([
60-
"import-test/default/a.graphql",
61-
"import-test/default/b.graphql",
62-
"import-test/default/c.graphql"
63-
]);
64-
})
70+
it('should accept a map as fourth argument for users to get visited file paths with details', () => {
71+
const visitedFiles: VisitedFilesMap = new Map();
72+
processImport('./import-test/default/a.graphql', __dirname, undefined, visitedFiles);
73+
const relativePaths = Array.from(visitedFiles.keys())
74+
.map(absPath => relative(__dirname, absPath))
75+
.map(relPath => relPath.replace(/\\/g, '\/'));
76+
expect(relativePaths).toStrictEqual([
77+
'import-test/default/a.graphql',
78+
'import-test/default/b.graphql',
79+
'import-test/default/c.graphql',
80+
]);
81+
});
6582
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#import 'c.graphql'
2+
3+
# eslint-disable-next-line
4+
query Foo {
5+
...BarFragment
6+
}
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { ASTNode, parse, DocumentNode, DefinitionNode, print } from 'graphql';
22
import { compareNodes } from '@graphql-tools/utils';
3+
import prettier from 'prettier';
34

45
declare global {
56
namespace jest {
67
interface Matchers<R, T> {
78
/**
89
* Normalizes whitespace and performs string comparisons
910
*/
10-
toBeSimilarGqlDoc(expected: string): R;
11+
toBeSimilarGqlDoc(expected: string, { skipComments }?: { skipComments: boolean }): R;
1112
}
1213
}
1314
}
@@ -27,35 +28,39 @@ function sortRecursive(a: ASTNode) {
2728
}
2829
}
2930

30-
function normalizeDocumentString(docStr: string) {
31+
function normalizeDocumentString(docStr: string, skipComments: boolean) {
32+
if (!skipComments) {
33+
return prettier.format(docStr, { parser: 'graphql' });
34+
}
35+
3136
const doc = parse(docStr, { noLocation: true }) as DocumentNode & { definitions: DefinitionNode[] };
3237
sortRecursive(doc);
3338
return print(doc);
3439
}
3540

3641
expect.extend({
37-
toBeSimilarGqlDoc(received: string, expected: string) {
38-
const strippedReceived = normalizeDocumentString(received);
39-
const strippedExpected = normalizeDocumentString(expected);
42+
toBeSimilarGqlDoc(received: string, expected: string, { skipComments } = { skipComments: true }) {
43+
const strippedReceived = normalizeDocumentString(received, skipComments);
44+
const strippedExpected = normalizeDocumentString(expected, skipComments);
4045

4146
if (strippedReceived.trim() === strippedExpected.trim()) {
4247
return {
4348
message: () =>
4449
`expected
45-
${received}
46-
not to be a string containing (ignoring indents)
47-
${expected}`,
50+
${received}
51+
not to be a string containing (ignoring indents)
52+
${expected}`,
4853
pass: true,
4954
};
50-
} else {
51-
return {
52-
message: () =>
53-
`expected
54-
${received}
55-
to be a string containing (ignoring indents)
56-
${expected}`,
57-
pass: false,
58-
};
5955
}
56+
57+
return {
58+
message: () =>
59+
`expected
60+
${received}
61+
to be a string containing (ignoring indents)
62+
${expected}`,
63+
pass: false,
64+
};
6065
},
6166
});

0 commit comments

Comments
 (0)