forked from HoudiniGraphql/houdini
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjest.setup.js
More file actions
95 lines (80 loc) · 2.85 KB
/
jest.setup.js
File metadata and controls
95 lines (80 loc) · 2.85 KB
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
93
94
95
const recast = require('recast')
const graphql = require('graphql')
const mockFs = require('mock-fs')
const path = require('path')
const { toMatchInlineSnapshot } = require('jest-snapshot')
const fs = require('fs/promises')
const typeScriptParser = require('recast/parsers/typescript')
process.env.TEST = 'true'
expect.addSnapshotSerializer({
test: (val) => val && Object.keys(recast.types.namedTypes).includes(val.type),
serialize: (val) => {
return recast.print(val).code
},
})
expect.addSnapshotSerializer({
test: (val) => val && Object.values(graphql.Kind).includes(val.kind),
serialize: (val) => graphql.print(val),
})
expect.addSnapshotSerializer({
test: (val) =>
val &&
!Object.values(graphql.Kind).includes(val.kind) &&
!Object.keys(recast.types.namedTypes).includes(val.type),
serialize: (val) => {
return JSON.stringify(val, null, 4)
},
})
expect.extend({
async toMatchArtifactSnapshot(value, ...rest) {
// The error (and its stacktrace) must be created before any `await`
this.error = new Error()
// 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 = await fs.readFile(artifactPath, 'utf-8')
// parse the contents
const parsed = recast.parse(artifactContents, {
parser: typeScriptParser,
}).program
return toMatchInlineSnapshot.call(this, parsed, ...rest)
},
})
beforeEach(() => {
mockFs({
$houdini: {
artifacts: {},
runtime: {},
stores: {},
},
// the runtime generator copies files relative to import.meta.url. we need our tests
// to point to the same filestructure that will exist
[`build/runtime-esm`]: mockFs.load(path.resolve('build', 'runtime-esm')),
[`build/runtime-cjs`]: mockFs.load(path.resolve('build', 'runtime-cjs')),
})
})
// make sure the runtime directory is clear before each test
afterEach(mockFs.restore)
function documentName(document) {
// if there is an operation in the document
const operation = document.definitions.find(({ kind }) => graphql.Kind.OPERATION_DEFINITION)
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
)
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))
}