This repository was archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathformatGeneratedModule-test.ts
92 lines (85 loc) · 3.27 KB
/
formatGeneratedModule-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { readFileSync } from 'fs'
import { join } from 'path'
import * as diff from 'jest-diff'
import { formatterFactory } from '../src/formatGeneratedModule'
expect.extend({
toMatchFile(received, fixturePath) {
const actual = readFileSync(fixturePath, 'utf8')
if (received.trim() === actual.trim()) {
return {
message: () => `expected ${fixturePath} not to match:\n\n${received}`,
pass: true,
}
} else {
return {
message: () => {
const diffString = diff(actual, received, {
expand: (this as any).expand,
});
return (
this.utils.matcherHint('.toBe') +
'\n\n' +
`Expected value to be (using ===):\n` +
` ${this.utils.printExpected(actual)}\n` +
`Received:\n` +
` ${this.utils.printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
);
},
pass: false,
}
}
}
})
describe('formatGeneratedModule', () => {
it('works', () => {
const formatGeneratedModule = formatterFactory({noImplicitAny: true});
expect(formatGeneratedModule({
moduleName: 'complete-example',
documentType: 'ConcreteFragment',
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } }}),
typeText: 'export type CompleteExample = { readonly id: string }',
hash: 'abcde',
relayRuntimeModule: 'relay-runtime',
sourceHash: 'edcba',
})).toMatchFile(join(__dirname, 'fixtures/generated-module/complete-example.ts'))
})
it('works without passing relay runtime module explicitly', () => {
const formatGeneratedModule = formatterFactory({noImplicitAny: true});
expect(formatGeneratedModule({
moduleName: 'complete-example',
documentType: 'ConcreteFragment',
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } }}),
typeText: 'export type CompleteExample = { readonly id: string }',
hash: 'abcde',
sourceHash: 'edcba',
})).toMatchFile(join(__dirname, 'fixtures/generated-module/complete-example.ts'))
})
it('doesn\'t add a typecast if noImplicitAny is not set', () => {
const formatGeneratedModule = formatterFactory();
expect(formatGeneratedModule({
moduleName: 'complete-example',
documentType: 'ConcreteFragment',
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } }}),
typeText: 'export type CompleteExample = { readonly id: string }',
hash: 'abcde',
sourceHash: 'edcba',
})).toMatchFile(join(__dirname, 'fixtures/generated-module/complete-example-no-cast.ts'))
})
it('works with devOnlyAssignments', () => {
const formatGeneratedModule = formatterFactory();
expect(formatGeneratedModule({
moduleName: 'complete-example',
documentType: 'ConcreteFragment',
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } }}),
typeText: 'export type CompleteExample = { readonly id: string }',
hash: 'abcde',
sourceHash: 'edcba',
devOnlyAssignments: '(node/*: any*/).params.text = "query CompleteExampleQuery { id }";',
})).toMatchFile(join(__dirname, 'fixtures/generated-module/dev-only-assignments.ts'))
})
})