11// eslint-disable-next-line unicorn/prefer-node-protocol -- mocked
22import mockFs from 'fs' ;
3+ import { text as mockText } from 'node:stream/consumers' ;
34
45import findUpMock from 'find-up' ;
5- import mockGetStdin from 'get-stdin' ;
66import { glob as globMock } from 'glob' ;
77import getLogger from 'loglevel-colored-level-prefix' ;
88
@@ -14,13 +14,20 @@ jest.mock('fs');
1414// !NOTE: this is a workaround to also mock `node:fs`
1515jest . mock ( 'node:fs' , ( ) => mockFs ) ;
1616
17+ // Mock stream consumers
18+ jest . mock ( 'node:stream/consumers' , ( ) => ( {
19+ text : jest . fn ( ) ,
20+ } ) ) ;
21+
1722beforeEach ( ( ) => {
1823 process . stdout . write = jest . fn ( ) ;
24+ process . stdin . isTTY = undefined ;
1925 console . error = jest . fn ( ) ;
2026 console . log = jest . fn ( ) ;
2127 formatMock . mockClear ( ) ;
2228 mockFs . writeFile . mockClear ( ) ;
2329 mockFs . readFile . mockClear ( ) ;
30+ mockText . mockClear ( ) ;
2431} ) ;
2532
2633afterEach ( ( ) => {
@@ -66,11 +73,13 @@ test('glob call excludes an ignore of node_modules', async () => {
6673} ) ;
6774
6875test ( 'should accept stdin' , async ( ) => {
69- mockGetStdin . stdin = ' var [ foo, { bar } ] = window.APP ;' ;
76+ const stdinContent = ' var [ foo, { bar } ] = window.APP ;' ;
77+ mockText . mockResolvedValue ( stdinContent ) ;
78+
7079 await formatFiles ( { stdin : true } ) ;
7180 expect ( formatMock ) . toHaveBeenCalledTimes ( 1 ) ;
7281 // the trim is part of the test
73- const text = mockGetStdin . stdin . trim ( ) ;
82+ const text = stdinContent . trim ( ) ;
7483 expect ( formatMock ) . toHaveBeenCalledWith ( expect . objectContaining ( { text } ) ) ;
7584 expect ( process . stdout . write ) . toHaveBeenCalledTimes ( 1 ) ;
7685 expect ( process . stdout . write ) . toHaveBeenCalledWith ( 'MOCK_OUTPUT for stdin' ) ;
@@ -83,7 +92,7 @@ test('will write to files if that is specified', async () => {
8392} ) ;
8493
8594test ( 'handles stdin errors gracefully' , async ( ) => {
86- mockGetStdin . stdin = 'MOCK_SYNTAX_ERROR' ;
95+ mockText . mockResolvedValue ( 'MOCK_SYNTAX_ERROR' ) ;
8796 await formatFiles ( { stdin : true } ) ;
8897 expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
8998} ) ;
@@ -288,6 +297,54 @@ test('will not blow up if an .eslintignore or .prettierignore cannot be found',
288297 }
289298} ) ;
290299
300+ test ( 'should handle TTY stdin' , async ( ) => {
301+ const originalIsTTY = process . stdin . isTTY ;
302+ process . stdin . isTTY = true ;
303+
304+ try {
305+ await formatFiles ( { stdin : true } ) ;
306+
307+ expect ( mockText ) . not . toHaveBeenCalled ( ) ;
308+
309+ expect ( formatMock ) . toHaveBeenCalledTimes ( 1 ) ;
310+ expect ( formatMock ) . toHaveBeenCalledWith (
311+ expect . objectContaining ( { text : '' } ) ,
312+ ) ;
313+
314+ expect ( process . stdout . write ) . toHaveBeenCalledTimes ( 1 ) ;
315+ expect ( process . stdout . write ) . toHaveBeenCalledWith ( 'MOCK_OUTPUT for stdin' ) ;
316+ } finally {
317+ process . stdin . isTTY = originalIsTTY ;
318+ }
319+ } ) ;
320+
321+ test ( 'should handle non-TTY stdin' , async ( ) => {
322+ const originalIsTTY = process . stdin . isTTY ;
323+ process . stdin . isTTY = false ;
324+
325+ const stdinContent = ' var [ foo, { bar } ] = window.APP ;' ;
326+ mockText . mockResolvedValue ( stdinContent ) ;
327+
328+ try {
329+ await formatFiles ( { stdin : true } ) ;
330+
331+ expect ( mockText ) . toHaveBeenCalledTimes ( 1 ) ;
332+ expect ( mockText ) . toHaveBeenCalledWith ( process . stdin ) ;
333+
334+ expect ( formatMock ) . toHaveBeenCalledTimes ( 1 ) ;
335+ expect ( formatMock ) . toHaveBeenCalledWith (
336+ expect . objectContaining ( {
337+ text : stdinContent . trim ( ) ,
338+ } ) ,
339+ ) ;
340+
341+ expect ( process . stdout . write ) . toHaveBeenCalledTimes ( 1 ) ;
342+ expect ( process . stdout . write ) . toHaveBeenCalledWith ( 'MOCK_OUTPUT for stdin' ) ;
343+ } finally {
344+ process . stdin . isTTY = originalIsTTY ;
345+ }
346+ } ) ;
347+
291348describe ( 'listDifferent' , ( ) => {
292349 test ( 'will list different files' , async ( ) => {
293350 await formatFiles ( {
0 commit comments