-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy patherror-request.test.ts
More file actions
92 lines (86 loc) · 2.62 KB
/
error-request.test.ts
File metadata and controls
92 lines (86 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { describe, it, expect } from "vitest";
import { defaultShouldRetry, errorRequest } from "../src/error-request.ts";
import { defaultRetryState } from "../src/index.ts";
import { RequestError } from "@octokit/request-error";
import { TestOctokit } from "./octokit.ts";
import { RetryState } from "../src/types.ts";
import type { RequestMethod, RequestOptions } from "@octokit/types";
describe("defaultShouldRetry", function () {
it("should re-throw non-RequestError errors", function () {
try {
defaultShouldRetry(defaultRetryState, new Error("Re-throw me"));
throw new Error("Should not reach this point");
} catch (err: any) {
expect(err.message).toEqual("Re-throw me");
}
});
it("should re-throw errors without RequestRequestOptions", function () {
try {
defaultShouldRetry(
defaultRetryState,
new RequestError("Re-throw me", 500, {
request: { method: "GET", url: "/something", headers: {} },
}),
);
throw new Error("Should not reach this point");
} catch (err: any) {
expect(err.message).toEqual("Re-throw me");
}
});
it("returns false for doNotRetry status codes", function () {
for (const statusCode of defaultRetryState.doNotRetry) {
const result = defaultShouldRetry(
defaultRetryState,
new RequestError("Re-throw me", statusCode, {
request: {
method: "GET",
url: "/something",
headers: {},
request: {},
},
}),
);
expect(result).toBe(false);
}
});
it("returns true for 500 errors", function () {
const result = defaultShouldRetry(
defaultRetryState,
new RequestError("Re-throw me", 500, {
request: {
method: "GET",
url: "/something",
headers: {},
request: {},
},
}),
);
expect(result).toBe(true);
});
});
describe("errorRequest", function () {
it("allows non-RequestErrors to be retried", async function () {
const state: RetryState = {
...defaultRetryState,
shouldRetry: (_state, _error) => true,
};
const requestOptions = {
method: "GET" as RequestMethod,
url: "/issues",
headers: {},
request: {},
} satisfies RequestOptions;
try {
await errorRequest(
state,
new TestOctokit(),
new Error("Some non-RequestError"),
requestOptions,
);
throw new Error("Should not reach this point");
} catch (error: any) {
expect(error.message).toBe("Some non-RequestError");
expect(error.request.request.retries).toBe(3);
}
});
});