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

test(jest): Use jest-circus and instrument jest tests #22237

Merged
merged 8 commits into from
Nov 30, 2020

Conversation

billyvg
Copy link
Member

@billyvg billyvg commented Nov 23, 2020

This changes to use the new jest test runner: jest-circus and adds Sentry instrumentation to our test runs. You can view the transactions here: https://sentry.io/organizations/sentry/performance/?project=4857230

Comment on lines +1 to +258
if (parent && !Array.isArray(parent)) {
return parent.child(span);
} else if (Array.isArray(parent)) {
return parent.find(isNotTransaction).child(span);
}
return span;
},
};

case 'start_describe_definition':
case 'finish_describe_definition':
case 'add_test':
case 'add_hook':
case 'run_start':
case 'run_finish':
case 'test_todo':
case 'setup':
case 'teardown':
return null;

default:
return null;
}
}

handleTestEvent(event) {
if (!this.Sentry) {
return;
}

const data = this.getData(event);
const {name} = event;

if (!data) {
return;
}

const {obj, parentObj, dataStore, parentStore, op, description, beforeFinish} = data;

const testName = this.getName(obj);

if (name.includes('start')) {
// Make this an option maybe
if (!testName) {
return;
}

const spans = [];
const parentName = parentObj && this.getName(parentObj);
const spanProps = {op, description: description || testName};
const span =
parentObj && parentStore.has(parentName)
? Array.isArray(parentStore.get(parentName))
? parentStore
.get(parentName)
.map(s =>
typeof s.child === 'function'
? s.child(spanProps)
: s.startChild(spanProps)
)
: [parentStore.get(parentName).child(spanProps)]
: [this.transaction.startChild(spanProps)];

spans.push(...span);

// If we are starting a test, let's also make it a transaction so we can see our slowest tests
if (spanProps.op === 'test') {
spans.push(
Sentry.startTransaction({
...spanProps,
op: 'jest test',
name: spanProps.description,
description: null,
})
);
}

dataStore.set(testName, spans);

return;
}

if (dataStore.has(testName)) {
const spans = dataStore.get(testName);

spans.forEach(span => {
if (beforeFinish) {
span = beforeFinish(span);
if (!span) {
throw new Error('`beforeFinish()` needs to return a span');
}
}

span.finish();
});
}
}
}

module.exports = SentryEnvironment;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HazAT This might be interesting to do as an independent package.

@billyvg billyvg marked this pull request as ready for review November 23, 2020 21:53
@billyvg billyvg requested a review from a team as a code owner November 23, 2020 21:53
@billyvg billyvg requested a review from a team November 23, 2020 21:53
Copy link
Member

@scttcper scttcper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really neat, would like to see this as a node module

@billyvg billyvg merged commit 03c8c0b into getsentry:master Nov 30, 2020
@billyvg billyvg deleted the build/jest/add-perf-timings branch November 30, 2020 23:25
@github-actions github-actions bot locked and limited conversation to collaborators Dec 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants