-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
fallback-mode.test.ts
136 lines (115 loc) · 3.92 KB
/
fallback-mode.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as path from 'path'
import { SetupWorkerApi } from 'msw'
import { createTeardown } from 'fs-teardown'
import { Page, pageWith, Response, ScenarioApi } from 'page-with'
import { fromTemp } from '../../../support/utils'
import { waitFor } from '../../../support/waitFor'
let runtime: ScenarioApi
declare namespace window {
export const worker: SetupWorkerApi
}
// Create a temporary directory on the disk
// that would server as the public directory.
const fsMock = createTeardown({
rootDir: fromTemp('fallback-mode'),
paths: {
'index.html': '<script src="index.js"></script>',
},
})
beforeAll(async () => {
await fsMock.prepare()
runtime = await pageWith({
title: 'Fallback mode',
example: path.resolve(__dirname, '../../../rest-api/basic.mocks.ts'),
serverOptions: {
// Force this particular server instance to compile
// assets to the file-system so the test could open them.
compileInMemory: false,
webpackConfig: {
output: {
filename: 'index.js',
path: fsMock.resolve(),
},
},
},
})
await runtime.page.goto(`file://${fsMock.resolve('index.html')}`, {
waitUntil: 'networkidle',
})
})
afterAll(async () => {
await fsMock.cleanup()
})
function createRequestHelper(page: Page) {
return (input: RequestInfo, init?: RequestInit): Promise<Response> => {
return page.evaluate(
([input, init]: [RequestInfo, RequestInit]) => {
return fetch(input, init)
.then((res) => {
const headers = {}
res.headers.forEach((value, key) => {
headers[key] = value
})
return res.json().then((body) => ({
status: res.status,
statusText: res.statusText,
headers,
body,
}))
})
.catch(() => null)
},
[input, init],
)
}
}
test('prints a fallback start message in the console', async () => {
const consoleGroups = runtime.consoleSpy.get('startGroupCollapsed')
expect(consoleGroups).toContain('[MSW] Mocking enabled (fallback mode).')
})
test('responds with a mocked response to a handled request', async () => {
const request = createRequestHelper(runtime.page)
const response = await request('https://api.github.com/users/octocat')
// Prints the request message group in the console.
await waitFor(() => {
expect(runtime.consoleSpy.get('startGroupCollapsed')).toEqual(
expect.arrayContaining([
expect.stringMatching(
/\[MSW\] \d{2}:\d{2}:\d{2} GET https:\/\/api\.github\.com\/users\/octocat 200 OK/,
),
]),
)
})
// Responds with a mocked response.
expect(response.status).toEqual(200)
expect(response.statusText).toEqual('OK')
expect(response.headers['x-powered-by']).toEqual('msw')
expect(response.body).toEqual({
name: 'John Maverick',
originalUsername: 'octocat',
})
})
test('warns on the unhandled request by default', async () => {
const request = createRequestHelper(runtime.page)
await request('https://example.com')
expect(runtime.consoleSpy.get('warning')).toEqual(
expect.arrayContaining([
expect.stringContaining(`\
[MSW] Warning: captured a request without a matching request handler:
• GET https://example.com/
If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks`),
]),
)
})
test('stops the fallback interceptor when called "worker.stop()"', async () => {
const request = createRequestHelper(runtime.page)
await runtime.page.evaluate(() => {
window.worker.stop()
})
// The stop message must be printed to the console.
expect(runtime.consoleSpy.get('log')).toContain('[MSW] Mocking disabled.')
// No requests should be intercepted.
const response = await request('https://api.github.com/users/octocat')
expect(response.headers).toHaveProperty('x-github-media-type')
})