示例工程 32-capstone-quality-gates
32-capstone-quality-gates/02-api-client/main.test.mjs
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { fetchUser, isUser, run } from './dist/main.js';
function response(status, body) {
return {
ok: status >= 200 && status < 300,
status,
json: async () => body,
};
}
test('API client 在边界做运行时校验', async () => {
await assert.rejects(() => run(() => {}), /浏览器/);
const ok = await fetchUser(async () => response(200, { id: 7, name: 'Ada' }), '/user');
assert.deepEqual(ok, { id: 7, name: 'Ada' });
await assert.rejects(() => fetchUser(async () => response(404, {}), '/user'), /HTTP 404/);
await assert.rejects(() => fetchUser(async () => response(200, { id: '7' }), '/user'), /invalid/);
assert.equal(isUser({ id: 1, name: 'x' }), true);
});