simple libraray for jest-tested projects to create jest mock for js aws-sdk .promise() calls
i was tired to copy-paste my mock helpers across repos
npm i -D @jurijzahn8019/aws-promise-jest-mock
in your code file
export function foo() {
return new SecretsManager().getSecretValue({ SecretId: "bar-baz" }).promise();
}
in your spec file
import { SecretsManager } from "aws-sdk";
import { on } from "@jurijzahn8019/aws-promise-jest-mock";
import { foo } from "./code.ts";
jest.mock("aws-sdk");
describe("aws-mock", () => {
it("Should succeed", async () => {
const m = on(SecretsManager)
.mock("getSecretValue")
.resolve({ SecretString: "foo-bar" });
const res = foo();
await expect(res).resolves.toMatchSnapshot("Result");
expect(m.mock).toHaveBeenCalledTimes(1);
});
it("Should fail", async () => {
const m = on(SecretsManager).mock("getSecretValue").reject("foo-baz");
const res = foo();
await expect(res).rejects.toMatchSnapshot("Result");
expect(m.mock).toHaveBeenCalledTimes(1);
});
});
chain mocks
import { SecretsManager } from "aws-sdk";
import { on } from "@jurijzahn8019/aws-promise-jest-mock";
import { foo } from "./code.ts";
jest.mock("aws-sdk");
describe("aws-mock", () => {
it("Should succeed", async () => {
const m = on(SecretsManager)
.mock("getSecretValue")
.resolveOnce({ SecretString: "foo-bar" })
.resolveOnce({ SecretString: "baz-bar" })
.rejectOnce({ SecretString: "baz-bar" });
const res = foo();
await expect(res).resolves.toMatchSnapshot("Result 1");
await expect(res).resolves.toMatchSnapshot("Result 2");
await expect(res).rejects.toMatchSnapshot("Error");
expect(m.mock).toHaveBeenCalledTimes(3);
});
});