forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutils.ts
68 lines (58 loc) · 2.44 KB
/
testutils.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
import { createTransport } from '@sentry/browser';
import { ClientOptions } from '@sentry/types';
import { GLOBAL_OBJ, resolvedSyncPromise } from '@sentry/utils';
import { JSDOM } from 'jsdom';
/**
* Injects DOM properties into node global object.
*
* Useful for running tests where some of the tested code applies to @sentry/node and some applies to @sentry/browser
* (e.g. tests in @sentry/tracing or @sentry/hub). Note that not all properties from the browser `window` object are
* available.
*
* @param properties The names of the properties to add
*/
export function addDOMPropertiesToGlobal(properties: string[]): void {
// we have to add things into the real global object
// because there are modules which call GLOBAL_OBJ as they load, which is too early for jest to intervene
const { window } = new JSDOM('', { url: 'http://dogs.are.great/' });
properties.forEach(prop => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(GLOBAL_OBJ as any)[prop] = window[prop];
});
}
/**
* Returns the symbol with the given description being used as a key in the given object.
*
* In the case where there are multiple symbols in the object with the same description, it throws an error.
*
* @param obj The object whose symbol-type key you want
* @param description The symbol's descriptor
* @returns The first symbol found in the object with the given description, or undefined if not found.
*/
export function getSymbolObjectKeyByName(obj: Record<string | symbol, any>, description: string): symbol | undefined {
const symbols = Object.getOwnPropertySymbols(obj);
const matches = symbols.filter(sym => sym.toString() === `Symbol(${description})`);
if (matches.length > 1) {
throw new Error(`More than one symbol key found with description '${description}'.`);
}
return matches[0] || undefined;
}
export const testOnlyIfNodeVersionAtLeast = (minVersion: number): jest.It => {
const currentNodeVersion = process.env.NODE_VERSION;
try {
if (Number(currentNodeVersion?.split('.')[0]) < minVersion) {
return it.skip;
}
} catch (oO) {
// we can't tell, so err on the side of running the test
}
return it;
};
export function getDefaultBrowserClientOptions(options: Partial<ClientOptions> = {}): ClientOptions {
return {
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => resolvedSyncPromise({})),
stackParser: () => [],
...options,
};
}