axios-miniprogram/scripts/test.utils.ts

50 lines
972 B
TypeScript
Raw Normal View History

2023-03-29 22:23:36 +08:00
export function asyncNext() {
return Promise.resolve().then;
}
export function asyncTimeout(delay = 0) {
2023-04-01 23:11:55 +08:00
return new Promise((resolve) => setTimeout(resolve, delay));
2023-03-29 22:23:36 +08:00
}
export function captureError<T = any>(fn: () => void): T {
try {
fn();
2023-04-01 23:11:55 +08:00
throw new Error('without Error');
2023-03-29 22:23:36 +08:00
} catch (err) {
return err as T;
}
}
export function cleanedStack(error: Error) {
if (error.stack) {
return error.stack.indexOf('at') === error.stack.indexOf('at /');
}
return true;
}
2023-03-29 22:23:36 +08:00
export function noop() {
return;
}
export function mockResponse(
status: number,
statusText: string,
headers: AnyObject,
data: AnyObject,
) {
return {
status,
statusText,
headers,
data,
};
}
2023-04-01 23:11:55 +08:00
export function mockSuccess(headers: AnyObject = {}, data: AnyObject = {}) {
2023-03-29 22:23:36 +08:00
return mockResponse(200, 'OK', headers, data);
}
2023-04-01 23:11:55 +08:00
export function mockFail(headers: AnyObject = {}, data: AnyObject = {}) {
2023-03-29 22:23:36 +08:00
return mockResponse(400, 'FAIL', headers, data);
}