60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { expect, test } from 'vitest';
|
|
import { normalizeError } from '../source/misc.ts';
|
|
|
|
/** Test cases for the normalizeError function. */
|
|
const normalizeErrorTestCases = [
|
|
{
|
|
input: new Error('test error'),
|
|
expected: new Error('test error'),
|
|
},
|
|
{
|
|
input: 'test error',
|
|
expected: new Error('test error'),
|
|
},
|
|
{
|
|
input: 1,
|
|
expected: new Error('1'),
|
|
},
|
|
{
|
|
input: undefined,
|
|
expected: new Error('undefined'),
|
|
},
|
|
{
|
|
input: null,
|
|
expected: new Error('null'),
|
|
},
|
|
{
|
|
input: false,
|
|
expected: new Error('false'),
|
|
},
|
|
{
|
|
input: true,
|
|
expected: new Error('true'),
|
|
},
|
|
{
|
|
input: {},
|
|
expected: new Error('[object Object]'),
|
|
},
|
|
{
|
|
input: [],
|
|
expected: new Error(),
|
|
},
|
|
];
|
|
|
|
/**
|
|
* Tests that normalizeError converts non-Error throws to Error instances.
|
|
*/
|
|
const testNormalizeError = async (): Promise<void> => {
|
|
for (const testCase of normalizeErrorTestCases) {
|
|
const result = normalizeError(testCase.input);
|
|
expect(result).toBeInstanceOf(Error);
|
|
expect(result.message).toBe(testCase.expected.message);
|
|
}
|
|
};
|
|
|
|
const runTests = async (): Promise<void> => {
|
|
test('normalizeError: converts non-Error throws to Error instances', testNormalizeError);
|
|
};
|
|
|
|
await runTests();
|