Skip to content

Commit 8a5af9b

Browse files
committed
fix: fix remote configuration e2e tests
1 parent 13884a7 commit 8a5af9b

1 file changed

Lines changed: 12 additions & 41 deletions

File tree

test/e2e/scenario/rum/remoteConfiguration.scenario.ts

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Page } from '@playwright/test'
22
import { test, expect } from '@playwright/test'
3-
import { createTest, html } from '../../lib/framework'
3+
import { createTest, html, waitForServersIdle } from '../../lib/framework'
44

55
const RC_APP_ID = 'e2e'
6+
const CACHE_KEY = `dd_rc_${RC_APP_ID}`
67

78
test.describe('remote configuration', () => {
89
createTest('should be fetched and applied')
@@ -47,7 +48,7 @@ test.describe('remote configuration', () => {
4748
version: { rcSerializedType: 'dynamic', strategy: 'dom', selector: '#version' },
4849
},
4950
})
50-
.withBody(html`<span id="version">123</span>`)
51+
.withHead(html`<span id="version">123</span>`)
5152
.run(async ({ page }) => {
5253
await waitForRemoteConfigurationToBeApplied(page)
5354
const initConfiguration = await page.evaluate(() => window.DD_RUM!.getInitConfiguration()!)
@@ -64,7 +65,7 @@ test.describe('remote configuration', () => {
6465
version: { rcSerializedType: 'dynamic', strategy: 'dom', selector: '#version', attribute: 'data-version' },
6566
},
6667
})
67-
.withBody(html`<span id="version" data-version="123"></span>`)
68+
.withHead(html`<span id="version" data-version="123"></span>`)
6869
.run(async ({ page }) => {
6970
await waitForRemoteConfigurationToBeApplied(page)
7071
const initConfiguration = await page.evaluate(() => window.DD_RUM!.getInitConfiguration()!)
@@ -81,11 +82,9 @@ test.describe('remote configuration', () => {
8182
version: { rcSerializedType: 'dynamic', strategy: 'js', path: 'dataLayer.version' },
8283
},
8384
})
84-
.withBody(html`
85+
.withHead(html`
8586
<script>
86-
dataLayer = {
87-
version: 'js-version',
88-
}
87+
window.dataLayer = { version: 'js-version' }
8988
</script>
9089
`)
9190
.run(async ({ page }) => {
@@ -157,32 +156,6 @@ test.describe('remote configuration', () => {
157156
expect(initConfiguration.version).toBeUndefined()
158157
})
159158

160-
createTest('should handle localStorage access failure gracefully')
161-
.withRum({
162-
remoteConfigurationId: 'e2e',
163-
})
164-
.withRemoteConfiguration({
165-
rum: {
166-
applicationId: 'e2e',
167-
version: { rcSerializedType: 'dynamic', strategy: 'localStorage', key: 'dd_app_version' },
168-
},
169-
})
170-
.withBody(html`
171-
<script>
172-
Object.defineProperty(window, 'localStorage', {
173-
get: function () {
174-
throw new Error('localStorage is not available')
175-
},
176-
configurable: true,
177-
})
178-
</script>
179-
`)
180-
.run(async ({ page }) => {
181-
await waitForRemoteConfigurationToBeApplied(page)
182-
const initConfiguration = await page.evaluate(() => window.DD_RUM!.getInitConfiguration()!)
183-
expect(initConfiguration.version).toBeUndefined()
184-
})
185-
186159
createTest('should resolve user context')
187160
.withRum({
188161
remoteConfigurationId: 'e2e',
@@ -231,13 +204,11 @@ test.describe('remote configuration', () => {
231204
})
232205
})
233206

207+
/* The background fetch on the initial page load writes the remote configuration into localStorage;
208+
* reloading lets the SDK pick it up synchronously on the next init().
209+
*/
234210
async function waitForRemoteConfigurationToBeApplied(page: Page) {
235-
for (let i = 0; i < 20; i++) {
236-
const initConfiguration = await page.evaluate(() => window.DD_RUM!.getInitConfiguration()!)
237-
if (initConfiguration.applicationId === RC_APP_ID) {
238-
break
239-
}
240-
console.log('wait for remote configuration to be applied')
241-
await page.waitForTimeout(100)
242-
}
211+
await page.waitForFunction((key) => localStorage.getItem(key) !== null, CACHE_KEY)
212+
await page.reload()
213+
await waitForServersIdle()
243214
}

0 commit comments

Comments
 (0)