-
-
Notifications
You must be signed in to change notification settings - Fork 542
/
Copy pathcompatibility.node.test.ts
88 lines (77 loc) · 1.71 KB
/
compatibility.node.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
import fetch from 'cross-fetch'
import { graphql as executeGraphql, buildSchema } from 'graphql'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { createGraphQLClient, gql } from '../../support/graphql'
const schema = gql`
type User {
firstName: String!
}
type Query {
user: User!
}
`
const server = setupServer(
graphql.query('GetUser', async ({ query }) => {
const executionResult = await executeGraphql({
schema: buildSchema(schema),
source: query,
rootValue: {
user: {
firstName: 'John',
},
},
})
return HttpResponse.json({
data: executionResult.data,
errors: executionResult.errors,
})
}),
)
const client = createGraphQLClient({
uri: 'https://api.mswjs.io',
fetch,
})
beforeAll(() => {
server.listen()
})
afterAll(() => {
server.close()
})
test('fetches the data from a GraphQL schema', async () => {
const res = await client({
query: gql`
query GetUser {
user {
firstName
}
}
`,
})
expect(res.data).toEqual({
user: {
firstName: 'John',
},
})
expect(res.errors).toBeUndefined()
})
test('propagates the GraphQL execution errors', async () => {
const res = await client({
query: gql`
query GetUser {
user {
firstName
# Intentionally querying a non-existing field
# to cause a GraphQL error upon execution.
lastName
}
}
`,
})
expect(res.data).toBeUndefined()
expect(res.errors).toHaveLength(1)
expect(res.errors[0]).toHaveProperty(
'message',
'Cannot query field "lastName" on type "User". Did you mean "firstName"?',
)
})