-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
vitest.setup.ts
90 lines (75 loc) · 2.76 KB
/
vitest.setup.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
import * as graphql from 'graphql'
import path from 'path'
import * as recast from 'recast'
import typeScriptParser from 'recast/parsers/typescript'
import { beforeEach, expect } from 'vitest'
import { fs } from './packages/houdini/src/lib'
import { clearMock, testConfig } from './packages/houdini/src/test'
process.env.HOUDINI_TEST = 'true'
beforeEach(clearMock)
const config = testConfig()
// serialize artifact references
expect.addSnapshotSerializer({
test: (val) => val?.document && fs.existsSync(config.artifactPath(val.document)),
serialize(value) {
// assuming that the value we were given is a collected document, figure
// out the path holding the artifact
const artifactPath = path.join('$houdini/artifacts', documentName(value.document) + '.js')
const artifactContents = fs.readFileSync(artifactPath)
expect(artifactContents).toBeTruthy()
// parse the contents
const parsed = recast.parse(artifactContents!, {
parser: typeScriptParser,
}).program
// @ts-ignore
return recast.print(parsed).code.replaceAll(config.projectRoot, 'PROJECT_ROOT')
},
})
// serialize javascript ASTs
expect.addSnapshotSerializer({
test: (val) => val && Object.keys(recast.types.namedTypes).includes(val.type),
serialize: (val) => {
return recast.print(val).code.replaceAll(config.projectRoot, 'PROJECT_ROOT')
},
})
// serialize
expect.addSnapshotSerializer({
test: (val) => val && Object.values(graphql.Kind).includes(val.kind),
serialize: (val) => graphql.print(val),
})
expect.addSnapshotSerializer({
test: (val) =>
val &&
typeof val !== 'string' &&
!Object.values(graphql.Kind).includes(val.kind) &&
!Object.keys(recast.types.namedTypes).includes(val.type) &&
!val.document,
serialize: (val) => {
return JSON.stringify(val, null, 4)
},
})
function documentName(document: graphql.DocumentNode) {
// if there is an operation in the document
const operation = document.definitions.find(
({ kind }) => graphql.Kind.OPERATION_DEFINITION
) as graphql.OperationDefinitionNode | null
if (operation) {
// if the operation does not have a name
if (!operation.name) {
// we can't give them a file
throw new Error('encountered operation with no name: ' + graphql.print(document))
}
// use the operation name for the artifact
return operation.name.value
}
// look for a fragment definition
const fragmentDefinitions = document.definitions.filter(
({ kind }) => kind === graphql.Kind.FRAGMENT_DEFINITION
) as graphql.FragmentDefinitionNode[]
if (fragmentDefinitions.length) {
// join all of the fragment definitions into one
return fragmentDefinitions.map((fragment) => fragment.name).join('_')
}
// we don't know how to generate a name for this document
throw new Error('Could not generate artifact name for document: ' + graphql.print(document))
}