-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlazyLoadIntegration.test.ts
85 lines (69 loc) · 2.81 KB
/
lazyLoadIntegration.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
/**
* @vitest-environment jsdom
*/
import { afterAll, afterEach, beforeEach, describe, expect, test } from 'vitest';
import { TextDecoder, TextEncoder } from 'util';
import { SDK_VERSION, lazyLoadIntegration } from '../../src';
import * as Sentry from '../../src';
const patchedEncoder = (!global.window.TextEncoder && (global.window.TextEncoder = TextEncoder)) || true;
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails (deleted in afterAll)
const patchedDecoder = (!global.window.TextDecoder && (global.window.TextDecoder = TextDecoder)) || true;
import { JSDOM } from 'jsdom';
const globalDocument = global.document;
const globalWindow = global.window;
const globalLocation = global.location;
describe('lazyLoadIntegration', () => {
beforeEach(() => {
const dom = new JSDOM('<body></body>', {
runScripts: 'dangerously',
resources: 'usable',
});
global.document = dom.window.document;
// @ts-expect-error need to override global document
global.window = dom.window;
global.location = dom.window.location;
// @ts-expect-error For testing sake
global.Sentry = undefined;
});
// Reset back to previous values
afterEach(() => {
global.document = globalDocument;
global.window = globalWindow;
global.location = globalLocation;
});
afterAll(() => {
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails
patchedEncoder && delete global.window.TextEncoder;
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails
patchedDecoder && delete global.window.TextDecoder;
});
test('it rejects invalid name', async () => {
// @ts-expect-error For testing sake - otherwise this bails out anyhow
global.Sentry = Sentry;
// @ts-expect-error we want to test this
await expect(() => lazyLoadIntegration('invalid!!!')).rejects.toThrow('Cannot lazy load integration: invalid!!!');
});
test('it does not inject a script tag if integration already exists', async () => {
// @ts-expect-error For testing sake
global.Sentry = Sentry;
const integration = await lazyLoadIntegration('httpClientIntegration');
expect(integration).toBe(Sentry.httpClientIntegration);
expect(global.document.querySelectorAll('script')).toHaveLength(0);
});
test('it injects a script tag if integration is not yet loaded', async () => {
// @ts-expect-error For testing sake
global.Sentry = {
...Sentry,
httpClientIntegration: undefined,
};
try {
await lazyLoadIntegration('httpClientIntegration');
} catch {
// skip
}
expect(global.document.querySelectorAll('script')).toHaveLength(1);
expect(global.document.querySelector('script')?.src).toEqual(
`https://browser.sentry-cdn.com/${SDK_VERSION}/httpclient.min.js`,
);
});
});