Skip to content

Commit 9c58aef

Browse files
[test optimization] Fix test optimization tests in v5 release line (#5844)
1 parent b9375e9 commit 9c58aef

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

ci/cypress/plugin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
const { NODE_MAJOR } = require('../../version')
2+
3+
// These polyfills are here because cypress@6.7.0, which we still support for v5, runs its plugin code
4+
// with Node.js@12.
5+
if (NODE_MAJOR < 18) {
6+
require('./polyfills')
7+
}
8+
19
require('../init')
210

311
module.exports = require('../../packages/datadog-plugin-cypress/src/plugin')

ci/cypress/polyfills.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
if (!Object.hasOwn) {
2+
Object.defineProperty(Object, 'hasOwn', {
3+
// eslint-disable-next-line prefer-object-has-own
4+
value: (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop),
5+
writable: true,
6+
configurable: true,
7+
})
8+
}
9+
10+
if (!Array.prototype.at) {
11+
// eslint-disable-next-line no-extend-native
12+
Object.defineProperty(Array.prototype, 'at', {
13+
value: function (n) {
14+
const len = this.length
15+
if (len === 0) return
16+
let index = Math.trunc(n)
17+
if (index < 0) index += len
18+
return (index < 0 || index >= len) ? undefined : this[index]
19+
},
20+
writable: true,
21+
configurable: true
22+
})
23+
}

integration-tests/ci-visibility/playwright-tests/landing-page-test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@ test.beforeEach(async ({ page }) => {
55
await page.goto(process.env.PW_BASE_URL)
66
})
77

8+
// active span is only supported from >=1.38.0, at which point we add DD_PLAYWRIGHT_WORKER to the env
9+
function setActiveTestSpanTags (tags) {
10+
if (process.env.DD_PLAYWRIGHT_WORKER) {
11+
tracer.scope().active().addTags(tags)
12+
}
13+
return null
14+
}
15+
816
test.describe('highest-level-describe', () => {
917
test.describe(' leading and trailing spaces ', () => {
1018
// even empty describe blocks should be allowed
1119
test.describe(' ', () => {
1220
test.beforeEach(async ({ page }) => {
13-
tracer.scope().active().addTags({
21+
setActiveTestSpanTags({
1422
'custom_tag.beforeEach': 'hello beforeEach'
1523
})
1624
})
1725
test.afterEach(async ({ page }) => {
18-
tracer.scope().active().addTags({
26+
setActiveTestSpanTags({
1927
'custom_tag.afterEach': 'hello afterEach'
2028
})
2129
})
2230
test('should work with passing tests', async ({ page }) => {
23-
tracer.scope().active().addTags({
31+
setActiveTestSpanTags({
2432
'custom_tag.it': 'hello it'
2533
})
2634
await expect(page.locator('.hello-world')).toHaveText([

integration-tests/playwright/playwright.spec.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,15 @@ versions.forEach((version) => {
201201
JSON.stringify({ arguments: { browser: 'chromium' }, metadata: {} })
202202
)
203203
assert.exists(testEvent.content.metrics[DD_HOST_CPU_COUNT])
204-
if (testEvent.content.meta[TEST_STATUS] !== 'skip' &&
205-
testEvent.content.meta[TEST_SUITE].includes('landing-page-test.js')) {
206-
assert.propertyVal(testEvent.content.meta, 'custom_tag.beforeEach', 'hello beforeEach')
207-
assert.propertyVal(testEvent.content.meta, 'custom_tag.afterEach', 'hello afterEach')
208-
}
209-
if (testEvent.content.meta[TEST_NAME].includes('should work with passing tests')) {
210-
assert.propertyVal(testEvent.content.meta, 'custom_tag.it', 'hello it')
204+
if (version === 'latest' || satisfies(version, '>=1.38.0')) {
205+
if (testEvent.content.meta[TEST_STATUS] !== 'skip' &&
206+
testEvent.content.meta[TEST_SUITE].includes('landing-page-test.js')) {
207+
assert.propertyVal(testEvent.content.meta, 'custom_tag.beforeEach', 'hello beforeEach')
208+
assert.propertyVal(testEvent.content.meta, 'custom_tag.afterEach', 'hello afterEach')
209+
}
210+
if (testEvent.content.meta[TEST_NAME].includes('should work with passing tests')) {
211+
assert.propertyVal(testEvent.content.meta, 'custom_tag.it', 'hello it')
212+
}
211213
}
212214
})
213215

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
module.exports = (on, config) => {
2-
// We can't use the tracer available in the testing process, because this code is
3-
// run in a different process. We need to init a different tracer reporting to the
4-
// url set by the plugin agent
5-
require('../../../../../dd-trace').init({ startupLogs: false })
6-
require('../../../../src/plugin')(on, config)
7-
}
1+
module.exports = require('../../../../../../ci/cypress/plugin')

packages/datadog-plugin-cypress/test/index.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ describe('Plugin', function () {
3434
return agent.load()
3535
})
3636
beforeEach(function (done) {
37+
// This test does not check test optimization agentless protocol,
38+
// so we'll make the tracer default to reporting to v0.4/traces
39+
agent.setAvailableEndpoints([])
3740
this.timeout(10000)
3841
agentListenPort = agent.server.address().port
3942
cypressExecutable = require(`../../../versions/cypress@${version}`).get()

0 commit comments

Comments
 (0)