Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(globals): allow passing globals to jest #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions createEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const Sentry = require("@sentry/node");
require("@sentry/tracing");
const {ProfilingIntegration} = require("@sentry/profiling-node");

let DID_INIT_SENTRY = false;
const { ProfilingIntegration } = require("@sentry/profiling-node");

function isNotTransaction(span) {
return span.op !== "jest test";
Expand All @@ -17,34 +15,38 @@ function createEnvironment({ baseEnvironment } = {}) {

const [config, context] = args;

this.options = config.projectConfig.testEnvironmentOptions?.sentryConfig
this.options = config.projectConfig.testEnvironmentOptions?.sentryConfig;

// Allow sharing of globals between tests
if (config.projectConfig.testEnvironmentOptions.global) {
for (const key in config.projectConfig.testEnvironmentOptions?.global) {
this.global[key] =
config.projectConfig.testEnvironmentOptions?.global[key];
}
}

if (
!this.options ||
// Do not include in watch mode... unfortunately, I don't think there's
// a better watch to detect when jest is in watch mode
process.argv.includes('--watch') ||
process.argv.includes('--watchAll')
process.argv.includes("--watch") ||
process.argv.includes("--watchAll")
) {
return;
}

const { init } = this.options;

this.Sentry = Sentry;
if(!DID_INIT_SENTRY) {
// Ensure integration is an array as init is a user input
if(!Array.isArray(init.integrations)) {
init.integrations = [];
}

// Add profiling integration
init.integrations.push(new ProfilingIntegration());

this.Sentry.init(init);
DID_INIT_SENTRY = true
// Ensure integration is an array as init is a user input
if (!Array.isArray(init.integrations)) {
init.integrations = [];
}

// Add profiling integration
init.integrations.push(new ProfilingIntegration());
this.Sentry.init(init);

this.testPath = context.testPath.replace(process.cwd(), "");

this.runDescribe = new Map();
Expand Down Expand Up @@ -253,10 +255,7 @@ function createEnvironment({ baseEnvironment } = {}) {
const span =
parentObj && parentStore.has(parentName)
? Array.isArray(parentStore.get(parentName))
? parentStore
.get(parentName)
.map((s) =>
s.startChild(spanProps))
? parentStore.get(parentName).map((s) => s.startChild(spanProps))
: [parentStore.get(parentName).startChild(spanProps)]
: [this.transaction.startChild(spanProps)];

Expand All @@ -273,7 +272,7 @@ function createEnvironment({ baseEnvironment } = {}) {
parentSpanId: span[0].spanId,
traceId: span[0].transaction.traceId,
tags: this.options.transactionOptions?.tags,
})
});
spans.push(testTransaction);

// ensure that the test transaction is on the scope while it's happening
Expand Down Expand Up @@ -307,7 +306,9 @@ function createEnvironment({ baseEnvironment } = {}) {
// if this is finishing a jest test span, then put the test suite transaction
// back on the scope
if (span.op === "jest test") {
this.Sentry.configureScope((scope) => scope.setSpan(this.transaction));
this.Sentry.configureScope((scope) =>
scope.setSpan(this.transaction)
);
}
});
}
Expand Down