Some uni-app platforms ignore the `timeout` option, leaving the returned promise pending forever and hanging the calling page with no error path. Drive the deadline locally: keep a single-shot `finish` guard so success, failure and timeout can only settle once, and on timeout call `task.abort()` before rejecting so the in-flight request is released instead of leaking. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
41 lines
974 B
JavaScript
41 lines
974 B
JavaScript
import assert from 'node:assert/strict';
|
|
import { after, test } from 'node:test';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { createServer } from 'vite';
|
|
|
|
let abortCount = 0;
|
|
globalThis.plus = {};
|
|
globalThis.uni = {
|
|
getStorageSync: () => '',
|
|
removeStorageSync: () => {},
|
|
request: () => ({
|
|
abort: () => {
|
|
abortCount += 1;
|
|
}
|
|
})
|
|
};
|
|
|
|
const vite = await createServer({
|
|
root: fileURLToPath(new URL('..', import.meta.url)),
|
|
configFile: false,
|
|
optimizeDeps: { noDiscovery: true },
|
|
server: { middlewareMode: true, hmr: false },
|
|
appType: 'custom'
|
|
});
|
|
|
|
after(async () => {
|
|
await vite.close();
|
|
delete globalThis.plus;
|
|
delete globalThis.uni;
|
|
});
|
|
|
|
const { apiRequest } = await vite.ssrLoadModule('/src/services/api.ts');
|
|
|
|
test('App 原生请求遗漏回调时主动超时并中止任务', async () => {
|
|
await assert.rejects(
|
|
apiRequest({ url: '/api/test', timeout: 10 }),
|
|
/网络请求超时/
|
|
);
|
|
assert.equal(abortCount, 1);
|
|
});
|