Something went wrong. Try again.
fork of hey-api/openapi-ts because I need some additional things
Something went wrong. Try again.
3.2 kB · 97 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import { shouldReportCrash } from '../error';
describe('shouldReportCrash', () => { it('should return false when isInteractive is false', async () => { const result = await shouldReportCrash({ error: new Error('test error'), isInteractive: false, }); expect(result).toBe(false); });
it('should return false when isInteractive is undefined', async () => { const result = await shouldReportCrash({ error: new Error('test error'), isInteractive: undefined, }); expect(result).toBe(false); });
it('should not prompt when isInteractive is explicitly false', async () => { // Mock stdin and console.log to ensure we don't wait for user input const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined); const setEncodingSpy = vi .spyOn(process.stdin, 'setEncoding') .mockImplementation(() => process.stdin as any); const onceSpy = vi.spyOn(process.stdin, 'once').mockImplementation(() => process.stdin);
const result = await shouldReportCrash({ error: new Error('test error'), isInteractive: false, });
expect(result).toBe(false); expect(logSpy).not.toHaveBeenCalled(); expect(setEncodingSpy).not.toHaveBeenCalled(); expect(onceSpy).not.toHaveBeenCalled();
logSpy.mockRestore(); setEncodingSpy.mockRestore(); onceSpy.mockRestore(); });
it('should prompt when isInteractive is true', async () => { // Mock stdin and console.log for interactive session const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined); const setEncodingSpy = vi .spyOn(process.stdin, 'setEncoding') .mockImplementation(() => process.stdin as any); const onceSpy = vi.spyOn(process.stdin, 'once').mockImplementation((_event, callback) => { // Simulate user typing 'n' setTimeout(() => { (callback as any)('n'); }, 0); return process.stdin; });
const result = await shouldReportCrash({ error: new Error('test error'), isInteractive: true, });
expect(result).toBe(false); // User said 'n' expect(logSpy).toHaveBeenCalledWith( expect.stringContaining('📢 Open a GitHub issue with crash details?'), );
logSpy.mockRestore(); setEncodingSpy.mockRestore(); onceSpy.mockRestore(); });
it('should handle user saying yes to crash report', async () => { // Mock stdin and console.log for interactive session const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined); const setEncodingSpy = vi .spyOn(process.stdin, 'setEncoding') .mockImplementation(() => process.stdin as any); const onceSpy = vi.spyOn(process.stdin, 'once').mockImplementation((_event, callback) => { // Simulate user typing 'y' setTimeout(() => { (callback as any)('y'); }, 0); return process.stdin; });
const result = await shouldReportCrash({ error: new Error('test error'), isInteractive: true, });
expect(result).toBe(true); // User said 'y'
logSpy.mockRestore(); setEncodingSpy.mockRestore(); onceSpy.mockRestore(); });});