axios-miniprogram/test/helpers/error.test.ts

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-03-28 21:32:54 +08:00
import { describe, test, expect } from 'vitest';
2023-04-03 21:03:33 +08:00
import { captureError, checkStack } from 'scripts/test.utils';
2023-04-10 22:53:15 +08:00
import { assert, throwError, cleanStack } from '@/helpers/error';
2023-03-28 21:32:54 +08:00
describe('src/helpers/error.ts', () => {
2023-03-28 21:32:54 +08:00
test('第一个参数为 true 时应该无事发生', () => {
expect(assert(true, '')).toBeUndefined();
});
test('第一个参数为 false 时应该抛出异常', () => {
expect(() => assert(false, '')).toThrowError();
2023-04-03 21:03:33 +08:00
expect(checkStack(captureError(() => assert(false, '')))).toBeTruthy();
2023-03-28 21:32:54 +08:00
});
test('应该抛出异常', () => {
2023-04-03 21:03:33 +08:00
expect(() => throwError('')).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: "',
2023-03-28 21:32:54 +08:00
);
2023-04-03 21:03:33 +08:00
expect(() => throwError('error')).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: error"',
);
expect(checkStack(captureError(() => throwError('error')))).toBeTruthy();
});
2023-04-10 22:53:15 +08:00
test('应该支持空错误栈', () => {
const ce = () => {
const error = new Error();
error.stack = undefined;
return error;
};
const error = ce();
cleanStack(error);
expect(checkStack(error)).toBeTruthy();
expect(error.stack).toBeUndefined();
});
test('应该清掉多余的错误栈', () => {
const ce = () => new Error();
const error = ce();
2023-04-03 21:03:33 +08:00
expect(checkStack(error)).toBeFalsy();
cleanStack(error);
2023-04-03 21:03:33 +08:00
expect(checkStack(error)).toBeTruthy();
2023-03-28 21:32:54 +08:00
});
});