



























11import { describe, expect, it, vi } from "vitest";
22import { runGatewayHttpRequestStages } from "./server-http.js";
334+type TestGatewayHttpRequestStage = Parameters<typeof runGatewayHttpRequestStages>[0][number];
5+6+async function expectContinueOnErrorStageSkips(params: {
7+stageName: string;
8+stageError: Error;
9+stageRun: TestGatewayHttpRequestStage["run"];
10+prefixStages?: TestGatewayHttpRequestStage[];
11+}): Promise<void> {
12+const stageC = vi.fn(() => true);
13+const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
14+try {
15+const result = await runGatewayHttpRequestStages([
16+ ...(params.prefixStages ?? []),
17+{
18+name: params.stageName,
19+continueOnError: true,
20+run: params.stageRun,
21+},
22+{ name: "c", run: stageC },
23+]);
24+25+expect(result).toBe(true);
26+expect(stageC).toHaveBeenCalled();
27+expect(consoleSpy.mock.calls).toEqual([
28+[`[gateway-http] stage "${params.stageName}" threw — skipping:`, params.stageError],
29+]);
30+} finally {
31+consoleSpy.mockRestore();
32+}
33+}
34+435describe("runGatewayHttpRequestStages", () => {
536it("returns true when a stage handles the request", async () => {
637const stages = [
@@ -21,57 +52,25 @@ describe("runGatewayHttpRequestStages", () => {
21522253it("skips a throwing stage marked continueOnError and continues to subsequent stages", async () => {
2354const stageError = new Error("Cannot find module '@slack/bolt'");
24-const stageC = vi.fn(() => true);
25-const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
26-27-const stages = [
28-{ name: "a", run: () => false },
29-{
30-name: "broken-facade",
31-continueOnError: true,
32-run: () => {
33-throw stageError;
34-},
55+await expectContinueOnErrorStageSkips({
56+stageName: "broken-facade",
57+ stageError,
58+stageRun: () => {
59+throw stageError;
3560},
36-{ name: "c", run: stageC },
37-];
38-39-const result = await runGatewayHttpRequestStages(stages);
40-41-expect(result).toBe(true);
42-expect(stageC).toHaveBeenCalled();
43-expect(consoleSpy.mock.calls).toEqual([
44-['[gateway-http] stage "broken-facade" threw — skipping:', stageError],
45-]);
46-47-consoleSpy.mockRestore();
61+prefixStages: [{ name: "a", run: () => false }],
62+});
4863});
49645065it("skips a rejecting async stage marked continueOnError and continues", async () => {
5166const stageError = new Error("ERR_MODULE_NOT_FOUND");
52-const stageC = vi.fn(() => true);
53-const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
54-55-const stages = [
56-{
57-name: "async-broken",
58-continueOnError: true,
59-run: async () => {
60-throw stageError;
61-},
67+await expectContinueOnErrorStageSkips({
68+stageName: "async-broken",
69+ stageError,
70+stageRun: async () => {
71+throw stageError;
6272},
63-{ name: "c", run: stageC },
64-];
65-66-const result = await runGatewayHttpRequestStages(stages);
67-68-expect(result).toBe(true);
69-expect(stageC).toHaveBeenCalled();
70-expect(consoleSpy.mock.calls).toEqual([
71-['[gateway-http] stage "async-broken" threw — skipping:', stageError],
72-]);
73-74-consoleSpy.mockRestore();
73+});
7574});
76757776it("rethrows when a stage throws without continueOnError", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。