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 aws-promise-jest-mock
// code file
export function foo() {
return new SecretsManager().getSecretValue({ SecretId: "bar-baz" }).promise();
}
// spec file
import { SecretsManager } from "aws-sdk";
import { on, infer } from "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", infer)
.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", infer)
.reject("foo-baz");
const res = foo();
await expect(res).rejects.toMatchSnapshot("Result");
expect(m.mock).toHaveBeenCalledTimes(1);
});
});