|
1 |
| -import { Integration } from '@sentry/types'; |
| 1 | +import { Integration, Options } from '@sentry/types'; |
2 | 2 |
|
3 | 3 | import { getIntegrationsToSetup } from '../../src/integration';
|
4 | 4 |
|
5 | 5 | /** JSDoc */
|
6 | 6 | class MockIntegration implements Integration {
|
7 | 7 | public name: string;
|
8 | 8 |
|
9 |
| - public constructor(name: string) { |
| 9 | + // Only for testing - tag to keep separate instances straight when testing deduplication |
| 10 | + public tag?: string; |
| 11 | + |
| 12 | + public constructor(name: string, tag?: string) { |
10 | 13 | this.name = name;
|
| 14 | + this.tag = tag; |
11 | 15 | }
|
12 | 16 |
|
13 | 17 | public setupOnce(): void {
|
14 | 18 | // noop
|
15 | 19 | }
|
16 | 20 | }
|
17 | 21 |
|
| 22 | +type TestCase = [ |
| 23 | + string, // test name |
| 24 | + Options['defaultIntegrations'], // default integrations |
| 25 | + Options['integrations'], // user-provided integrations |
| 26 | + Array<string | string[]>, // expected resulst |
| 27 | +]; |
| 28 | + |
18 | 29 | describe('getIntegrationsToSetup', () => {
|
| 30 | + describe('no duplicate integrations', () => { |
| 31 | + const defaultIntegrations = [new MockIntegration('ChaseSquirrels')]; |
| 32 | + const userIntegrationsArray = [new MockIntegration('CatchTreats')]; |
| 33 | + const userIntegrationsFunction = (defaults: Integration[]) => [...defaults, ...userIntegrationsArray]; |
| 34 | + |
| 35 | + const testCases: TestCase[] = [ |
| 36 | + // each test case is [testName, defaultIntegrations, userIntegrations, expectedResult] |
| 37 | + ['no default integrations, no user integrations provided', false, undefined, []], |
| 38 | + ['no default integrations, empty user-provided array', false, [], []], |
| 39 | + ['no default integrations, user-provided array', false, userIntegrationsArray, ['CatchTreats']], |
| 40 | + ['no default integrations, user-provided function', false, userIntegrationsFunction, ['CatchTreats']], |
| 41 | + ['with default integrations, no user integrations provided', defaultIntegrations, undefined, ['ChaseSquirrels']], |
| 42 | + ['with default integrations, empty user-provided array', defaultIntegrations, [], ['ChaseSquirrels']], |
| 43 | + [ |
| 44 | + 'with default integrations, user-provided array', |
| 45 | + defaultIntegrations, |
| 46 | + userIntegrationsArray, |
| 47 | + ['ChaseSquirrels', 'CatchTreats'], |
| 48 | + ], |
| 49 | + [ |
| 50 | + 'with default integrations, user-provided function', |
| 51 | + defaultIntegrations, |
| 52 | + userIntegrationsFunction, |
| 53 | + ['ChaseSquirrels', 'CatchTreats'], |
| 54 | + ], |
| 55 | + ]; |
| 56 | + |
| 57 | + test.each(testCases)('%s', (_, defaultIntegrations, userIntegrations, expected) => { |
| 58 | + const integrations = getIntegrationsToSetup({ |
| 59 | + defaultIntegrations, |
| 60 | + integrations: userIntegrations, |
| 61 | + }); |
| 62 | + expect(integrations.map(i => i.name)).toEqual(expected); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('deduping', () => { |
| 67 | + // No duplicates |
| 68 | + const defaultIntegrations = [new MockIntegration('ChaseSquirrels', 'defaultA')]; |
| 69 | + const userIntegrationsArray = [new MockIntegration('CatchTreats', 'userA')]; |
| 70 | + |
| 71 | + // Duplicates within either default or user integrations, but no overlap between them (to show that last one wins) |
| 72 | + const duplicateDefaultIntegrations = [ |
| 73 | + new MockIntegration('ChaseSquirrels', 'defaultA'), |
| 74 | + new MockIntegration('ChaseSquirrels', 'defaultB'), |
| 75 | + ]; |
| 76 | + const duplicateUserIntegrationsArray = [ |
| 77 | + new MockIntegration('CatchTreats', 'userA'), |
| 78 | + new MockIntegration('CatchTreats', 'userB'), |
| 79 | + ]; |
| 80 | + const duplicateUserIntegrationsFunctionDefaultsFirst = (defaults: Integration[]) => [ |
| 81 | + ...defaults, |
| 82 | + ...duplicateUserIntegrationsArray, |
| 83 | + ]; |
| 84 | + const duplicateUserIntegrationsFunctionDefaultsSecond = (defaults: Integration[]) => [ |
| 85 | + ...duplicateUserIntegrationsArray, |
| 86 | + ...defaults, |
| 87 | + ]; |
| 88 | + |
| 89 | + // User integrations containing new instances of default integrations (to show that user integration wins) |
| 90 | + const userIntegrationsMatchingDefaultsArray = [ |
| 91 | + new MockIntegration('ChaseSquirrels', 'userA'), |
| 92 | + new MockIntegration('CatchTreats', 'userA'), |
| 93 | + ]; |
| 94 | + const userIntegrationsMatchingDefaultsFunctionDefaultsFirst = (defaults: Integration[]) => [ |
| 95 | + ...defaults, |
| 96 | + ...userIntegrationsMatchingDefaultsArray, |
| 97 | + ]; |
| 98 | + const userIntegrationsMatchingDefaultsFunctionDefaultsSecond = (defaults: Integration[]) => [ |
| 99 | + ...userIntegrationsMatchingDefaultsArray, |
| 100 | + ...defaults, |
| 101 | + ]; |
| 102 | + |
| 103 | + const testCases: TestCase[] = [ |
| 104 | + // each test case is [testName, defaultIntegrations, userIntegrations, expectedResult] |
| 105 | + [ |
| 106 | + 'duplicate default integrations', |
| 107 | + duplicateDefaultIntegrations, |
| 108 | + userIntegrationsArray, |
| 109 | + [ |
| 110 | + ['ChaseSquirrels', 'defaultB'], |
| 111 | + ['CatchTreats', 'userA'], |
| 112 | + ], |
| 113 | + ], |
| 114 | + [ |
| 115 | + 'duplicate user integrations, user-provided array', |
| 116 | + defaultIntegrations, |
| 117 | + duplicateUserIntegrationsArray, |
| 118 | + [ |
| 119 | + ['ChaseSquirrels', 'defaultA'], |
| 120 | + ['CatchTreats', 'userB'], |
| 121 | + ], |
| 122 | + ], |
| 123 | + [ |
| 124 | + 'duplicate user integrations, user-provided function with defaults first', |
| 125 | + defaultIntegrations, |
| 126 | + duplicateUserIntegrationsFunctionDefaultsFirst, |
| 127 | + [ |
| 128 | + ['ChaseSquirrels', 'defaultA'], |
| 129 | + ['CatchTreats', 'userB'], |
| 130 | + ], |
| 131 | + ], |
| 132 | + [ |
| 133 | + 'duplicate user integrations, user-provided function with defaults second', |
| 134 | + defaultIntegrations, |
| 135 | + duplicateUserIntegrationsFunctionDefaultsSecond, |
| 136 | + [ |
| 137 | + ['CatchTreats', 'userB'], |
| 138 | + ['ChaseSquirrels', 'defaultA'], |
| 139 | + ], |
| 140 | + ], |
| 141 | + [ |
| 142 | + 'same integration in default and user integrations, user-provided array', |
| 143 | + defaultIntegrations, |
| 144 | + userIntegrationsMatchingDefaultsArray, |
| 145 | + [ |
| 146 | + ['ChaseSquirrels', 'userA'], |
| 147 | + ['CatchTreats', 'userA'], |
| 148 | + ], |
| 149 | + ], |
| 150 | + [ |
| 151 | + 'same integration in default and user integrations, user-provided function with defaults first', |
| 152 | + defaultIntegrations, |
| 153 | + userIntegrationsMatchingDefaultsFunctionDefaultsFirst, |
| 154 | + [ |
| 155 | + ['ChaseSquirrels', 'userA'], |
| 156 | + ['CatchTreats', 'userA'], |
| 157 | + ], |
| 158 | + ], |
| 159 | + [ |
| 160 | + 'same integration in default and user integrations, user-provided function with defaults second', |
| 161 | + defaultIntegrations, |
| 162 | + userIntegrationsMatchingDefaultsFunctionDefaultsSecond, |
| 163 | + [ |
| 164 | + ['ChaseSquirrels', 'userA'], |
| 165 | + ['CatchTreats', 'userA'], |
| 166 | + ], |
| 167 | + ], |
| 168 | + ]; |
| 169 | + |
| 170 | + test.each(testCases)('%s', (_, defaultIntegrations, userIntegrations, expected) => { |
| 171 | + const integrations = getIntegrationsToSetup({ |
| 172 | + defaultIntegrations: defaultIntegrations, |
| 173 | + integrations: userIntegrations, |
| 174 | + }) as MockIntegration[]; |
| 175 | + |
| 176 | + expect(integrations.map(i => [i.name, i.tag])).toEqual(expected); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + describe('puts `Debug` integration last', () => { |
| 181 | + // No variations here (default vs user, duplicates, user array vs user function, etc) because by the time we're |
| 182 | + // dealing with the `Debug` integration, all of the combining and deduping has already been done |
| 183 | + const noDebug = [new MockIntegration('ChaseSquirrels')]; |
| 184 | + const debugNotLast = [new MockIntegration('Debug'), new MockIntegration('CatchTreats')]; |
| 185 | + const debugAlreadyLast = [new MockIntegration('ChaseSquirrels'), new MockIntegration('Debug')]; |
| 186 | + |
| 187 | + const testCases: TestCase[] = [ |
| 188 | + // each test case is [testName, defaultIntegrations, userIntegrations, expectedResult] |
| 189 | + ['`Debug` not present', false, noDebug, ['ChaseSquirrels']], |
| 190 | + ['`Debug` not originally last', false, debugNotLast, ['CatchTreats', 'Debug']], |
| 191 | + ['`Debug` already last', false, debugAlreadyLast, ['ChaseSquirrels', 'Debug']], |
| 192 | + ]; |
| 193 | + |
| 194 | + test.each(testCases)('%s', (_, defaultIntegrations, userIntegrations, expected) => { |
| 195 | + const integrations = getIntegrationsToSetup({ |
| 196 | + defaultIntegrations, |
| 197 | + integrations: userIntegrations, |
| 198 | + }); |
| 199 | + expect(integrations.map(i => i.name)).toEqual(expected); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
19 | 203 | it('works with empty array', () => {
|
20 | 204 | const integrations = getIntegrationsToSetup({
|
21 | 205 | integrations: [],
|
@@ -48,20 +232,6 @@ describe('getIntegrationsToSetup', () => {
|
48 | 232 | expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
|
49 | 233 | });
|
50 | 234 |
|
51 |
| - it('filter duplicated items and always let first win', () => { |
52 |
| - const first = new MockIntegration('foo'); |
53 |
| - (first as any).order = 'first'; |
54 |
| - const second = new MockIntegration('foo'); |
55 |
| - (second as any).order = 'second'; |
56 |
| - |
57 |
| - const integrations = getIntegrationsToSetup({ |
58 |
| - integrations: [first, second, new MockIntegration('bar')], |
59 |
| - }); |
60 |
| - |
61 |
| - expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
62 |
| - expect((integrations[0] as any).order).toEqual('first'); |
63 |
| - }); |
64 |
| - |
65 | 235 | it('work with empty defaults', () => {
|
66 | 236 | const integrations = getIntegrationsToSetup({
|
67 | 237 | defaultIntegrations: [],
|
|
0 commit comments