modestbench 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/adapters/jest-adapter.cjs +496 -0
  3. package/dist/adapters/jest-adapter.cjs.map +1 -0
  4. package/dist/adapters/jest-adapter.d.cts +42 -0
  5. package/dist/adapters/jest-adapter.d.cts.map +1 -0
  6. package/dist/adapters/jest-adapter.d.ts +42 -0
  7. package/dist/adapters/jest-adapter.d.ts.map +1 -0
  8. package/dist/adapters/jest-adapter.js +459 -0
  9. package/dist/adapters/jest-adapter.js.map +1 -0
  10. package/dist/adapters/jest-hooks.cjs +83 -0
  11. package/dist/adapters/jest-hooks.cjs.map +1 -0
  12. package/dist/adapters/jest-hooks.d.cts +24 -0
  13. package/dist/adapters/jest-hooks.d.cts.map +1 -0
  14. package/dist/adapters/jest-hooks.d.ts +24 -0
  15. package/dist/adapters/jest-hooks.d.ts.map +1 -0
  16. package/dist/adapters/jest-hooks.js +78 -0
  17. package/dist/adapters/jest-hooks.js.map +1 -0
  18. package/dist/adapters/jest-register.cjs +17 -0
  19. package/dist/adapters/jest-register.cjs.map +1 -0
  20. package/dist/adapters/jest-register.d.cts +12 -0
  21. package/dist/adapters/jest-register.d.cts.map +1 -0
  22. package/dist/adapters/jest-register.d.ts +12 -0
  23. package/dist/adapters/jest-register.d.ts.map +1 -0
  24. package/dist/adapters/jest-register.js +15 -0
  25. package/dist/adapters/jest-register.js.map +1 -0
  26. package/dist/adapters/types.cjs.map +1 -1
  27. package/dist/adapters/types.d.cts +5 -1
  28. package/dist/adapters/types.d.cts.map +1 -1
  29. package/dist/adapters/types.d.ts +5 -1
  30. package/dist/adapters/types.d.ts.map +1 -1
  31. package/dist/adapters/types.js.map +1 -1
  32. package/dist/cli/commands/test.cjs +3 -0
  33. package/dist/cli/commands/test.cjs.map +1 -1
  34. package/dist/cli/commands/test.d.cts.map +1 -1
  35. package/dist/cli/commands/test.d.ts.map +1 -1
  36. package/dist/cli/commands/test.js +3 -0
  37. package/dist/cli/commands/test.js.map +1 -1
  38. package/dist/cli/index.cjs +2 -2
  39. package/dist/cli/index.cjs.map +1 -1
  40. package/dist/cli/index.js +2 -2
  41. package/dist/cli/index.js.map +1 -1
  42. package/package.json +26 -6
  43. package/src/adapters/jest-adapter.ts +563 -0
  44. package/src/adapters/jest-hooks.ts +82 -0
  45. package/src/adapters/jest-register.ts +16 -0
  46. package/src/adapters/types.ts +5 -1
  47. package/src/cli/commands/test.ts +3 -0
  48. package/src/cli/index.ts +2 -2
@@ -0,0 +1,563 @@
1
+ /**
2
+ * ModestBench Jest Adapter
3
+ *
4
+ * Captures test definitions from Jest test files using ES module loader hooks.
5
+ *
6
+ * Jest differs from AVA and node:test in important ways:
7
+ *
8
+ * - Nested describe blocks: describe() callbacks execute IMMEDIATELY during file
9
+ * load to discover nested tests. We must run describe callbacks but NOT test
10
+ * functions.
11
+ * - Rich API surface: .skip, .only, .each, .todo, .concurrent, .failing modifiers
12
+ * - Jest object: Tests may access jest.fn(), jest.mock(), etc.
13
+ * - Expect assertions: Tests use expect() for assertions
14
+ *
15
+ * Architecture:
16
+ *
17
+ * 1. Install mock on globalThis
18
+ * 2. Loader intercepts '@jest/globals' imports and returns the mock
19
+ * 3. Import test file - describe callbacks execute, test functions are captured
20
+ * 4. Return captured test structure
21
+ */
22
+
23
+ import { pathToFileURL } from 'node:url';
24
+
25
+ import type {
26
+ CapturedSuite,
27
+ CapturedTest,
28
+ CapturedTestFile,
29
+ SuiteHooks,
30
+ TestFrameworkAdapter,
31
+ } from './types.js';
32
+
33
+ /**
34
+ * Global capture state key
35
+ */
36
+ const CAPTURE_STATE_KEY = '__MODESTBENCH_JEST_CAPTURE__';
37
+
38
+ /**
39
+ * Internal capture state structure
40
+ *
41
+ * Uses a stack to track nested describe blocks
42
+ */
43
+ interface CaptureState {
44
+ rootSuite: MutableCapturedSuite;
45
+ suiteStack: MutableCapturedSuite[];
46
+ }
47
+
48
+ /**
49
+ * Jest's each table type for parameterized tests
50
+ */
51
+ type EachTable = ReadonlyArray<readonly unknown[]> | readonly unknown[];
52
+
53
+ /**
54
+ * Jest's test function type
55
+ */
56
+ type JestTestFn = () => Promise<void> | void;
57
+
58
+ /**
59
+ * Internal captured suite with Jest-specific data
60
+ */
61
+ interface MutableCapturedSuite {
62
+ children: MutableCapturedSuite[];
63
+ hooks: {
64
+ afterAll: Array<{ fn: JestTestFn; timeout?: number }>;
65
+ afterEach: Array<{ fn: JestTestFn; timeout?: number }>;
66
+ beforeAll: Array<{ fn: JestTestFn; timeout?: number }>;
67
+ beforeEach: Array<{ fn: JestTestFn; timeout?: number }>;
68
+ };
69
+ name: string;
70
+ only?: boolean;
71
+ skip?: boolean;
72
+ tests: MutableCapturedTest[];
73
+ }
74
+
75
+ /**
76
+ * Internal captured test with Jest-specific flags
77
+ */
78
+ interface MutableCapturedTest {
79
+ fn: JestTestFn;
80
+ name: string;
81
+ only?: boolean;
82
+ skip?: boolean;
83
+ timeout?: number;
84
+ }
85
+
86
+ /**
87
+ * Get the current suite from the stack
88
+ */
89
+ const getCurrentSuite = (state: CaptureState): MutableCapturedSuite => {
90
+ return state.suiteStack[state.suiteStack.length - 1] ?? state.rootSuite;
91
+ };
92
+
93
+ /**
94
+ * Jest test framework adapter
95
+ *
96
+ * Captures test definitions by installing mock implementations before importing
97
+ * the test file.
98
+ *
99
+ * IMPORTANT: For this to work with Jest imports, you must run Node.js with our
100
+ * loader: node --import modestbench/jest your-test.js
101
+ */
102
+ export class JestAdapter implements TestFrameworkAdapter {
103
+ readonly framework = 'jest' as const;
104
+
105
+ /**
106
+ * Capture test definitions from a Jest test file
107
+ *
108
+ * @param filePath - Absolute path to the test file
109
+ * @returns Captured test structure
110
+ */
111
+ async capture(filePath: string): Promise<CapturedTestFile> {
112
+ // Initialize capture state
113
+ const state = initCaptureState();
114
+
115
+ // Install mocks
116
+ installJestMocks(state);
117
+
118
+ try {
119
+ // Import the test file
120
+ // The loader hook will intercept '@jest/globals' and use our mocks
121
+ // Describe callbacks will execute immediately, capturing the test structure
122
+ const fileUrl = pathToFileURL(filePath).href;
123
+ const bustCache = `?t=${Date.now()}`;
124
+ await import(fileUrl + bustCache);
125
+
126
+ // Return captured structure
127
+ return toCapturedTestFile(state, filePath);
128
+ } finally {
129
+ // Clean up
130
+ uninstallJestMocks();
131
+ clearCaptureState();
132
+ }
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Clear capture state from globalThis
138
+ */
139
+ const clearCaptureState = (): void => {
140
+ // @ts-expect-error - intentionally using globalThis
141
+ delete globalThis[CAPTURE_STATE_KEY];
142
+ };
143
+
144
+ /**
145
+ * Create a mock expect function
146
+ *
147
+ * Jest's expect() returns an object with assertion methods. We provide a stub
148
+ * that allows tests to call expect() without errors during structure capture.
149
+ * Since we capture but don't execute test functions, this is mainly for
150
+ * safety.
151
+ */
152
+ const createMockExpect = (): ((value: unknown) => unknown) => {
153
+ const noop = () => mockExpect;
154
+ const noopAsync = () => Promise.resolve(mockExpect);
155
+
156
+ // Chainable assertion mock
157
+ const assertionChain: Record<string, unknown> = {
158
+ not: {} as Record<string, unknown>,
159
+ rejects: {} as Record<string, unknown>,
160
+ resolves: {} as Record<string, unknown>,
161
+ };
162
+
163
+ // Common matchers - all return the chain for further chaining
164
+ const matchers = [
165
+ 'toBe',
166
+ 'toEqual',
167
+ 'toStrictEqual',
168
+ 'toBeNull',
169
+ 'toBeUndefined',
170
+ 'toBeDefined',
171
+ 'toBeTruthy',
172
+ 'toBeFalsy',
173
+ 'toBeNaN',
174
+ 'toContain',
175
+ 'toContainEqual',
176
+ 'toHaveLength',
177
+ 'toHaveProperty',
178
+ 'toMatch',
179
+ 'toMatchObject',
180
+ 'toMatchSnapshot',
181
+ 'toMatchInlineSnapshot',
182
+ 'toThrow',
183
+ 'toThrowError',
184
+ 'toThrowErrorMatchingSnapshot',
185
+ 'toThrowErrorMatchingInlineSnapshot',
186
+ 'toBeGreaterThan',
187
+ 'toBeGreaterThanOrEqual',
188
+ 'toBeLessThan',
189
+ 'toBeLessThanOrEqual',
190
+ 'toBeCloseTo',
191
+ 'toBeInstanceOf',
192
+ 'toHaveBeenCalled',
193
+ 'toHaveBeenCalledTimes',
194
+ 'toHaveBeenCalledWith',
195
+ 'toHaveBeenLastCalledWith',
196
+ 'toHaveBeenNthCalledWith',
197
+ 'toHaveReturned',
198
+ 'toHaveReturnedTimes',
199
+ 'toHaveReturnedWith',
200
+ 'toHaveLastReturnedWith',
201
+ 'toHaveNthReturnedWith',
202
+ ];
203
+
204
+ for (const matcher of matchers) {
205
+ assertionChain[matcher] = noop;
206
+ (assertionChain.not as Record<string, unknown>)[matcher] = noop;
207
+ (assertionChain.resolves as Record<string, unknown>)[matcher] = noopAsync;
208
+ (assertionChain.rejects as Record<string, unknown>)[matcher] = noopAsync;
209
+ }
210
+
211
+ const mockExpect = (_value: unknown) => assertionChain;
212
+
213
+ // Static expect methods
214
+ Object.assign(mockExpect, {
215
+ addSnapshotSerializer: noop,
216
+ any: noop,
217
+ anything: noop,
218
+ arrayContaining: noop,
219
+ assertions: noop,
220
+ closeTo: noop,
221
+ extend: noop,
222
+ getState: () => ({}),
223
+ hasAssertions: noop,
224
+ not: {
225
+ arrayContaining: noop,
226
+ objectContaining: noop,
227
+ stringContaining: noop,
228
+ stringMatching: noop,
229
+ },
230
+ objectContaining: noop,
231
+ setState: noop,
232
+ stringContaining: noop,
233
+ stringMatching: noop,
234
+ });
235
+
236
+ return mockExpect;
237
+ };
238
+
239
+ /**
240
+ * Create a mock jest object
241
+ *
242
+ * Provides stub implementations of common jest.* utilities that tests might
243
+ * call during structure capture. Since we don't execute test functions, most of
244
+ * these won't be called, but they're here for safety.
245
+ */
246
+ const createMockJestObject = (): Record<string, unknown> => {
247
+ const noop = () => {};
248
+ const noopReturnsThis = function (this: unknown) {
249
+ return this;
250
+ };
251
+
252
+ // Mock function factory
253
+ const mockFn = (implementation?: unknown) => {
254
+ const fn = typeof implementation === 'function' ? implementation : noop;
255
+ return Object.assign(fn, {
256
+ mock: { calls: [], instances: [], results: [] },
257
+ mockClear: noopReturnsThis,
258
+ mockImplementation: noopReturnsThis,
259
+ mockImplementationOnce: noopReturnsThis,
260
+ mockName: noopReturnsThis,
261
+ mockRejectedValue: noopReturnsThis,
262
+ mockRejectedValueOnce: noopReturnsThis,
263
+ mockReset: noopReturnsThis,
264
+ mockResolvedValue: noopReturnsThis,
265
+ mockResolvedValueOnce: noopReturnsThis,
266
+ mockRestore: noopReturnsThis,
267
+ mockReturnThis: noopReturnsThis,
268
+ mockReturnValue: noopReturnsThis,
269
+ mockReturnValueOnce: noopReturnsThis,
270
+ });
271
+ };
272
+
273
+ return {
274
+ advanceTimersByTime: noop,
275
+ advanceTimersByTimeAsync: () => Promise.resolve(),
276
+ advanceTimersToNextTimer: noop,
277
+ advanceTimersToNextTimerAsync: () => Promise.resolve(),
278
+ autoMockOff: noopReturnsThis,
279
+ autoMockOn: noopReturnsThis,
280
+ clearAllMocks: noop,
281
+ clearAllTimers: noop,
282
+ createMockFromModule: () => ({}),
283
+ deepUnmock: noopReturnsThis,
284
+ disableAutomock: noopReturnsThis,
285
+ doMock: noop,
286
+ dontMock: noopReturnsThis,
287
+ enableAutomock: noopReturnsThis,
288
+ fn: mockFn,
289
+ genMockFromModule: () => ({}),
290
+ getRealSystemTime: () => Date.now(),
291
+ getSeed: () => 0,
292
+ getTimerCount: () => 0,
293
+ isEnvironmentTornDown: () => false,
294
+ isMockFunction: () => false,
295
+ isolateModules: noop,
296
+ isolateModulesAsync: () => Promise.resolve(),
297
+ mock: noop,
298
+ mocked: (source: unknown) => source,
299
+ now: () => Date.now(),
300
+ replaceProperty: noopReturnsThis,
301
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-return -- Mimics Jest's requireActual API
302
+ requireActual: (moduleName: string) => require(moduleName),
303
+ requireMock: () => ({}),
304
+ resetAllMocks: noop,
305
+ resetModules: noopReturnsThis,
306
+ restoreAllMocks: noop,
307
+ retryTimes: noopReturnsThis,
308
+ runAllImmediates: noop,
309
+ runAllTicks: noop,
310
+ runAllTimers: noop,
311
+ runAllTimersAsync: () => Promise.resolve(),
312
+ runOnlyPendingTimers: noop,
313
+ runOnlyPendingTimersAsync: () => Promise.resolve(),
314
+ setMock: noopReturnsThis,
315
+ setSystemTime: noop,
316
+ setTimeout: noopReturnsThis,
317
+ spyOn: () => mockFn(),
318
+ unmock: noopReturnsThis,
319
+ unstable_mockModule: noop,
320
+ useFakeTimers: noopReturnsThis,
321
+ useRealTimers: noopReturnsThis,
322
+ };
323
+ };
324
+
325
+ /**
326
+ * Initialize capture state on globalThis
327
+ */
328
+ const initCaptureState = (): CaptureState => {
329
+ const rootSuite: MutableCapturedSuite = {
330
+ children: [],
331
+ hooks: {
332
+ afterAll: [],
333
+ afterEach: [],
334
+ beforeAll: [],
335
+ beforeEach: [],
336
+ },
337
+ name: '',
338
+ tests: [],
339
+ };
340
+
341
+ const state: CaptureState = {
342
+ rootSuite,
343
+ suiteStack: [rootSuite],
344
+ };
345
+
346
+ // @ts-expect-error - intentionally using globalThis for cross-module state
347
+ globalThis[CAPTURE_STATE_KEY] = state;
348
+
349
+ return state;
350
+ };
351
+
352
+ /**
353
+ * Install Jest mocks on globalThis for module interception
354
+ */
355
+ const installJestMocks = (state: CaptureState): void => {
356
+ const mockExpect = createMockExpect();
357
+ const mockJest = createMockJestObject();
358
+
359
+ /**
360
+ * Create a describe function implementation
361
+ */
362
+ const makeDescribeFn =
363
+ (options: { only?: boolean; skip?: boolean } = {}) =>
364
+ (name: string, fn: () => void): void => {
365
+ const suite: MutableCapturedSuite = {
366
+ children: [],
367
+ hooks: {
368
+ afterAll: [],
369
+ afterEach: [],
370
+ beforeAll: [],
371
+ beforeEach: [],
372
+ },
373
+ name,
374
+ only: options.only,
375
+ skip: options.skip,
376
+ tests: [],
377
+ };
378
+
379
+ const currentSuite = getCurrentSuite(state);
380
+ currentSuite.children.push(suite);
381
+
382
+ // Push onto stack, execute callback to discover nested tests, then pop
383
+ state.suiteStack.push(suite);
384
+ try {
385
+ fn(); // Execute describe callback to discover nested content
386
+ } finally {
387
+ state.suiteStack.pop();
388
+ }
389
+ };
390
+
391
+ /**
392
+ * Create a test function implementation
393
+ */
394
+ const makeTestFn =
395
+ (options: { only?: boolean; skip?: boolean } = {}) =>
396
+ (name: string, fn?: JestTestFn, timeout?: number): void => {
397
+ const currentSuite = getCurrentSuite(state);
398
+ currentSuite.tests.push({
399
+ fn: fn ?? (() => {}),
400
+ name,
401
+ only: options.only,
402
+ skip: options.skip,
403
+ timeout,
404
+ });
405
+ };
406
+
407
+ // Create base describe function with modifiers
408
+ const describeFn = makeDescribeFn();
409
+ const describeOnly = makeDescribeFn({ only: true });
410
+ const describeSkip = makeDescribeFn({ skip: true });
411
+
412
+ const mockDescribe = Object.assign(describeFn, {
413
+ each:
414
+ (table: EachTable) =>
415
+ (nameTemplate: string, fn: (...args: unknown[]) => void): void => {
416
+ // Handle both array of arrays and template literal tagged syntax
417
+ const rows = Array.isArray(table[0]) ? table : [table];
418
+ for (const row of rows as readonly unknown[][]) {
419
+ // Simple template substitution for %s, %i, %d, %p, %j, etc.
420
+ let name = nameTemplate;
421
+ for (let i = 0; i < row.length; i++) {
422
+ name = name.replace(/%[sidpjfo#]/i, String(row[i]));
423
+ }
424
+ describeFn(name, () => fn(...row));
425
+ }
426
+ },
427
+ only: describeOnly,
428
+ skip: describeSkip,
429
+ });
430
+
431
+ // Create base test function with modifiers
432
+ const testFn = makeTestFn();
433
+ const testOnly = makeTestFn({ only: true });
434
+ const testSkip = makeTestFn({ skip: true });
435
+
436
+ const mockTest = Object.assign(testFn, {
437
+ concurrent: (name: string, fn: JestTestFn, timeout?: number): void => {
438
+ // Treat concurrent tests as regular tests for benchmarking
439
+ testFn(name, fn, timeout);
440
+ },
441
+ each:
442
+ (table: EachTable) =>
443
+ (
444
+ nameTemplate: string,
445
+ fn: (...args: unknown[]) => Promise<void> | void,
446
+ timeout?: number,
447
+ ): void => {
448
+ const rows = Array.isArray(table[0]) ? table : [table];
449
+ for (const row of rows as readonly unknown[][]) {
450
+ let name = nameTemplate;
451
+ for (let i = 0; i < row.length; i++) {
452
+ name = name.replace(/%[sidpjfo#]/i, String(row[i]));
453
+ }
454
+ testFn(name, () => fn(...row), timeout);
455
+ }
456
+ },
457
+ failing: (name: string, fn: JestTestFn, timeout?: number): void => {
458
+ // Treat failing tests as regular tests for benchmarking
459
+ testFn(name, fn, timeout);
460
+ },
461
+ only: testOnly,
462
+ skip: testSkip,
463
+ todo: (name: string): void => {
464
+ const currentSuite = getCurrentSuite(state);
465
+ currentSuite.tests.push({
466
+ fn: () => {},
467
+ name,
468
+ skip: true,
469
+ });
470
+ },
471
+ });
472
+
473
+ // Hook registration helpers
474
+ const createHook =
475
+ (hookType: 'afterAll' | 'afterEach' | 'beforeAll' | 'beforeEach') =>
476
+ (fn: JestTestFn, timeout?: number): void => {
477
+ const currentSuite = getCurrentSuite(state);
478
+ currentSuite.hooks[hookType].push({ fn, timeout });
479
+ };
480
+
481
+ // Install on globalThis for the loader to access
482
+ // @ts-expect-error - intentionally using globalThis
483
+ globalThis.__MODESTBENCH_JEST_MOCK__ = {
484
+ afterAll: createHook('afterAll'),
485
+ afterEach: createHook('afterEach'),
486
+ beforeAll: createHook('beforeAll'),
487
+ beforeEach: createHook('beforeEach'),
488
+ describe: mockDescribe,
489
+ expect: mockExpect,
490
+ fdescribe: mockDescribe.only,
491
+ fit: mockTest.only,
492
+ it: mockTest,
493
+ jest: mockJest,
494
+ test: mockTest,
495
+ xdescribe: mockDescribe.skip,
496
+ xit: mockTest.skip,
497
+ xtest: mockTest.skip,
498
+ };
499
+ };
500
+
501
+ /**
502
+ * Convert a mutable suite to the captured format
503
+ */
504
+ const convertSuite = (suite: MutableCapturedSuite): CapturedSuite => {
505
+ const hooks: SuiteHooks = {
506
+ after: suite.hooks.afterAll.map((h) => h.fn),
507
+ afterEach: suite.hooks.afterEach.map((h) => h.fn),
508
+ before: suite.hooks.beforeAll.map((h) => h.fn),
509
+ beforeEach: suite.hooks.beforeEach.map((h) => h.fn),
510
+ };
511
+
512
+ const tests: CapturedTest[] = suite.tests.map((t) => ({
513
+ fn: t.fn,
514
+ name: t.name,
515
+ only: t.only,
516
+ skip: t.skip,
517
+ }));
518
+
519
+ return {
520
+ children: suite.children.map(convertSuite),
521
+ hooks,
522
+ name: suite.name,
523
+ only: suite.only,
524
+ skip: suite.skip,
525
+ tests,
526
+ };
527
+ };
528
+
529
+ /**
530
+ * Convert capture state to CapturedTestFile
531
+ */
532
+ const toCapturedTestFile = (
533
+ state: CaptureState,
534
+ filePath: string,
535
+ ): CapturedTestFile => {
536
+ const root = state.rootSuite;
537
+
538
+ // Root-level tests (not in any describe block)
539
+ const rootTests: CapturedTest[] = root.tests.map((t) => ({
540
+ fn: t.fn,
541
+ name: t.name,
542
+ only: t.only,
543
+ skip: t.skip,
544
+ }));
545
+
546
+ // Convert child suites
547
+ const rootSuites = root.children.map(convertSuite);
548
+
549
+ return {
550
+ filePath,
551
+ framework: 'jest',
552
+ rootSuites,
553
+ rootTests,
554
+ };
555
+ };
556
+
557
+ /**
558
+ * Uninstall Jest mocks
559
+ */
560
+ const uninstallJestMocks = (): void => {
561
+ // @ts-expect-error - cleaning up globalThis
562
+ delete globalThis.__MODESTBENCH_JEST_MOCK__;
563
+ };
@@ -0,0 +1,82 @@
1
+ /**
2
+ * ModestBench Jest Loader Hooks
3
+ *
4
+ * ES module loader hooks that intercept `@jest/globals` imports and return our
5
+ * capturing mock from globalThis.
6
+ *
7
+ * Usage: node --import modestbench/jest test-file.js
8
+ *
9
+ * This loader exports async `resolve` and `load` hooks that get registered via
10
+ * module.register() when imported through jest-register.ts.
11
+ */
12
+
13
+ import type { LoadHook, ResolveHook } from 'node:module';
14
+
15
+ /**
16
+ * Generate the mock module source code
17
+ *
18
+ * Uses top-level await to conditionally get mock or real module. Note: Uses
19
+ * '@jest/globals?passthrough' to bypass our hook when falling back.
20
+ *
21
+ * Security: The globalThis mock is only installed by our own adapter code, so
22
+ * the generated source is safe. No user input is interpolated into this
23
+ * template.
24
+ */
25
+ const generateMockSource = (): string => `
26
+ const mock = globalThis.__MODESTBENCH_JEST_MOCK__;
27
+
28
+ // If no mock installed, fall through to real @jest/globals
29
+ // The '?passthrough' query tells our hook to not intercept this import
30
+ const source = mock ?? await import('@jest/globals?passthrough');
31
+
32
+ export const describe = source.describe;
33
+ export const fdescribe = source.fdescribe ?? source.describe?.only;
34
+ export const xdescribe = source.xdescribe ?? source.describe?.skip;
35
+ export const test = source.test;
36
+ export const it = source.it ?? source.test;
37
+ export const fit = source.fit ?? source.test?.only;
38
+ export const xit = source.xit ?? source.test?.skip;
39
+ export const xtest = source.xtest ?? source.test?.skip;
40
+ export const expect = source.expect;
41
+ export const jest = source.jest;
42
+ export const beforeAll = source.beforeAll;
43
+ export const afterAll = source.afterAll;
44
+ export const beforeEach = source.beforeEach;
45
+ export const afterEach = source.afterEach;
46
+ export default source;
47
+ `;
48
+
49
+ /**
50
+ * Resolve hook - intercepts @jest/globals specifier
51
+ *
52
+ * Uses query param '?passthrough' to prevent infinite recursion when falling
53
+ * back to real @jest/globals (when no mock is installed).
54
+ */
55
+ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
56
+ // Only intercept bare '@jest/globals', not '@jest/globals?passthrough'
57
+ if (specifier === '@jest/globals') {
58
+ return {
59
+ shortCircuit: true,
60
+ url: 'modestbench://capture/jest',
61
+ };
62
+ }
63
+ // Strip passthrough query to resolve real @jest/globals
64
+ if (specifier === '@jest/globals?passthrough') {
65
+ return nextResolve('@jest/globals', context);
66
+ }
67
+ return nextResolve(specifier, context);
68
+ };
69
+
70
+ /**
71
+ * Load hook - returns mock module for our custom URL
72
+ */
73
+ export const load: LoadHook = async (url, context, nextLoad) => {
74
+ if (url === 'modestbench://capture/jest') {
75
+ return {
76
+ format: 'module',
77
+ shortCircuit: true,
78
+ source: generateMockSource(),
79
+ };
80
+ }
81
+ return nextLoad(url, context);
82
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * ModestBench Jest Loader Registration
3
+ *
4
+ * Registers the Jest ESM loader hooks via module.register().
5
+ *
6
+ * Usage: node --import modestbench/jest your-test.js
7
+ *
8
+ * This file registers the hooks module which intercepts '@jest/globals'
9
+ * imports.
10
+ */
11
+
12
+ import { register } from 'node:module';
13
+
14
+ register('./jest-hooks.js', {
15
+ parentURL: import.meta.url,
16
+ });
@@ -20,6 +20,10 @@ export interface CapturedSuite {
20
20
  readonly hooks: SuiteHooks;
21
21
  /** Suite name */
22
22
  readonly name: string;
23
+ /** Whether this suite is marked .only */
24
+ readonly only?: boolean;
25
+ /** Whether this suite is marked .skip */
26
+ readonly skip?: boolean;
23
27
  /** Tests in this suite */
24
28
  readonly tests: CapturedTest[];
25
29
  }
@@ -79,7 +83,7 @@ export interface SuiteHooks {
79
83
  /**
80
84
  * Supported test frameworks
81
85
  */
82
- export type TestFramework = 'ava' | 'mocha' | 'node-test';
86
+ export type TestFramework = 'ava' | 'jest' | 'mocha' | 'node-test';
83
87
 
84
88
  /**
85
89
  * Interface for test framework adapters
@@ -21,6 +21,7 @@ import type {
21
21
  import type { CliContext } from '../index.js';
22
22
 
23
23
  import { AvaAdapter } from '../../adapters/ava-adapter.js';
24
+ import { JestAdapter } from '../../adapters/jest-adapter.js';
24
25
  import { MochaAdapter } from '../../adapters/mocha-adapter.js';
25
26
  import { NodeTestAdapter } from '../../adapters/node-test-adapter.js';
26
27
  import {
@@ -512,6 +513,8 @@ const selectAdapter = (framework: TestFramework) => {
512
513
  switch (framework) {
513
514
  case 'ava':
514
515
  return new AvaAdapter();
516
+ case 'jest':
517
+ return new JestAdapter();
515
518
  case 'mocha':
516
519
  return new MochaAdapter();
517
520
  case 'node-test':
package/src/cli/index.ts CHANGED
@@ -993,11 +993,11 @@ export const main = async (
993
993
  )
994
994
  .command(
995
995
  'test <framework> [files..]',
996
- 'Run test files as benchmarks (captures tests from Mocha, node:test, or AVA)',
996
+ 'Run test files as benchmarks (captures tests from Jest, Mocha, node:test, or AVA)',
997
997
  (yargs) => {
998
998
  return yargs
999
999
  .positional('framework', {
1000
- choices: ['mocha', 'node-test', 'ava'] as const,
1000
+ choices: ['ava', 'jest', 'mocha', 'node-test'] as const,
1001
1001
  demandOption: true,
1002
1002
  describe: 'Test framework to use',
1003
1003
  nargs: 1,