Skip to content

Commit 0f65a3d

Browse files
authored
Merge pull request #3 from getsentry/feat/expose-sentry-to-the-tests
feat: expose sentry to the tests
2 parents c4aaf30 + b86ff78 commit 0f65a3d

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

createEnvironment.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function createEnvironment({ baseEnvironment } = {}) {
5353
tags: transactionOptions.tags,
5454
});
5555
this.global.transaction = this.transaction;
56+
this.global.Sentry = this.Sentry;
5657

5758
this.Sentry.configureScope((scope) => scope.setSpan(this.transaction));
5859

@@ -251,15 +252,20 @@ function createEnvironment({ baseEnvironment } = {}) {
251252

252253
// If we are starting a test, let's also make it a transaction so we can see our slowest tests
253254
if (spanProps.op === "test") {
254-
spans.push(
255-
this.Sentry.startTransaction({
256-
...spanProps,
257-
op: "jest test",
258-
name: spanProps.description,
259-
description: null,
260-
tags: this.options.transactionOptions?.tags,
261-
})
262-
);
255+
const testTransaction = this.Sentry.startTransaction({
256+
...spanProps,
257+
op: "jest test",
258+
name: spanProps.description,
259+
description: null,
260+
// attach the trace id and span id of parent transaction so they're part of the same trace
261+
parentSpanId: span[0].spanId,
262+
traceId: span[0].transaction.traceId,
263+
tags: this.options.transactionOptions?.tags,
264+
})
265+
spans.push(testTransaction);
266+
267+
// ensure that the test transaction is on the scope while it's happening
268+
this.Sentry.configureScope((scope) => scope.setSpan(testTransaction));
263269
}
264270

265271
dataStore.set(testName, spans);
@@ -272,11 +278,7 @@ function createEnvironment({ baseEnvironment } = {}) {
272278

273279
if (name.includes("failure")) {
274280
if (event.error) {
275-
const testSpan = spans.find(span => span.transaction.op === "jest test")
276-
this.Sentry.configureScope(scope => {
277-
scope.setSpan(testSpan.transaction ?? spans[0].transaction);
278-
});
279-
const exception = this.Sentry.captureException(event.error);
281+
this.Sentry.captureException(event.error);
280282
}
281283
}
282284

@@ -289,6 +291,12 @@ function createEnvironment({ baseEnvironment } = {}) {
289291
}
290292

291293
span.finish();
294+
295+
// if this is finishing a jest test span, then put the test suite transaction
296+
// back on the scope
297+
if (span.op === "jest test") {
298+
this.Sentry.configureScope((scope) => scope.setSpan(this.transaction));
299+
}
292300
});
293301
}
294302
}

0 commit comments

Comments
 (0)