Skip to content

Commit 6bc3fa0

Browse files
committed
chore: update with test
1 parent 97b8c92 commit 6bc3fa0

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎packages/devtools-vite/src/plugin.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
254254
const prefix = chalk.cyan('[Client]')
255255
const logMethod =
256256
originalConsole[entry.level as ConsoleLevel] ??
257+
originalConsole.log ??
257258
console.log
258259

259260
const cleanedArgs = stripEnhancedLogPrefix(

‎packages/devtools-vite/tests/index.test.ts‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { devtools } from '../src/plugin'
33
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+
})
419

520
// Helper to find a plugin by name from the array returned by devtools()
621
function findPlugin(plugins: Array<Plugin>, name: string): Plugin | undefined {
@@ -313,6 +328,72 @@ describe('devtools plugin', () => {
313328
})
314329
})
315330

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+
316397
describe('plugin array', () => {
317398
it('should return an array of plugins', () => {
318399
const plugins = devtools()

0 commit comments

Comments
 (0)