Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 47 additions & 3 deletions packages/cli-kit/src/public/node/output.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import {
collectedLogs,
clearCollectedLogs,
LogLevel,
outputDebug,
outputWhereAppropriate,
outputToken,
shouldDisplayColors,
formatPackageManagerCommand,
} from './output.js'

import {currentProcessIsGlobal} from './is-global.js'
import {describe, expect, test, vi} from 'vitest'
import {beforeEach, describe, expect, test, vi} from 'vitest'
import {Writable} from 'stream'

const isVerboseMock = vi.hoisted(() => vi.fn(() => false))
const isUnitTestMock = vi.hoisted(() => vi.fn(() => false))

vi.mock('./context/local.js', async () => {
return {
isVerbose: () => false,
isUnitTest: () => false,
isVerbose: isVerboseMock,
isUnitTest: isUnitTestMock,
}
})
vi.mock('./is-global.js')

beforeEach(() => {
isVerboseMock.mockReturnValue(false)
isUnitTestMock.mockReturnValue(false)
clearCollectedLogs()
})

describe('Output helpers', () => {
test('can format dependency manager commands with flags', () => {
expect(outputToken.packagejsonScript('yarn', 'dev', '--reset').value).toEqual('yarn dev --reset')
Expand Down Expand Up @@ -87,6 +99,38 @@ describe('outputWhereAppropriate', () => {
})
})

describe('outputDebug', () => {
test('collects debug logs during unit tests', () => {
// Given
const logger = vi.fn()
isUnitTestMock.mockReturnValue(true)

// When
outputDebug('debug message', logger)

// Then
expect(collectedLogs.debug).toEqual(['debug message'])
expect(logger).not.toHaveBeenCalled()
})

test('skips timestamp and logger work when debug output is disabled', () => {
// Given
const logger = vi.fn()
const toISOStringSpy = vi.spyOn(Date.prototype, 'toISOString')

try {
// When
outputDebug('debug message', logger)

// Then
expect(toISOStringSpy).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
} finally {
toISOStringSpy.mockRestore()
}
})
})

describe('formatPackageManagerCommand', () => {
test('can format yarn commands', () => {
// Given
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-kit/src/public/node/output.ts
Comment thread
gonzaloriestra marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ export function outputCompleted(content: OutputMessage, logger: Logger = console
*/
export function outputDebug(content: OutputMessage, logger: Logger = consoleWarn): void {
if (isUnitTest()) collectLog('debug', content)
if (!shouldOutput('debug')) return

const message = colors.gray(stringifyMessage(content))
outputWhereAppropriate('debug', logger, `${new Date().toISOString()}: ${message}`)
}
Expand Down
Loading