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-visibility] Add support for vitest #4415

Merged
merged 31 commits into from
Jun 28, 2024
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0268820
add vitest support wip
juan-fernandez Jun 18, 2024
4b03ee1
better vitest support
juan-fernandez Jun 18, 2024
689d9aa
wip
juan-fernandez Jun 18, 2024
6512002
clean up
juan-fernandez Jun 19, 2024
d953763
clean up
juan-fernandez Jun 19, 2024
0d4f4ac
add e2e tests
juan-fernandez Jun 19, 2024
17de859
support 1.6.0 too
juan-fernandez Jun 19, 2024
b1ce709
clean up and add error handling
juan-fernandez Jun 20, 2024
81d8a1c
add session error message
juan-fernandez Jun 20, 2024
8c49dbd
add ci job
juan-fernandez Jun 20, 2024
b2a163e
remove volta
juan-fernandez Jun 20, 2024
6213755
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 20, 2024
420e0dd
support for before after all hooks
juan-fernandez Jun 21, 2024
e5ae80f
support beforeeach and aftereach
juan-fernandez Jun 21, 2024
5fd20c7
add test source file
juan-fernandez Jun 21, 2024
7edbaf6
better tests
juan-fernandez Jun 21, 2024
482db61
ts definitions
juan-fernandez Jun 24, 2024
cb580df
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 24, 2024
ba5088c
add support for skipped tests
juan-fernandez Jun 24, 2024
5c570a9
update comment
juan-fernandez Jun 24, 2024
1ddeee8
do not use iitm version and add wildcard logic
juan-fernandez Jun 25, 2024
95c5286
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 25, 2024
aa20aae
static list of esm plugins
juan-fernandez Jun 25, 2024
5551e63
add safeguard hook
juan-fernandez Jun 25, 2024
c15e63c
better regex logic
juan-fernandez Jun 25, 2024
ee2b032
remove unused var
juan-fernandez Jun 25, 2024
0d60e30
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 27, 2024
37f33e7
move esm first logic to hooks
juan-fernandez Jun 28, 2024
b29cfdb
remove dependency on getport
juan-fernandez Jun 28, 2024
6bee656
adding checks for hasSubscriber and asyncresource
juan-fernandez Jun 28, 2024
44ed749
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 28, 2024
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
Prev Previous commit
Next Next commit
adding checks for hasSubscriber and asyncresource
juan-fernandez committed Jun 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6bee6563e5ce854a3256f6826e2af26a4261b678
42 changes: 32 additions & 10 deletions packages/datadog-instrumentations/src/vitest.js
simon-id marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -88,6 +88,9 @@ addHook({
const { VitestTestRunner } = vitestPackage
// test start (only tests that are not marked as skip or todo)
shimmer.wrap(VitestTestRunner.prototype, 'onBeforeTryTask', onBeforeTryTask => async function (task) {
simon-id marked this conversation as resolved.
Show resolved Hide resolved
if (!testStartCh.hasSubscribers) {
return onBeforeTryTask.apply(this, arguments)
}
const asyncResource = new AsyncResource('bound-anonymous-fn')
taskToAsync.set(task, asyncResource)

@@ -100,15 +103,21 @@ addHook({
// test finish (only passed tests)
shimmer.wrap(VitestTestRunner.prototype, 'onAfterTryTask', onAfterTryTask =>
async function (task, { retry: retryCount }) {
simon-id marked this conversation as resolved.
Show resolved Hide resolved
if (!testFinishTimeCh.hasSubscribers) {
return onAfterTryTask.apply(this, arguments)
}
const result = await onAfterTryTask.apply(this, arguments)

const status = getVitestTestStatus(task, retryCount)
const asyncResource = taskToAsync.get(task)
simon-id marked this conversation as resolved.
Show resolved Hide resolved

// We don't finish here because the test might fail in a later hook
asyncResource.runInAsyncScope(() => {
testFinishTimeCh.publish({ status, task })
})
if (asyncResource) {
// We don't finish here because the test might fail in a later hook
asyncResource.runInAsyncScope(() => {
testFinishTimeCh.publish({ status, task })
})
}

return result
})

@@ -125,6 +134,9 @@ addHook({
return vitestPackage
}
shimmer.wrap(vitestPackage.B.prototype, 'sort', sort => async function () {
if (!testSessionFinishCh.hasSubscribers) {
uurien marked this conversation as resolved.
Show resolved Hide resolved
return sort.apply(this, arguments)
}
shimmer.wrap(this.ctx, 'exit', exit => async function () {
let onFinish

@@ -163,6 +175,9 @@ addHook({
filePattern: 'dist/vendor/cac.*'
}, (vitestPackage, frameworkVersion) => {
shimmer.wrap(vitestPackage, 'c', oldCreateCli => function () {
if (!testSessionStartCh.hasSubscribers) {
return oldCreateCli.apply(this, arguments)
}
sessionAsyncResource.runInAsyncScope(() => {
const processArgv = process.argv.slice(2).join(' ')
testSessionStartCh.publish({ command: `vitest ${processArgv}`, frameworkVersion })
@@ -182,6 +197,9 @@ addHook({
}, vitestPackage => {
shimmer.wrap(vitestPackage, 'startTests', startTests => async function (testPath) {
let testSuiteError = null
if (!testSuiteStartCh.hasSubscribers) {
return startTests.apply(this, arguments)
}

const testSuiteAsyncResource = new AsyncResource('bound-anonymous-fn')
testSuiteAsyncResource.runInAsyncScope(() => {
@@ -205,9 +223,11 @@ addHook({
if (state === 'skip') { // programmatic skip
testSkipCh.publish({ testName: getTestName(task), testSuiteAbsolutePath: task.suite.file.filepath })
} else if (state === 'pass') {
testAsyncResource.runInAsyncScope(() => {
testPassCh.publish({ task })
})
if (testAsyncResource) {
testAsyncResource.runInAsyncScope(() => {
testPassCh.publish({ task })
})
}
} else if (state === 'fail') {
// If it's failing, we have no accurate finish time, so we have to use `duration`
let testError
@@ -216,9 +236,11 @@ addHook({
testError = errors[0]
}

testAsyncResource.runInAsyncScope(() => {
testErrorCh.publish({ duration, error: testError })
})
if (testAsyncResource) {
testAsyncResource.runInAsyncScope(() => {
testErrorCh.publish({ duration, error: testError })
})
}
if (errors?.length) {
testSuiteError = testError // we store the error to bubble it up to the suite
}