Skip to content

Commit 7b0e30e

Browse files
committed
♻️ Extract createEmbeddedConfigSetup to shared helper to avoid cross-scenario imports
Playwright prohibits importing between test scenario files. Move the shared setup factory to test/e2e/lib/helpers/embeddedConfigSetup.ts so both embeddedConfig and embeddedConfigDynamic scenarios can import it cleanly.
1 parent 519f24c commit 7b0e30e

File tree

3 files changed

+86
-74
lines changed

3 files changed

+86
-74
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { CONTEXT_RESOLUTION_HELPERS } from '@datadog/browser-sdk-endpoint'
2+
import { DEFAULT_RUM_CONFIGURATION, html, basePage, createCrossOriginScriptUrls } from '../framework'
3+
import type { SetupOptions, Servers } from '../framework'
4+
5+
export interface ContextItem {
6+
key: string
7+
value: {
8+
rcSerializedType: string
9+
value?: string
10+
strategy?: string
11+
name?: string
12+
path?: string
13+
selector?: string
14+
attribute?: string
15+
extractor?: unknown
16+
}
17+
}
18+
19+
/**
20+
* Creates a SetupFactory that generates an HTML page mimicking the embedded config bundle
21+
* produced by generateCombinedBundle(), with context resolution for user and globalContext.
22+
* Uses CONTEXT_RESOLUTION_HELPERS verbatim so tests exercise the same code as production.
23+
*
24+
* @param extraConfig - optional user[] / context[] arrays and optional pre-SDK script
25+
* @param extraConfig.user - optional user context items
26+
* @param extraConfig.context - optional global context items
27+
* @param extraConfig.preSDKScript - extra inline JS injected before the SDK script tag (e.g. to pre-set a cookie)
28+
*/
29+
export function createEmbeddedConfigSetup(extraConfig: {
30+
user?: ContextItem[]
31+
context?: ContextItem[]
32+
/** Extra inline JS injected before the SDK script tag (e.g. to pre-set a cookie). */
33+
preSDKScript?: string
34+
}) {
35+
return (options: SetupOptions, servers: Servers): string => {
36+
const { rumScriptUrl } = createCrossOriginScriptUrls(servers, options)
37+
38+
const embeddedConfig = {
39+
...DEFAULT_RUM_CONFIGURATION,
40+
sessionSampleRate: 100,
41+
proxy: servers.intake.origin,
42+
...extraConfig,
43+
}
44+
const configJson = JSON.stringify(embeddedConfig)
45+
const testContextJson = JSON.stringify(options.context)
46+
47+
const header = html`
48+
${extraConfig.preSDKScript ? `<script type="text/javascript">${extraConfig.preSDKScript}</script>` : ''}
49+
<script type="text/javascript" src="${rumScriptUrl}"></script>
50+
<script type="text/javascript">
51+
(function () {
52+
'use strict';
53+
var __DATADOG_REMOTE_CONFIG__ = ${configJson};
54+
var __dd_user = {};
55+
(__DATADOG_REMOTE_CONFIG__.user || []).forEach(function (item) {
56+
__dd_user[item.key] = __dd_resolveContextValue(item.value);
57+
});
58+
var __dd_globalContext = {};
59+
(__DATADOG_REMOTE_CONFIG__.context || []).forEach(function (item) {
60+
__dd_globalContext[item.key] = __dd_resolveContextValue(item.value);
61+
});
62+
var hasUser = Object.keys(__dd_user).length > 0;
63+
var hasGlobalContext = Object.keys(__dd_globalContext).length > 0;
64+
window.DD_RUM.init(Object.assign({}, __DATADOG_REMOTE_CONFIG__, {
65+
user: hasUser ? __dd_user : undefined,
66+
context: undefined,
67+
globalContext: hasGlobalContext ? __dd_globalContext : undefined
68+
}));
69+
// Re-apply test isolation context after init so it is not overwritten by the
70+
// globalContext init option (setContext replaces; we add test properties back).
71+
var __dd_testContext = ${testContextJson};
72+
Object.keys(__dd_testContext).forEach(function(key) {
73+
window.DD_RUM.setGlobalContextProperty(key, __dd_testContext[key]);
74+
});
75+
76+
${CONTEXT_RESOLUTION_HELPERS.trim()}
77+
})();
78+
</script>
79+
`
80+
return basePage({ header })
81+
}
82+
}

test/e2e/scenario/rum/embeddedConfig.scenario.ts

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,7 @@
1-
import { generateCombinedBundle, CONTEXT_RESOLUTION_HELPERS } from '@datadog/browser-sdk-endpoint'
1+
import { generateCombinedBundle } from '@datadog/browser-sdk-endpoint'
22
import { test, expect } from '@playwright/test'
3-
import { createTest, DEFAULT_RUM_CONFIGURATION, html, basePage, createCrossOriginScriptUrls } from '../../lib/framework'
4-
import type { SetupOptions, Servers } from '../../lib/framework'
5-
6-
export interface ContextItem {
7-
key: string
8-
value: { rcSerializedType: string; value?: string; strategy?: string; name?: string; path?: string; selector?: string; attribute?: string; extractor?: unknown }
9-
}
10-
11-
/**
12-
* Creates a SetupFactory that generates an HTML page mimicking the embedded config bundle
13-
* produced by generateCombinedBundle(), with context resolution for user and globalContext.
14-
* Uses CONTEXT_RESOLUTION_HELPERS verbatim so tests exercise the same code as production.
15-
*
16-
* @param extraConfig - optional user[] / context[] arrays and optional pre-SDK script
17-
* @param extraConfig.user - optional user context items
18-
* @param extraConfig.context - optional global context items
19-
* @param extraConfig.preSDKScript - extra inline JS injected before the SDK script tag (e.g. to pre-set a cookie)
20-
*/
21-
export function createEmbeddedConfigSetup(extraConfig: {
22-
user?: ContextItem[]
23-
context?: ContextItem[]
24-
/** Extra inline JS injected before the SDK script tag (e.g. to pre-set a cookie). */
25-
preSDKScript?: string
26-
}) {
27-
return (options: SetupOptions, servers: Servers): string => {
28-
const { rumScriptUrl } = createCrossOriginScriptUrls(servers, options)
29-
30-
const embeddedConfig = {
31-
...DEFAULT_RUM_CONFIGURATION,
32-
sessionSampleRate: 100,
33-
proxy: servers.intake.origin,
34-
...extraConfig,
35-
}
36-
const configJson = JSON.stringify(embeddedConfig)
37-
const testContextJson = JSON.stringify(options.context)
38-
39-
const header = html`
40-
${extraConfig.preSDKScript ? `<script type="text/javascript">${extraConfig.preSDKScript}</script>` : ''}
41-
<script type="text/javascript" src="${rumScriptUrl}"></script>
42-
<script type="text/javascript">
43-
(function () {
44-
'use strict';
45-
var __DATADOG_REMOTE_CONFIG__ = ${configJson};
46-
var __dd_user = {};
47-
(__DATADOG_REMOTE_CONFIG__.user || []).forEach(function (item) {
48-
__dd_user[item.key] = __dd_resolveContextValue(item.value);
49-
});
50-
var __dd_globalContext = {};
51-
(__DATADOG_REMOTE_CONFIG__.context || []).forEach(function (item) {
52-
__dd_globalContext[item.key] = __dd_resolveContextValue(item.value);
53-
});
54-
var hasUser = Object.keys(__dd_user).length > 0;
55-
var hasGlobalContext = Object.keys(__dd_globalContext).length > 0;
56-
window.DD_RUM.init(Object.assign({}, __DATADOG_REMOTE_CONFIG__, {
57-
user: hasUser ? __dd_user : undefined,
58-
context: undefined,
59-
globalContext: hasGlobalContext ? __dd_globalContext : undefined
60-
}));
61-
// Re-apply test isolation context after init so it is not overwritten by the
62-
// globalContext init option (setContext replaces; we add test properties back).
63-
var __dd_testContext = ${testContextJson};
64-
Object.keys(__dd_testContext).forEach(function(key) {
65-
window.DD_RUM.setGlobalContextProperty(key, __dd_testContext[key]);
66-
});
67-
68-
${CONTEXT_RESOLUTION_HELPERS.trim()}
69-
})();
70-
</script>
71-
`
72-
return basePage({ header })
73-
}
74-
}
3+
import { createTest } from '../../lib/framework'
4+
import { createEmbeddedConfigSetup } from '../../lib/helpers/embeddedConfigSetup'
755

766
test.describe('embedded configuration', () => {
777
createTest('should load SDK with embedded config and expose getInitConfiguration')

test/e2e/scenario/rum/embeddedConfigDynamic.scenario.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test, expect } from '@playwright/test'
22
import { generateCombinedBundle } from '@datadog/browser-sdk-endpoint'
33
import { createTest } from '../../lib/framework'
4-
import { createEmbeddedConfigSetup } from './embeddedConfig.scenario'
4+
import { createEmbeddedConfigSetup } from '../../lib/helpers/embeddedConfigSetup'
55

66
test.describe('embedded configuration with dynamic values', () => {
77
test('preserves cookie dynamic value markers in generated bundle', () => {

0 commit comments

Comments
 (0)