Skip to content

Commit

Permalink
Merge pull request #5 from Teneff/develop
Browse files Browse the repository at this point in the history
Housekeeping (no functional changes)
  • Loading branch information
Teneff committed Apr 10, 2024
2 parents 9141cc2 + d99bf2f commit d8f5161
Show file tree
Hide file tree
Showing 8 changed files with 2,157 additions and 1,806 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"**/?(*.)+(spec|test).[tj]s?(x)"
],
"plugins": ["jest"],
"extends": ["plugin:jest/recommended"]
"extends": ["plugin:@typescript-eslint/recommended", "plugin:jest/recommended"]
}
]
}
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: "12.x"
node-version: "18.x"

- uses: actions/cache@v2
id: yarn-cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: "12.x"
node-version: "18.x"
registry-url: "https://registry.npmjs.org"
- run: yarn
- run: yarn build
Expand Down
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["javascript"]
}
"typescript.tsdk": "node_modules/typescript/lib"
}
20 changes: 20 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"preset": "ts-jest",
"rootDir": "src",
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"tsconfig": "tsconfig.jest.json"
}
]
},
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
}
35 changes: 9 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@teneff/with-retry",
"version": "1.1.0",
"version": "1.1.1",
"description": "Decorator for retrying async operations",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -28,34 +28,17 @@
},
"homepage": "https://github.com/teneff/withRetry#readme",
"devDependencies": {
"@types/jest": "^27.4.1",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"codecov": "^3.8.3",
"eslint": "^8.12.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.3",
"jest": "^27.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3"
},
"jest": {
"preset": "ts-jest",
"rootDir": "src",
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.jest.json"
}
},
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
"eslint-plugin-jest": "^28.2.0",
"jest": "^29.7.0",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.4"
}
}
150 changes: 111 additions & 39 deletions src/withRetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,12 @@ describe("withRetry", () => {
});

describe("given function delay", () => {
const mockCallback = jest.fn<Promise<string>, [string, string]>(() => {
throw new Error("subsequent error");
});
const mockCallback = jest
.fn<Promise<string>, [string, string]>()
.mockRejectedValueOnce(new Error("first error"))
.mockRejectedValueOnce(new Error("second error"))
.mockRejectedValueOnce(new Error("third error"))
.mockRejectedValueOnce(new Error("fourth error"));

let result: Promise<string>;

Expand Down Expand Up @@ -249,43 +252,76 @@ describe("withRetry", () => {
it("should contain previous errors", async () => {
await expect(result).rejects.toHaveProperty(
"cause",
new Array(4).fill(expect.any(Error))
[
expect.objectContaining({
constructor: Error,
message: "first error",
}),
expect.objectContaining({
constructor: Error,
message: "second error",
}),
expect.objectContaining({
constructor: Error,
message: "third error",
}),
expect.objectContaining({
constructor: Error,
message: "fourth error",
}),
]
);
});

describe("options.delay function", () => {
it("should be called with", () => {
expect(delayFn.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
"call": 0,
"errors": Array [
[Error: subsequent error],
],
},
],
Array [
Object {
"call": 1,
"errors": Array [
[Error: subsequent error],
[Error: subsequent error],
],
},
],
Array [
Object {
"call": 2,
"errors": Array [
[Error: subsequent error],
[Error: subsequent error],
[Error: subsequent error],
],
},
],
]
`);
expect(delayFn.mock.calls).toEqual([
[
{
call: 0,
errors: [
expect.objectContaining({
constructor: Error,
message: "first error",
}),
],
},
],
[
{
call: 1,
errors: [
expect.objectContaining({
constructor: Error,
message: "first error",
}),
expect.objectContaining({
constructor: Error,
message: "second error",
}),
],
},
],
[
{
call: 2,
errors: [
expect.objectContaining({
constructor: Error,
message: "first error",
}),
expect.objectContaining({
constructor: Error,
message: "second error",
}),
expect.objectContaining({
constructor: Error,
message: "third error",
}),
],
},
],
]);
});
});
});
Expand All @@ -295,18 +331,54 @@ Array [
});

describe("given a function that throws non-Errors", () => {
const mockFn = jest.fn().mockRejectedValue("non-Error");
const mockFn = jest
.fn()
.mockRejectedValue("additional error")
.mockRejectedValueOnce("first fail")
.mockRejectedValueOnce("second fail")
.mockRejectedValueOnce("third fail")
.mockRejectedValueOnce("fourth fail");

let result: Promise<unknown>;
beforeAll(async () => {
const callbackWithRetry = withRetry({
maxCalls: 4,
errors: [UnknownError],
})(mockFn);

await callbackWithRetry("arg1", "arg2").catch(() => undefined);

result = callbackWithRetry("arg1", "arg2");
try {
await result;
} catch {}
});

it("should have called the function 4 times", () => {
expect(mockFn).toHaveBeenCalledTimes(4)
expect(mockFn).toHaveBeenCalledTimes(4);
});

it("should fail with ResourceExhaustedError containing all internal errors", async () => {
await expect(result).rejects.toHaveProperty("cause", [
expect.objectContaining({
constructor: UnknownError,
message: "Unknown error",
unknown: "first fail",
}),
expect.objectContaining({
constructor: UnknownError,
message: "Unknown error",
unknown: "second fail",
}),
expect.objectContaining({
constructor: UnknownError,
message: "Unknown error",
unknown: "third fail",
}),
expect.objectContaining({
constructor: UnknownError,
message: "Unknown error",
unknown: "fourth fail",
}),
]);
});
});
});
Loading

0 comments on commit d8f5161

Please sign in to comment.