@jurijzahn8019/aws-promise-jest-mock
TypeScript icon, indicating that this package has built-in type declarations

2.4.84 • Public • Published

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

Package Sidebar

Install

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

Weekly Downloads

520

Version

2.4.84

License

MIT

Unpacked Size

936 kB

Total Files

18

Last publish

Collaborators

  • jurijzahn8019