UNPKG

code-tree-cli

Version:
43 lines (39 loc) 1.33 kB
// Import. import fs from 'fs'; import { buildIgnoreFilter, getAllFiles, askYesNo } from './cli'; import { jest } from '@jest/globals'; /** * Check if the function buildIgnoreFilter returns an ignore filter. */ describe('buildIgnoreFilter', () => { it('returns an ignore filter', async () => { jest.spyOn(fs, 'existsSync').mockReturnValue(false); const filter = await buildIgnoreFilter('.'); expect(typeof filter.ignores).toBe('function'); fs.existsSync.mockRestore(); }); }); /** * Check if the function getAllFiles returns an array of file paths. */ describe('getAllFiles', () => { it('returns an array of file paths', async () => { const filter = { ignores: jest.fn().mockReturnValue(false) }; const files = await getAllFiles(process.cwd(), filter, '**/*'); expect(Array.isArray(files)).toBe(true); }); }); /** * Check if the function askYesNo resolves to true when user inputs y. */ describe('askYesNo', () => { it('resolves to true when user inputs y', async () => { const mockQuestion = jest.spyOn(process.stdin, 'once').mockImplementation((event, cb) => { if (event === 'data') cb('y\n'); }); const answer = askYesNo('Test question?'); process.stdin.emit('data', 'y\n'); expect(await answer).toBe(true); mockQuestion.mockRestore(); }); });