Skip to content

chore(middleware-retry): move mocks to beforeEach #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions packages/middleware-retry/src/defaultStrategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,22 @@ import { getDefaultRetryQuota } from "./defaultRetryQuota";
import { HttpRequest } from "@aws-sdk/protocol-http";
import { v4 } from "uuid";

jest.mock("@aws-sdk/service-error-classification", () => ({
isThrottlingError: jest.fn().mockReturnValue(true)
}));
jest.mock("@aws-sdk/service-error-classification");
jest.mock("./delayDecider");
jest.mock("./retryDecider");
jest.mock("./defaultRetryQuota");
jest.mock("@aws-sdk/protocol-http");
jest.mock("uuid");

jest.mock("./delayDecider", () => ({
defaultDelayDecider: jest.fn().mockReturnValue(0)
}));

jest.mock("./retryDecider", () => ({
defaultRetryDecider: jest.fn().mockReturnValue(true)
}));
describe("defaultStrategy", () => {
let next: jest.Mock; // variable for next mock function in utility methods
const maxAttempts = 3;

jest.mock("./defaultRetryQuota", () => {
const mockDefaultRetryQuota = {
hasRetryTokens: jest.fn().mockReturnValue(true),
retrieveRetryTokens: jest.fn().mockReturnValue(1),
releaseRetryTokens: jest.fn()
};
return { getDefaultRetryQuota: () => mockDefaultRetryQuota };
});

jest.mock("@aws-sdk/protocol-http", () => ({
HttpRequest: {
isInstance: jest.fn().mockReturnValue(false)
}
}));

jest.mock("uuid", () => ({
v4: jest.fn(() => "42")
}));

describe("defaultStrategy", () => {
let next: jest.Mock; // variable for next mock function in utility methods
const maxAttempts = 3;

const mockSuccessfulOperation = (
maxAttempts: number,
Expand Down Expand Up @@ -122,6 +104,17 @@ describe("defaultStrategy", () => {
return retryStrategy.retry(next, { request: { headers: {} } } as any);
};

beforeEach(() => {
(isThrottlingError as jest.Mock).mockReturnValue(true);
(defaultDelayDecider as jest.Mock).mockReturnValue(0);
(defaultRetryDecider as jest.Mock).mockReturnValue(true);
(getDefaultRetryQuota as jest.Mock).mockReturnValue(mockDefaultRetryQuota);
((HttpRequest as unknown) as jest.Mock).mockReturnValue({
isInstance: jest.fn().mockReturnValue(false)
});
(v4 as jest.Mock).mockReturnValue("42");
});

afterEach(() => {
jest.clearAllMocks();
});
Expand Down
14 changes: 8 additions & 6 deletions packages/middleware-retry/src/retryDecider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import {
import { defaultRetryDecider } from "./retryDecider";
import { SdkError } from "@aws-sdk/smithy-client";

jest.mock("@aws-sdk/service-error-classification", () => ({
isRetryableByTrait: jest.fn().mockReturnValue(false),
isClockSkewError: jest.fn().mockReturnValue(false),
isThrottlingError: jest.fn().mockReturnValue(false),
isTransientError: jest.fn().mockReturnValue(false)
}));
jest.mock("@aws-sdk/service-error-classification");

describe("defaultRetryDecider", () => {
const createMockError = () =>
Object.assign(new Error(), { $metadata: {} }) as SdkError;

beforeEach(() => {
(isRetryableByTrait as jest.Mock).mockReturnValue(false);
(isClockSkewError as jest.Mock).mockReturnValue(false);
(isThrottlingError as jest.Mock).mockReturnValue(false);
(isTransientError as jest.Mock).mockReturnValue(false);
});

afterEach(() => {
jest.clearAllMocks();
});

Expand Down