1
+ const v8 = require ( 'node:v8' )
2
+
1
3
const { addHook, channel, AsyncResource } = require ( './helpers/instrument' )
2
4
const shimmer = require ( '../../datadog-shimmer' )
3
5
const log = require ( '../../dd-trace/src/log' )
4
-
5
6
// test hooks
6
7
const testStartCh = channel ( 'ci:vitest:test:start' )
7
8
const testFinishTimeCh = channel ( 'ci:vitest:test:finish-time' )
@@ -26,6 +27,8 @@ const knownTestsCh = channel('ci:vitest:known-tests')
26
27
const isEarlyFlakeDetectionFaultyCh = channel ( 'ci:vitest:is-early-flake-detection-faulty' )
27
28
const testManagementTestsCh = channel ( 'ci:vitest:test-management-tests' )
28
29
30
+ const workerReporterCh = channel ( 'ci:vitest:worker-report:trace' )
31
+
29
32
const taskToCtx = new WeakMap ( )
30
33
const taskToStatuses = new WeakMap ( )
31
34
const newTasks = new WeakSet ( )
@@ -61,6 +64,8 @@ function getProvidedContext () {
61
64
_ddIsFlakyTestRetriesEnabled : isFlakyTestRetriesEnabled
62
65
} = globalThis . __vitest_worker__ . providedContext
63
66
67
+ // console.log('globalThis.__vitest_worker__', globalThis.__vitest_worker__)
68
+
64
69
return {
65
70
isDiEnabled : _ddIsDiEnabled ,
66
71
isEarlyFlakeDetectionEnabled : _ddIsEarlyFlakeDetectionEnabled ,
@@ -187,6 +192,11 @@ function getSortWrapper (sort) {
187
192
let knownTests = { }
188
193
let testManagementTests = { }
189
194
195
+ // console.log('this.ctx.vitest', this.ctx.vitest)
196
+ // console.log('this.ctx.rpc', this.ctx.rpc)
197
+ console . log ( '__vitest_worker__' , global . __vitest_worker__ )
198
+ // console.log('this.ctx.getCoreWorkspaceProject()', this.ctx.getCoreWorkspaceProject())
199
+ // console.log('this.ctx', this)
190
200
try {
191
201
const { err, libraryConfig } = await getChannelPromise ( libraryConfigurationCh )
192
202
if ( ! err ) {
@@ -262,24 +272,25 @@ function getSortWrapper (sort) {
262
272
log . warn ( 'Could not send Dynamic Instrumentation configuration to workers.' )
263
273
}
264
274
}
265
-
266
- if ( isTestManagementTestsEnabled ) {
267
- const { err, testManagementTests : receivedTestManagementTests } = await getChannelPromise ( testManagementTestsCh )
268
- if ( ! err ) {
269
- testManagementTests = receivedTestManagementTests
270
- try {
271
- const workspaceProject = this . ctx . getCoreWorkspaceProject ( )
272
- workspaceProject . _provided . _ddIsTestManagementTestsEnabled = isTestManagementTestsEnabled
273
- workspaceProject . _provided . _ddTestManagementAttemptToFixRetries = testManagementAttemptToFixRetries
274
- workspaceProject . _provided . _ddTestManagementTests = testManagementTests
275
- } catch {
276
- log . warn ( 'Could not send test management tests to workers so Test Management will not work.' )
277
- }
278
- } else {
279
- isTestManagementTestsEnabled = false
280
- log . error ( 'Could not get test management tests.' )
281
- }
282
- }
275
+ debugger
276
+
277
+ // if (isTestManagementTestsEnabled) {
278
+ // const { err, testManagementTests: receivedTestManagementTests } = await getChannelPromise(testManagementTestsCh)
279
+ // if (!err) {
280
+ // testManagementTests = receivedTestManagementTests
281
+ // try {
282
+ // const workspaceProject = this.ctx.getCoreWorkspaceProject()
283
+ // workspaceProject._provided._ddIsTestManagementTestsEnabled = isTestManagementTestsEnabled
284
+ // workspaceProject._provided._ddTestManagementAttemptToFixRetries = testManagementAttemptToFixRetries
285
+ // workspaceProject._provided._ddTestManagementTests = testManagementTests
286
+ // } catch {
287
+ // log.warn('Could not send test management tests to workers so Test Management will not work.')
288
+ // }
289
+ // } else {
290
+ // isTestManagementTestsEnabled = false
291
+ // log.error('Could not get test management tests.')
292
+ // }
293
+ // }
283
294
284
295
let testCodeCoverageLinesTotal
285
296
@@ -344,6 +355,47 @@ function getCreateCliWrapper (vitestPackage, frameworkVersion) {
344
355
return vitestPackage
345
356
}
346
357
358
+
359
+ // UNUSED RIGHT NOW, but we can use it to bind the async resource to the test fn
360
+ // getFn is what's used to get the test fn to run it with vitest:
361
+ // https://github.com/vitest-dev/vitest/blob/0cbad1b0d0d56f1ec60f8496678d1435f8bb8977/packages/runner/src/run.ts#L315-L321
362
+ let getFn = null
363
+
364
+ // run in workers only
365
+ addHook ( {
366
+ name : '@vitest/runner' ,
367
+ versions : [ '>=1.6.0' ] ,
368
+ file : 'dist/index.js'
369
+ } , ( suitePackage ) => {
370
+ getFn = suitePackage . getFn
371
+
372
+ return suitePackage
373
+ } )
374
+
375
+ addHook ( {
376
+ name : 'tinypool' ,
377
+ versions : [ '>=1.0.0' ] ,
378
+ file : 'dist/index.js'
379
+ } , ( TinyPool ) => {
380
+ debugger
381
+ // we can pass handle here to the worker, and then use it to send messages to the main process
382
+ shimmer . wrap ( TinyPool . prototype , 'run' , run => function ( _ , { channel } ) {
383
+ const res = run . apply ( this , arguments )
384
+
385
+ this . threads . forEach ( thread => {
386
+ thread . process . on ( 'message' , ( message ) => {
387
+ if ( message . __tinypool_worker_message__ && message . data ) {
388
+ workerReporterCh . publish ( message . data )
389
+ }
390
+ } )
391
+ } )
392
+
393
+ return res
394
+ } )
395
+
396
+ return TinyPool
397
+ } )
398
+
347
399
addHook ( {
348
400
name : 'vitest' ,
349
401
versions : [ '>=1.6.0' ] ,
@@ -354,6 +406,7 @@ addHook({
354
406
// `onBeforeRunTask` is run before any repetition or attempt is run
355
407
// `onBeforeRunTask` is an async function
356
408
shimmer . wrap ( VitestTestRunner . prototype , 'onBeforeRunTask' , onBeforeRunTask => function ( task ) {
409
+ // console.log('on before run task', process.env)
357
410
const testName = getTestName ( task )
358
411
359
412
const {
@@ -420,6 +473,7 @@ addHook({
420
473
// `onAfterRunTask` is run after all repetitions or attempts are run
421
474
// `onAfterRunTask` is an async function
422
475
shimmer . wrap ( VitestTestRunner . prototype , 'onAfterRunTask' , onAfterRunTask => function ( task ) {
476
+ // console.log('task', task)
423
477
const { isEarlyFlakeDetectionEnabled, isTestManagementTestsEnabled } = getProvidedContext ( )
424
478
425
479
if ( isTestManagementTestsEnabled ) {
@@ -819,6 +873,7 @@ addHook({
819
873
820
874
testSuiteFinishCh . publish ( { status : testSuiteResult . state , onFinish, ...testSuiteCtx . currentStore } )
821
875
876
+ // console.log('test usite finish!')
822
877
// TODO: fix too frequent flushes
823
878
await onFinishPromise
824
879
0 commit comments