bunshine
Version:
A Bun HTTP & WebSocket server that is a little ray of sunshine.
35 lines (33 loc) • 976 B
text/typescript
import { describe, expect, it, spyOn } from 'bun:test';
import withTryCatch from './withTryCatch';
describe('withTryCatch utility', () => {
it('should return normally', async () => {
const fn = withTryCatch({
label: 'test',
defaultReturn: false,
func: () => true,
});
expect(fn()).toBe(true);
});
it('should return normally - promise', async () => {
const fn = withTryCatch({
label: 'test',
defaultReturn: false,
func: () => Promise.resolve(true),
});
expect(await fn()).toBe(true);
});
it('should swallow thrown errors', async () => {
const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
const fn = withTryCatch({
label: 'test',
defaultReturn: 'hello',
func: () => {
throw new Error('My error!');
},
});
expect(await fn()).toBe('hello');
expect(errorSpy).toHaveBeenCalledWith('test: My error!');
errorSpy.mockRestore();
});
});