test: guard infra helper assertions · openclaw/openclaw@1365b71
steipete
·
2026-05-12
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -33,8 +33,10 @@ function collectMatching<T, U>(
|
33 | 33 | |
34 | 34 | function findBeaconByInstance(beacons: readonly BeaconRecord[], instanceName: string) { |
35 | 35 | const beacon = beacons.find((item) => item.instanceName === instanceName); |
36 | | -expect(beacon).toBeDefined(); |
37 | | -return beacon as BeaconRecord; |
| 36 | +if (!beacon) { |
| 37 | +throw new Error(`Expected beacon ${instanceName}`); |
| 38 | +} |
| 39 | +return beacon; |
38 | 40 | } |
39 | 41 | |
40 | 42 | describe("bonjour-discovery", () => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,9 @@ async function expectPathMissing(targetPath: string): Promise<void> {
|
27 | 27 | } catch (error) { |
28 | 28 | statError = error; |
29 | 29 | } |
30 | | -expect(statError).toBeDefined(); |
| 30 | +if (statError === undefined) { |
| 31 | +throw new Error(`Expected ${targetPath} to be missing`); |
| 32 | +} |
31 | 33 | expect((statError as { code?: unknown }).code).toBe("ENOENT"); |
32 | 34 | } |
33 | 35 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -143,8 +143,10 @@ function expectRisk(
|
143 | 143 | const risk = risks.find((candidate) => riskMatches(candidate, fields)) as |
144 | 144 | | Record<string, unknown> |
145 | 145 | | undefined; |
146 | | -expect(risk).toBeDefined(); |
147 | | -return risk ?? {}; |
| 146 | +if (!risk) { |
| 147 | +throw new Error(`Expected risk ${JSON.stringify(fields)}`); |
| 148 | +} |
| 149 | +return risk; |
148 | 150 | } |
149 | 151 | |
150 | 152 | afterEach(() => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,9 @@ afterEach(async () => {
|
26 | 26 | |
27 | 27 | async function expectRejectCode(promise: Promise<unknown>, expected: string | RegExp) { |
28 | 28 | const err = await promise.catch((caught: unknown) => caught); |
29 | | -expect(err).toBeDefined(); |
| 29 | +if (err === undefined) { |
| 30 | +throw new Error("Expected promise to reject"); |
| 31 | +} |
30 | 32 | const code = (err as NodeJS.ErrnoException).code; |
31 | 33 | if (typeof expected === "string") { |
32 | 34 | expect(code).toBe(expected); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -26,8 +26,10 @@ function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
26 | 26 | function spawnCall(mock: unknown, callIndex: number) { |
27 | 27 | const calls = (mock as { mock?: { calls?: Array<Array<unknown>> } }).mock?.calls ?? []; |
28 | 28 | const call = calls.at(callIndex); |
29 | | -expect(call, `spawn call ${callIndex + 1}`).toBeDefined(); |
30 | | -return call as Array<unknown>; |
| 29 | +if (!call) { |
| 30 | +throw new Error(`Expected spawn call ${callIndex + 1}`); |
| 31 | +} |
| 32 | +return call; |
31 | 33 | } |
32 | 34 | |
33 | 35 | function expectSpawn(mock: unknown, callIndex: number, command: string, args: Array<unknown>) { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -65,9 +65,11 @@ describe("runHeartbeatOnce commitments", () => {
|
65 | 65 | commitment: CommitmentRecord | undefined, |
66 | 66 | expected: Partial<CommitmentRecord>, |
67 | 67 | ) { |
68 | | -expect(commitment).toBeDefined(); |
| 68 | +if (!commitment) { |
| 69 | +throw new Error("Expected heartbeat commitment"); |
| 70 | +} |
69 | 71 | for (const [key, value] of Object.entries(expected)) { |
70 | | -expect(commitment?.[key as keyof CommitmentRecord]).toEqual(value); |
| 72 | +expect(commitment[key as keyof CommitmentRecord]).toEqual(value); |
71 | 73 | } |
72 | 74 | } |
73 | 75 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -64,8 +64,10 @@ describe("startHeartbeatRunner", () => {
|
64 | 64 | |
65 | 65 | function getRunCall(runSpy: MockRunOnce, callIndex: number) { |
66 | 66 | const call = runSpy.mock.calls[callIndex]; |
67 | | -expect(call).toBeDefined(); |
68 | | -const options = call?.[0]; |
| 67 | +if (!call) { |
| 68 | +throw new Error(`Expected heartbeat run call ${callIndex}`); |
| 69 | +} |
| 70 | +const options = call[0]; |
69 | 71 | expect(typeof options).toBe("object"); |
70 | 72 | expect(options).not.toBeNull(); |
71 | 73 | return options as Record<string, unknown>; |
@@ -93,9 +95,11 @@ describe("startHeartbeatRunner", () => {
|
93 | 95 | .slice(params.startIndex ?? 0) |
94 | 96 | .map((entry) => entry[0] as { agentId?: string; heartbeat?: { every?: string } }) |
95 | 97 | .find((options) => options.agentId === params.agentId); |
96 | | -expect(call).toBeDefined(); |
| 98 | +if (!call) { |
| 99 | +throw new Error(`Expected heartbeat run call for ${params.agentId}`); |
| 100 | +} |
97 | 101 | if (params.expectedHeartbeatEvery) { |
98 | | -expect(call?.heartbeat?.every).toBe(params.expectedHeartbeatEvery); |
| 102 | +expect(call.heartbeat?.every).toBe(params.expectedHeartbeatEvery); |
99 | 103 | } |
100 | 104 | } |
101 | 105 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -172,7 +172,9 @@ describe("json-file helpers", () => {
|
172 | 172 | } catch (error) { |
173 | 173 | saveError = error; |
174 | 174 | } |
175 | | -expect(saveError).toBeDefined(); |
| 175 | +if (saveError === undefined) { |
| 176 | +throw new Error("Expected saveJsonFile to fail"); |
| 177 | +} |
176 | 178 | expect((saveError as { code?: unknown }).code).toBe("ENOENT"); |
177 | 179 | expect(fs.existsSync(missingTargetDir)).toBe(false); |
178 | 180 | expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,9 @@ function expectExecCall(
|
32 | 32 | options?: Record<string, unknown>, |
33 | 33 | ) { |
34 | 34 | const call = exec.mock.calls[callNumber - 1]; |
35 | | -expect(call, `call ${callNumber}`).toBeDefined(); |
| 35 | +if (!call) { |
| 36 | +throw new Error(`Expected exec call ${callNumber}`); |
| 37 | +} |
36 | 38 | expect(call[0]).toBe(command); |
37 | 39 | expect(call[1]).toEqual(args); |
38 | 40 | if (options) { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。