Skip to content

simple libraray for jest-tested projects to create jest mock for js aws-sdk .promise() calls

License

Notifications You must be signed in to change notification settings

jurijzahn8019/aws-promise-jest-mock

Repository files navigation

aws-promise-jest-mock

Build Status Coverage Status Dependabot Status GitHub npm Vulnerabilities npm bundle size (scoped) GitHub last commit

simple libraray for jest-tested projects to create jest mock for js aws-sdk .promise() calls

WHY

i was tired to copy-paste my mock helpers across repos

install

npm i -D @jurijzahn8019/aws-promise-jest-mock

usage

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);
  });
});

Changelog

Changelog.

License

MIT Lizenz

About

simple libraray for jest-tested projects to create jest mock for js aws-sdk .promise() calls

Resources

License

Stars

Watchers

Forks

Packages