-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.ts
259 lines (223 loc) · 7.52 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import * as Sentry from '@sentry/node';
import { EnvelopeItemType } from '@sentry/types';
import { logger, parseSemver } from '@sentry/utils';
import axios, { AxiosRequestConfig } from 'axios';
import { Express } from 'express';
import * as http from 'http';
import nock from 'nock';
import * as path from 'path';
import { getPortPromise } from 'portfinder';
export type TestServerConfig = {
url: string;
server: http.Server;
};
export type DataCollectorOptions = {
// Optional custom URL
url?: string;
// The expected amount of requests to the envelope endpoint.
// If the amount of sent requests is lower than `count`, this function will not resolve.
count?: number;
// The method of the request.
method?: 'get' | 'post';
// Whether to stop the server after the requests have been intercepted
endServer?: boolean;
// Type(s) of the envelopes to capture
envelopeType?: EnvelopeItemType | EnvelopeItemType[];
};
/**
* Returns`describe` or `describe.skip` depending on allowed major versions of Node.
*
* @param {{ min?: number; max?: number }} allowedVersion
* @return {*} {jest.Describe}
*/
export const conditionalTest = (allowedVersion: { min?: number; max?: number }): jest.Describe => {
const NODE_VERSION = parseSemver(process.versions.node).major;
if (!NODE_VERSION) {
return describe.skip;
}
return NODE_VERSION < (allowedVersion.min || -Infinity) || NODE_VERSION > (allowedVersion.max || Infinity)
? describe.skip
: describe;
};
/**
* Asserts against a Sentry Event ignoring non-deterministic properties
*
* @param {Record<string, unknown>} actual
* @param {Record<string, unknown>} expected
*/
export const assertSentryEvent = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
expect(actual).toMatchObject({
event_id: expect.any(String),
timestamp: expect.anything(),
...expected,
});
};
/**
* Asserts against a Sentry Transaction ignoring non-deterministic properties
*
* @param {Record<string, unknown>} actual
* @param {Record<string, unknown>} expected
*/
export const assertSentryTransaction = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
expect(actual).toMatchObject({
event_id: expect.any(String),
timestamp: expect.anything(),
start_timestamp: expect.anything(),
spans: expect.any(Array),
type: 'transaction',
...expected,
});
};
/**
* Parses response body containing an Envelope
*
* @param {string} body
* @return {*} {Array<Record<string, unknown>>}
*/
export const parseEnvelope = (body: string): Array<Record<string, unknown>> => {
return body.split('\n').map(e => JSON.parse(e));
};
/**
* Sends a get request to given URL.
* Flushes the Sentry event queue.
*
* @param {string} url
* @return {*} {Promise<void>}
*/
export async function runScenario(url: string): Promise<void> {
await axios.get(url);
await Sentry.flush();
}
async function makeRequest(
method: 'get' | 'post' = 'get',
url: string,
axiosConfig?: AxiosRequestConfig,
): Promise<void> {
try {
if (method === 'get') {
await axios.get(url, axiosConfig);
} else {
await axios.post(url, axiosConfig);
}
} catch (e) {
// We sometimes expect the request to fail, but not the test.
// So, we do nothing.
logger.warn(e);
}
}
export class TestEnv {
private _axiosConfig: AxiosRequestConfig | undefined = undefined;
public constructor(public readonly server: http.Server, public readonly url: string) {
this.server = server;
this.url = url;
}
/**
* Starts a test server and returns the TestEnv instance
*
* @param {string} testDir
* @param {string} [serverPath]
* @param {string} [scenarioPath]
* @return {*} {Promise<string>}
*/
public static async init(testDir: string, serverPath?: string, scenarioPath?: string): Promise<TestEnv> {
const port = await getPortPromise();
const url = `http://localhost:${port}/test`;
const defaultServerPath = path.resolve(process.cwd(), 'utils', 'defaults', 'server');
const server = await new Promise<http.Server>(resolve => {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
const app = require(serverPath || defaultServerPath).default as Express;
app.get('/test', (_req, res) => {
try {
require(scenarioPath || `${testDir}/scenario`);
} finally {
res.status(200).end();
}
});
const server = app.listen(port, () => {
resolve(server);
});
});
return new TestEnv(server, url);
}
/**
* Intercepts and extracts up to a number of requests containing Sentry envelopes.
*
* @param {DataCollectorOptions} options
* @returns The intercepted envelopes.
*/
public async getMultipleEnvelopeRequest(options: DataCollectorOptions): Promise<Record<string, unknown>[][]> {
const envelopeTypeArray =
typeof options.envelopeType === 'string'
? [options.envelopeType]
: options.envelopeType || (['event'] as EnvelopeItemType[]);
const resProm = this.setupNock(
options.count || 1,
typeof options.endServer === 'undefined' ? true : options.endServer,
envelopeTypeArray,
);
void makeRequest(options.method, options.url || this.url, this._axiosConfig);
return resProm;
}
/**
* Intercepts and extracts a single request containing a Sentry envelope
*
* @param {DataCollectorOptions} options
* @returns The extracted envelope.
*/
public async getEnvelopeRequest(options?: DataCollectorOptions): Promise<Array<Record<string, unknown>>> {
return (await this.getMultipleEnvelopeRequest({ ...options, count: 1 }))[0];
}
/**
* Sends a get request to given URL, with optional headers. Returns the response.
* Ends the server instance and flushes the Sentry event queue.
*
* @param {Record<string, string>} [headers]
* @return {*} {Promise<any>}
*/
public async getAPIResponse(url?: string, headers?: Record<string, string>): Promise<unknown> {
const { data } = await axios.get(url || this.url, { headers: headers || {} });
await Sentry.flush();
this.server.close();
return data;
}
public async setupNock(
count: number,
endServer: boolean,
envelopeType: EnvelopeItemType[],
): Promise<Record<string, unknown>[][]> {
return new Promise(resolve => {
const envelopes: Record<string, unknown>[][] = [];
const mock = nock('https://dsn.ingest.sentry.io')
.persist()
.post('/api/1337/envelope/', body => {
const envelope = parseEnvelope(body);
if (envelopeType.includes(envelope[1].type as EnvelopeItemType)) {
envelopes.push(envelope);
} else {
return false;
}
if (count === envelopes.length) {
nock.removeInterceptor(mock);
if (endServer) {
// Cleaning nock only before the server is closed,
// not to break tests that use simultaneous requests to the server.
// Ex: Remix scope bleed tests.
nock.cleanAll();
this.server.close(() => {
resolve(envelopes);
});
}
resolve(envelopes);
}
return true;
});
mock
.query(true) // accept any query params - used for sentry_key param
.reply(200);
});
}
public setAxiosConfig(axiosConfig: AxiosRequestConfig): void {
this._axiosConfig = axiosConfig;
}
}