Skip to content

Commit c9f017f

Browse files
jest: wrap outer hooks too (#7587)
1 parent adf6379 commit c9f017f

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

integration-tests/ci-visibility/test-custom-tags/custom-tags.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@ const tracer = require('dd-trace')
44
const assert = require('assert')
55

66
const sum = require('../test/sum')
7-
describe('test optimization custom tags', () => {
7+
describe('test optimization', () => {
88
beforeEach(() => {
99
const testSpan = tracer.scope().active()
10-
testSpan.setTag('custom_tag.beforeEach', 'true')
10+
testSpan.setTag('outer_scope.beforeEach', 'true')
1111
})
1212

13-
it('can report tests', () => {
14-
const testSpan = tracer.scope().active()
15-
testSpan.setTag('custom_tag.it', 'true')
16-
assert.strictEqual(sum(1, 2), 3)
13+
describe('custom tags', () => {
14+
beforeEach(() => {
15+
const testSpan = tracer.scope().active()
16+
testSpan.setTag('custom_tag.beforeEach', 'true')
17+
})
18+
19+
it('can report tests', () => {
20+
const testSpan = tracer.scope().active()
21+
testSpan.setTag('custom_tag.it', 'true')
22+
assert.strictEqual(sum(1, 2), 3)
23+
})
24+
25+
afterEach(() => {
26+
const testSpan = tracer.scope().active()
27+
testSpan.setTag('custom_tag.afterEach', 'true')
28+
})
1729
})
1830

1931
afterEach(() => {
2032
const testSpan = tracer.scope().active()
21-
testSpan.setTag('custom_tag.afterEach', 'true')
33+
testSpan.setTag('outer_scope.afterEach', 'true')
2234
})
2335
})

integration-tests/jest/jest.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5971,9 +5971,11 @@ describe(`jest@${JEST_VERSION} commonJS`, () => {
59715971

59725972
assertObjectContains(test, {
59735973
meta: {
5974+
'outer_scope.beforeEach': 'true',
59745975
'custom_tag.beforeEach': 'true',
59755976
'custom_tag.it': 'true',
59765977
'custom_tag.afterEach': 'true',
5978+
'outer_scope.afterEach': 'true',
59775979
},
59785980
})
59795981
})

packages/datadog-instrumentations/src/jest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,13 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
466466
testContexts.set(event.test, ctx)
467467

468468
testStartCh.runStores(ctx, () => {
469-
for (const hook of event.test.parent.hooks) {
469+
let p = event.test.parent
470+
const hooks = []
471+
while (p != null) {
472+
hooks.push(...p.hooks)
473+
p = p.parent
474+
}
475+
for (const hook of hooks) {
470476
let hookFn = hook.fn
471477
if (originalHookFns.has(hook)) {
472478
hookFn = originalHookFns.get(hook)

0 commit comments

Comments
 (0)