-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
201 lines (188 loc) · 4.94 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { createServer } from 'node:http'
import { type AddressInfo } from 'node:net'
import { type AxiosInstance, type AxiosRequestConfig, create } from 'axios'
import { type Application } from './types.js'
/**
* An Axios instance that is bound to a test server.
*/
export interface AxiosTestInstance extends AxiosInstance {
/**
* Close the internal http server and restore the original baseURL.
*/
close: () => Promise<void>
}
interface RunningServer {
/**
* The URI to set as a base URI.
*/
uri: string
/**
* A function to close the running server.
*/
close: () => Promise<void>
}
export { Application }
/**
* Start a server for the given application.
*
* @param app The application to start a server for.
* @returns An internal server configuration.
*/
async function startServer(app: Application): Promise<RunningServer> {
if ('server' in app && 'listen' in app && 'close' in app) {
return new Promise((resolve, reject) => {
app.listen({ host: '127.0.0.1', port: 0 }, (error, uri) => {
if (error) {
reject(error)
} else {
resolve({
uri,
async close() {
await app.close()
}
})
}
})
})
}
const server = createServer(app instanceof Function ? app : app.callback())
await new Promise<void>((resolve) => {
server.listen(undefined, '127.0.0.1', resolve)
})
const { address, port } = server.address() as AddressInfo
return {
uri: `http://${address}:${port}`,
close: (): Promise<void> =>
new Promise((resolve, reject) => {
server.close((error) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
}
/**
* Patch an Axios instance so its requests are redirected to the test server.
*
* Note that this will modify the input instance.
*
* Don’t forget to close the test instance after the test!
*
* @param instance The instance to patch.
* @param app The HTTP callback function or Koa app to which requests will be redirected.
* @returns the patched instance.
* @example
* import { patchInstance, AxiosTestInstance } from 'axios-test-instance';
*
* import { instance } from './request';
* import app from './app';
*
* let testInstance: AxiosTestInstance;
*
* beforeAll(async () => {
* testInstance = await patchInstance(instance, app);
* });
*
* afterAll(async () => {
* await testInstance.close();
* });
*/
export async function patchInstance(
instance: AxiosInstance,
app: Application
): Promise<AxiosTestInstance> {
const { close, uri } = await startServer(app)
const inst = instance as AxiosTestInstance
const { baseURL } = instance.defaults
inst.defaults.baseURL = String(new URL(baseURL || '', uri))
inst.close = async (): Promise<void> => {
inst.defaults.baseURL = baseURL
await close()
inst.close = (): Promise<void> => Promise.resolve()
}
return inst
}
/**
* Create an axios instance for testing an app.
*
* Don’t forget to close the test instance after the test!
*
* @param app An http callback function or a Koa app instance.
* @param axiosConfig Configuration options to pass to the axios create call.
* @returns An axios instance that is bound to a test server.
* @example
* import { createInstance, AxiosTestInstance } from 'axios-test-instance';
*
* import app from './app';
*
* let instance: AxiosTestInstance;
*
* beforeAll(async () => {
* instance = await createInstance(app);
* });
*
* afterAll(async () => {
* await instance.close();
* });
*/
export function createInstance(
app: Application,
axiosConfig?: AxiosRequestConfig
): Promise<AxiosTestInstance> {
return patchInstance(
create({
maxRedirects: 0,
validateStatus: () => true,
...axiosConfig
}),
app
)
}
/**
* A default axios test instance.
*
* Don’t forget to close the test instance after the test!
*
* @example
* import { closeTestApp, request, setTestApp } from 'axios-test-instance';
*
* import app from './app';
*
* beforeAll(async () => {
* await setTestApp(app);
* });
*
* afterAll(closeTestApp);
*/
export const request: AxiosTestInstance = Object.assign(
create({ maxRedirects: 0, validateStatus: () => true }),
{ close: () => Promise.resolve() }
)
/**
* Close the default axios test instance.
*
* This can be passed directly to the `afterEach()` or `afterAll() function of the testing
* framework.
*
* @see request for more details
*/
export async function closeTestApp(): Promise<void> {
await request.close()
}
/**
* Set the test app for the default axios test instance.
*
* @param app An http callback function or a Koa app instance.
* @returns The default axios test instance
* @see request for more details
*/
export async function setTestApp(app: Application): Promise<AxiosTestInstance> {
await closeTestApp()
return patchInstance(request, app)
}
if (typeof afterAll !== 'undefined') {
afterAll(closeTestApp)
}