Skip to content

Commit

Permalink
test: compatibility with vitest v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jan 7, 2025
1 parent f219c46 commit 322a4de
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
18 changes: 11 additions & 7 deletions packages/middleware-retry/src/StandardRetryStrategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,29 @@ describe("defaultStrategy", () => {
vi.clearAllMocks();
});

it("sets maxAttemptsProvider as class member variable", () => {
[1, 2, 3].forEach((maxAttempts) => {
const retryStrategy = new StandardRetryStrategy(() => Promise.resolve(maxAttempts));
expect(retryStrategy["maxAttemptsProvider"]()).resolves.toBe(maxAttempts);
});
it("sets maxAttemptsProvider as class member variable", async () => {
await Promise.all(
[1, 2, 3].map(async (maxAttempts) => {
const retryStrategy = new StandardRetryStrategy(() => Promise.resolve(maxAttempts));
expect(await retryStrategy["maxAttemptsProvider"]()).toBe(maxAttempts);
})
);
});

it(`sets mode=${RETRY_MODES.STANDARD}`, () => {
const retryStrategy = new StandardRetryStrategy(() => Promise.resolve(maxAttempts));
expect(retryStrategy.mode).toStrictEqual(RETRY_MODES.STANDARD);
});

it("handles non-standard errors", () => {
it("handles non-standard errors", async () => {
const nonStandardErrors = [undefined, "foo", { foo: "bar" }, 123, false, null];
const maxAttempts = 1;
const retryStrategy = new StandardRetryStrategy(() => Promise.resolve(maxAttempts));
for (const error of nonStandardErrors) {
next = vi.fn().mockRejectedValue(error);
expect(retryStrategy.retry(next, { request: { headers: {} } } as any)).rejects.toBeInstanceOf(Error);
expect(await retryStrategy.retry(next, { request: { headers: {} } } as any).catch((_) => _)).toBeInstanceOf(
Error
);
}
});

Expand Down
11 changes: 6 additions & 5 deletions packages/middleware-retry/src/configurations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Provider } from "@smithy/types";
import { normalizeProvider } from "@smithy/util-middleware";
import { AdaptiveRetryStrategy, DEFAULT_MAX_ATTEMPTS, StandardRetryStrategy } from "@smithy/util-retry";
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
Expand All @@ -17,7 +18,7 @@ describe(resolveRetryConfig.name, () => {

beforeEach(() => {
vi.mocked(normalizeProvider).mockImplementation((input) =>
typeof input === "function" ? input : () => Promise.resolve(input)
typeof input === "function" ? (input as Provider<unknown>) : () => Promise.resolve(input)
);
});

Expand All @@ -38,15 +39,15 @@ describe(resolveRetryConfig.name, () => {
});

describe("retryStrategy", () => {
it("passes retryStrategy if present", () => {
it("passes retryStrategy if present", async () => {
const mockRetryStrategy = {
retry: vi.fn(),
};
const { retryStrategy } = resolveRetryConfig({
retryMode,
retryStrategy: mockRetryStrategy,
});
expect(retryStrategy()).resolves.toEqual(mockRetryStrategy);
expect(await retryStrategy()).toEqual(mockRetryStrategy);
});

describe("creates RetryStrategy if retryStrategy not present", () => {
Expand Down Expand Up @@ -141,7 +142,7 @@ describe(resolveRetryConfig.name, () => {
it(`should throw if if value of env ${ENV_MAX_ATTEMPTS} is not a number`, () => {
const value = "not a number";
const env = { [ENV_MAX_ATTEMPTS]: value };
expect(() => NODE_MAX_ATTEMPT_CONFIG_OPTIONS.environmentVariableSelector(env)).toThrow("");
expect(() => NODE_MAX_ATTEMPT_CONFIG_OPTIONS.environmentVariableSelector(env)).toThrow();
});
});

Expand All @@ -159,7 +160,7 @@ describe(resolveRetryConfig.name, () => {
it(`should throw if shared INI files entry ${CONFIG_MAX_ATTEMPTS} is not a number`, () => {
const value = "not a number";
const profile = { [CONFIG_MAX_ATTEMPTS]: value };
expect(() => NODE_MAX_ATTEMPT_CONFIG_OPTIONS.configFileSelector(profile)).toThrow("");
expect(() => NODE_MAX_ATTEMPT_CONFIG_OPTIONS.configFileSelector(profile)).toThrow();
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/util-waiter/src/createWaiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("createWaiter", () => {
);
vi.advanceTimersByTime(10 * 1000);
abortController.abort(); // Abort before maxWaitTime(20s);
expect(await statusPromise).toContain(abortedState);
expect(await statusPromise).toMatchObject(abortedState);
});

it("should success when acceptor checker returns seccess", async () => {
Expand All @@ -67,7 +67,7 @@ describe("createWaiter", () => {
mockAcceptorChecks
);
vi.advanceTimersByTime(minimalWaiterConfig.minDelay * 1000);
expect(await statusPromise).toContain(successState);
expect(await statusPromise).toMatchObject(successState);
});

it("should fail when acceptor checker returns failure", async () => {
Expand All @@ -81,6 +81,6 @@ describe("createWaiter", () => {
mockAcceptorChecks
);
vi.advanceTimersByTime(minimalWaiterConfig.minDelay * 1000);
expect(await statusPromise).toContain(failureState);
expect(await statusPromise).toMatchObject(failureState);
});
});

0 comments on commit 322a4de

Please sign in to comment.