forked from mswjs/msw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.node.test.ts
85 lines (78 loc) · 1.59 KB
/
extensions.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
/**
* @jest-environment node
*/
import type { ExecutionResult } from 'graphql'
import { buildSchema, graphql as executeGraphql } from 'graphql'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import fetch from 'node-fetch'
import { gql } from '../support/graphql'
const schema = gql`
type User {
firstName: String!
}
type Query {
user: User!
}
`
const server = setupServer(
graphql.query('GetUser', async (req, res, ctx) => {
const { data, errors } = await executeGraphql({
schema: buildSchema(schema),
source: req.body.query,
rootValue: {
user: {
firstName: 'John',
},
},
})
return res(
ctx.data(data),
ctx.errors(errors),
ctx.extensions({
tracking: {
version: 1,
page: '/test',
},
}),
)
}),
)
beforeAll(() => {
server.listen()
})
afterAll(() => {
server.close()
})
test('adds extensions to the original response data', async () => {
const res = await fetch('https://api.mswjs.io', {
method: 'POST',
headers: {
accept: '*/*',
'content-type': 'application/json',
},
body: JSON.stringify({
query: gql`
query GetUser {
user {
firstName
}
}
`,
}),
})
const body: ExecutionResult = await res.json()
expect(res.status).toEqual(200)
expect(body.data).toEqual({
user: {
firstName: 'John',
},
})
expect(body.errors).toBeUndefined()
expect(body.extensions).toEqual({
tracking: {
version: 1,
page: '/test',
},
})
})