Skip to content

Commit 0d70637

Browse files
authored
Make browser tests faster / less flaky (#713)
1 parent e0be11c commit 0d70637

File tree

16 files changed

+624
-174
lines changed

16 files changed

+624
-174
lines changed

.changeset/real-otters-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
add logic if plan.integrations is falsy

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"jest": "^28.1.0",
5151
"lint-staged": "^13.0.0",
5252
"lodash": "^4.17.21",
53+
"nock": "^13.2.9",
5354
"node-gyp": "^9.0.0",
5455
"prettier": "^2.6.2",
5556
"ts-jest": "^28.0.4",

packages/browser/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"devDependencies": {
6363
"@internal/config": "0.0.0",
64+
"@segment/analytics.js-integration": "^3.3.3",
6465
"@segment/analytics.js-integration-amplitude": "^3.3.3",
6566
"@segment/inspector-webext": "^2.0.3",
6667
"@size-limit/preset-big-lib": "^7.0.8",

packages/browser/src/browser/__tests__/integration.test.ts

Lines changed: 16 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
2+
import { cdnSettingsKitchenSink } from '../../test-helpers/fixtures/cdn-settings'
3+
import { createMockFetchImplementation } from '../../test-helpers/fixtures/create-fetch-method'
24
import { Context } from '@/core/context'
35
import { Plugin } from '@/core/plugin'
46
import { JSDOM } from 'jsdom'
@@ -11,27 +13,19 @@ import { AnalyticsBrowser, loadLegacySettings } from '..'
1113
import { isOffline } from '../../core/connection'
1214
import * as SegmentPlugin from '../../plugins/segmentio'
1315
import jar from 'js-cookie'
14-
import {
15-
AMPLITUDE_WRITEKEY,
16-
TEST_WRITEKEY,
17-
} from '../../test-helpers/test-writekeys'
1816
import { PriorityQueue } from '../../lib/priority-queue'
1917
import { getCDN, setGlobalCDNUrl } from '../../lib/parse-cdn'
2018
import { clearAjsBrowserStorage } from '../../test-helpers/browser-storage'
2119
import { ActionDestination } from '@/plugins/remote-loader'
22-
import { ClassicIntegrationBuilder } from '../../plugins/ajs-destination/types'
2320

24-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2521
let fetchCalls: Array<any>[] = []
26-
// Mock unfetch so we can record any http requests made
22+
2723
jest.mock('unfetch', () => {
28-
const originalModule = jest.requireActual('unfetch')
2924
return {
3025
__esModule: true,
31-
...originalModule,
32-
default: (...args: unknown[]) => {
33-
fetchCalls.push(args)
34-
return originalModule.apply(originalModule, args)
26+
default: (url: RequestInfo, body?: RequestInit) => {
27+
fetchCalls.push([url, body])
28+
return createMockFetchImplementation(cdnSettingsKitchenSink)(url, body)
3529
},
3630
}
3731
})
@@ -87,17 +81,18 @@ const enrichBilling: Plugin = {
8781
},
8882
}
8983

90-
const writeKey = TEST_WRITEKEY
84+
const writeKey = 'foo'
85+
const amplitudeWriteKey = 'bar'
9186

9287
beforeEach(() => {
9388
setGlobalCDNUrl(undefined as any)
9489
})
9590

9691
describe('Initialization', () => {
9792
beforeEach(async () => {
93+
fetchCalls = []
9894
jest.resetAllMocks()
9995
jest.resetModules()
100-
fetchCalls = []
10196
})
10297

10398
it('loads plugins', async () => {
@@ -204,6 +199,7 @@ describe('Initialization', () => {
204199
},
205200
],
206201
})
202+
207203
expect(fetchCalls[0][0]).toContain(overriddenCDNUrl)
208204
expect.assertions(3)
209205
})
@@ -680,7 +676,7 @@ describe('addDestinationMiddleware', () => {
680676
'amplitude',
681677
'latest',
682678
{
683-
apiKey: AMPLITUDE_WRITEKEY,
679+
apiKey: amplitudeWriteKey,
684680
},
685681
{}
686682
)
@@ -823,7 +819,7 @@ describe('deregister', () => {
823819
'amplitude',
824820
'latest',
825821
{
826-
apiKey: AMPLITUDE_WRITEKEY,
822+
apiKey: amplitudeWriteKey,
827823
},
828824
{}
829825
)
@@ -868,7 +864,7 @@ describe('retries', () => {
868864

869865
it('does not retry errored events if retryQueue setting is set to false', async () => {
870866
const [ajs] = await AnalyticsBrowser.load(
871-
{ writeKey: TEST_WRITEKEY },
867+
{ writeKey: writeKey },
872868
{ retryQueue: false }
873869
)
874870

@@ -951,147 +947,6 @@ describe('Segment.io overrides', () => {
951947
})
952948
})
953949

954-
describe('.Integrations', () => {
955-
beforeEach(async () => {
956-
jest.restoreAllMocks()
957-
jest.resetAllMocks()
958-
959-
const html = `
960-
<!DOCTYPE html>
961-
<head>
962-
<script>'hi'</script>
963-
</head>
964-
<body>
965-
</body>
966-
</html>
967-
`.trim()
968-
969-
const jsd = new JSDOM(html, {
970-
runScripts: 'dangerously',
971-
resources: 'usable',
972-
url: 'https://localhost',
973-
})
974-
975-
const windowSpy = jest.spyOn(global, 'window', 'get')
976-
windowSpy.mockImplementation(
977-
() => jsd.window as unknown as Window & typeof globalThis
978-
)
979-
980-
const documentSpy = jest.spyOn(global, 'document', 'get')
981-
documentSpy.mockImplementation(
982-
() => jsd.window.document as unknown as Document
983-
)
984-
})
985-
986-
it('lists all legacy destinations', async () => {
987-
const amplitude = new LegacyDestination(
988-
'Amplitude',
989-
'latest',
990-
{
991-
apiKey: AMPLITUDE_WRITEKEY,
992-
},
993-
{}
994-
)
995-
996-
const ga = new LegacyDestination('Google-Analytics', 'latest', {}, {})
997-
998-
const [analytics] = await AnalyticsBrowser.load({
999-
writeKey,
1000-
plugins: [amplitude, ga],
1001-
})
1002-
1003-
await analytics.ready()
1004-
1005-
expect(analytics.Integrations).toMatchInlineSnapshot(`
1006-
Object {
1007-
"Amplitude": [Function],
1008-
"Google-Analytics": [Function],
1009-
}
1010-
`)
1011-
})
1012-
1013-
it('catches destinations with dots in their names', async () => {
1014-
const amplitude = new LegacyDestination(
1015-
'Amplitude',
1016-
'latest',
1017-
{
1018-
apiKey: AMPLITUDE_WRITEKEY,
1019-
},
1020-
{}
1021-
)
1022-
1023-
const ga = new LegacyDestination('Google-Analytics', 'latest', {}, {})
1024-
const customerIO = new LegacyDestination('Customer.io', 'latest', {}, {})
1025-
1026-
const [analytics] = await AnalyticsBrowser.load({
1027-
writeKey,
1028-
plugins: [amplitude, ga, customerIO],
1029-
})
1030-
1031-
await analytics.ready()
1032-
1033-
expect(analytics.Integrations).toMatchInlineSnapshot(`
1034-
Object {
1035-
"Amplitude": [Function],
1036-
"Customer.io": [Function],
1037-
"Google-Analytics": [Function],
1038-
}
1039-
`)
1040-
})
1041-
1042-
it('uses directly provided classic integrations without fetching them from cdn', async () => {
1043-
const amplitude = // @ts-ignore
1044-
(await import('@segment/analytics.js-integration-amplitude')).default
1045-
1046-
const intializeSpy = jest.spyOn(amplitude.prototype, 'initialize')
1047-
const trackSpy = jest.spyOn(amplitude.prototype, 'track')
1048-
1049-
const [analytics] = await AnalyticsBrowser.load(
1050-
{
1051-
writeKey,
1052-
classicIntegrations: [
1053-
amplitude as unknown as ClassicIntegrationBuilder,
1054-
],
1055-
},
1056-
{
1057-
integrations: {
1058-
Amplitude: {
1059-
apiKey: 'abc',
1060-
},
1061-
},
1062-
}
1063-
)
1064-
1065-
await analytics.ready()
1066-
expect(intializeSpy).toHaveBeenCalledTimes(1)
1067-
1068-
await analytics.track('test event')
1069-
1070-
expect(trackSpy).toHaveBeenCalledTimes(1)
1071-
})
1072-
1073-
it('ignores directly provided classic integrations if settings for them are unavailable', async () => {
1074-
const amplitude = // @ts-ignore
1075-
(await import('@segment/analytics.js-integration-amplitude')).default
1076-
1077-
const intializeSpy = jest.spyOn(amplitude.prototype, 'initialize')
1078-
const trackSpy = jest.spyOn(amplitude.prototype, 'track')
1079-
1080-
const [analytics] = await AnalyticsBrowser.load({
1081-
writeKey,
1082-
classicIntegrations: [amplitude as unknown as ClassicIntegrationBuilder],
1083-
})
1084-
1085-
await analytics.ready()
1086-
1087-
expect(intializeSpy).not.toHaveBeenCalled()
1088-
1089-
await analytics.track('test event')
1090-
1091-
expect(trackSpy).not.toHaveBeenCalled()
1092-
})
1093-
})
1094-
1095950
describe('Options', () => {
1096951
beforeEach(async () => {
1097952
jest.restoreAllMocks()
@@ -1129,7 +984,7 @@ describe('Options', () => {
1129984
'amplitude',
1130985
'latest',
1131986
{
1132-
apiKey: AMPLITUDE_WRITEKEY,
987+
apiKey: amplitudeWriteKey,
1133988
},
1134989
{}
1135990
)
@@ -1165,7 +1020,7 @@ describe('Options', () => {
11651020
'amplitude',
11661021
'latest',
11671022
{
1168-
apiKey: AMPLITUDE_WRITEKEY,
1023+
apiKey: amplitudeWriteKey,
11691024
},
11701025
initOptions
11711026
)
@@ -1201,7 +1056,7 @@ describe('Options', () => {
12011056
'amplitude',
12021057
'latest',
12031058
{
1204-
apiKey: AMPLITUDE_WRITEKEY,
1059+
apiKey: amplitudeWriteKey,
12051060
},
12061061
initOptions
12071062
)

0 commit comments

Comments
 (0)