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
support 1.6.0 too
juan-fernandez committed Jun 19, 2024
commit 17de8593f6715f1379ddba779b90c821348cd63b
107 changes: 82 additions & 25 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
@@ -1,6 +1,5 @@
const { addHook, channel, AsyncResource } = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')

// Needs modifying node_modules/.pnpm/[email protected]/node_modules/import-in-the-middle/index.js
// the specifiers thing I don't understand

@@ -23,45 +22,103 @@ const isVitestTestRunner = (vitestPackage) => {
return vitestPackage.VitestTestRunner
}

const isVitestPlugin = (vitestPackage) => {
return vitestPackage.B?.name === 'BaseSequencer'
// maybe not valid???
// const isVitestPlugin = (vitestPackage) => {
// return vitestPackage.B?.name === 'BaseSequencer'
// }

// from 1.6.0 at least, BaseSequencer is not exported at the same point as startVitest
// const isVitestPlugin = (vitestPackage) => {
// return vitestPackage.s?.name === 'startVitest'
// }

const isCac = (vitestPackage) => {
return vitestPackage.c?.name === 'createCLI'
}

const isReporterPackage = (vitestPackage) => {
// return vitestPackage.a?.name === 'BasicReporter' // it doesn't even export, so can't use BaseReport
return vitestPackage.B?.name === 'BaseSequencer' // this is probably used everywhere, so maybe it has info
}

// --- do I need to specify a file? That's problematic because they contain a hash

// maybe use getEnvPackageName as "start" of session?

// vitestPackage.s only works for 1.1.0 - it doesn't for 1.6.0
addHook({
name: 'vitest',
versions: ['>=1.1.0 <1.6.0']
versions: ['>=1.1.0']
}, (vitestPackage, frameworkVersion) => {
debugger
if (isVitestPlugin(vitestPackage)) {
//
console.log('is S still s???????', vitestPackage)
// TODO: will the "s" (minified version) be the same in future versions?
shimmer.wrap(vitestPackage, 's', startVitest => async function () {
let onFinish

const flushPromise = new Promise(resolve => {
onFinish = resolve
})

if (isCac(vitestPackage)) {
const oldCreateCli = vitestPackage.c
// could be start session
// TODO: change to shimmer
vitestPackage.c = function () {
sessionAsyncResource.runInAsyncScope(() => {
// TODO: change command to proper command
testSessionStartCh.publish({ command: 'vitest run', frameworkVersion })
})
const res = await startVitest.apply(this, arguments)

sessionAsyncResource.runInAsyncScope(() => {
// TODO: get proper status
testSessionFinishCh.publish({ status: 'pass', onFinish })
return oldCreateCli.apply(this, arguments)
}
}
if (isReporterPackage(vitestPackage)) {
// this hack seems to work for 1.6.0
shimmer.wrap(vitestPackage.B.prototype, 'sort', sort => async function () {
// maybe this could also be start, instead of having two different places - easier

// BY THIS TIME IT'S TOO LATE!!!!! -> changing process.env does not propagate to the worker threads
// sessionAsyncResource.runInAsyncScope(() => {
// // TODO: change command to proper command
// testSessionStartCh.publish({ command: 'vitest run', frameworkVersion })
// })

shimmer.wrap(this.ctx, 'exit', exit => async function () {
let onFinish

const flushPromise = new Promise(resolve => {
onFinish = resolve
})
sessionAsyncResource.runInAsyncScope(() => {
// TODO: get proper status
testSessionFinishCh.publish({ status: 'pass', onFinish })
})

await flushPromise

return exit.apply(this, arguments)
})

await flushPromise

return res
return sort.apply(this, arguments)
})
}
// if (isVitestPlugin(vitestPackage)) {
// // this DOES NOT WORK for 1.6.0
// // TODO: will the "s" (minified version) be the same in future versions?
// shimmer.wrap(vitestPackage, 's', startVitest => async function () {
// let onFinish

// const flushPromise = new Promise(resolve => {
// onFinish = resolve
// })

// sessionAsyncResource.runInAsyncScope(() => {
// // TODO: change command to proper command
// testSessionStartCh.publish({ command: 'vitest run', frameworkVersion })
// })
// const res = await startVitest.apply(this, arguments)

// console.log('SHOULD NOT LOG THIS!!!!!!!!!!')
// sessionAsyncResource.runInAsyncScope(() => {
// // TODO: get proper status
// testSessionFinishCh.publish({ status: 'pass', onFinish })
// })

// await flushPromise

// return res
// })
// }

if (isVitestTestRunner(vitestPackage)) {
// test start
@@ -100,7 +157,7 @@ addHook({
versions: ['>=0.0.0']
}, vitestPackage => {
if (vitestPackage.startTests) {
shimmer.wrap(vitestPackage, 'startTests', startTests => async function (testPath, vitestTestRunner) {
shimmer.wrap(vitestPackage, 'startTests', startTests => async function (testPath) {
const asyncResource = new AsyncResource('bound-anonymous-fn')
asyncResource.runInAsyncScope(() => {
testSuiteStartCh.publish(testPath[0])