|
1 | | -import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { devtools } from '../src/plugin' |
3 | 3 | import type { Plugin } from 'vite' |
| 4 | +import type * as Utils from '../src/utils' |
| 5 | + |
| 6 | +let capturedOnConsolePipe: ((entries: Array<any>) => void) | undefined |
| 7 | + |
| 8 | +vi.mock('../src/utils', async (importOriginal) => { |
| 9 | + const actual = await importOriginal<typeof Utils>() |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + handleDevToolsViteRequest: vi.fn((_req: any, _res: any, _next: any, handlers: any) => { |
| 13 | + if (typeof handlers === 'object' && handlers?.onConsolePipe) { |
| 14 | + capturedOnConsolePipe = handlers.onConsolePipe |
| 15 | + } |
| 16 | + }), |
| 17 | + } |
| 18 | +}) |
4 | 19 |
|
5 | 20 | // Helper to find a plugin by name from the array returned by devtools() |
6 | 21 | function findPlugin(plugins: Array<Plugin>, name: string): Plugin | undefined { |
@@ -313,6 +328,72 @@ describe('devtools plugin', () => { |
313 | 328 | }) |
314 | 329 | }) |
315 | 330 |
|
| 331 | + describe('configureServer - onConsolePipe uses pre-wrap console methods', () => { |
| 332 | + let originalLog: typeof console.log |
| 333 | + let beforeWrapSpy: ReturnType<typeof vi.fn> |
| 334 | + let afterWrapSpy: ReturnType<typeof vi.fn> |
| 335 | + |
| 336 | + beforeEach(async () => { |
| 337 | + capturedOnConsolePipe = undefined |
| 338 | + originalLog = console.log |
| 339 | + beforeWrapSpy = vi.fn() |
| 340 | + afterWrapSpy = vi.fn() |
| 341 | + |
| 342 | + console.log = beforeWrapSpy |
| 343 | + |
| 344 | + const plugins = devtools({ eventBusConfig: { enabled: false } }) |
| 345 | + const customServerPlugin = findPlugin( |
| 346 | + plugins, |
| 347 | + '@tanstack/devtools:custom-server', |
| 348 | + )! |
| 349 | + |
| 350 | + const server = { |
| 351 | + ...createMockServer(), |
| 352 | + middlewares: { |
| 353 | + use: vi.fn((handler: any) => { |
| 354 | + handler({ url: '/not-a-tsd-route', socket: {} }, {}, vi.fn()) |
| 355 | + }), |
| 356 | + }, |
| 357 | + } |
| 358 | + |
| 359 | + const configureServer = customServerPlugin.configureServer as ( |
| 360 | + server: any, |
| 361 | + ) => Promise<void> |
| 362 | + await configureServer(server) |
| 363 | + |
| 364 | + console.log = afterWrapSpy |
| 365 | + }) |
| 366 | + |
| 367 | + afterEach(() => { |
| 368 | + console.log = originalLog |
| 369 | + capturedOnConsolePipe = undefined |
| 370 | + }) |
| 371 | + |
| 372 | + it('uses the captured original method directly for a known level', () => { |
| 373 | + expect(capturedOnConsolePipe).toBeDefined() |
| 374 | + |
| 375 | + capturedOnConsolePipe!([ |
| 376 | + { level: 'log', args: ['test message'], timestamp: Date.now() }, |
| 377 | + ]) |
| 378 | + |
| 379 | + expect(beforeWrapSpy).toHaveBeenCalled() |
| 380 | + expect(afterWrapSpy).not.toHaveBeenCalled() |
| 381 | + }) |
| 382 | + |
| 383 | + it('falls back to captured originalConsole.log for an unknown level, not the live console.log', () => { |
| 384 | + expect(capturedOnConsolePipe).toBeDefined() |
| 385 | + |
| 386 | + // 'trace' is not in consolePipingLevels so originalConsole['trace'] is |
| 387 | + // undefined and the fallback branch is taken. |
| 388 | + capturedOnConsolePipe!([ |
| 389 | + { level: 'trace', args: ['test message'], timestamp: Date.now() }, |
| 390 | + ]) |
| 391 | + |
| 392 | + expect(beforeWrapSpy).toHaveBeenCalled() |
| 393 | + expect(afterWrapSpy).not.toHaveBeenCalled() |
| 394 | + }) |
| 395 | + }) |
| 396 | + |
316 | 397 | describe('plugin array', () => { |
317 | 398 | it('should return an array of plugins', () => { |
318 | 399 | const plugins = devtools() |
|
0 commit comments