1.2.54 • Published 1 year ago

@jurijzahn8019/jest-mock-inference-helper v1.2.54

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

jest-mock-inference-helper

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

This targets typescrit projects and aims to simplify declaration of mocked classes and functions

install

npm i -D @jurijzahn8019/jest-mock-inference-helper

usage

asMock

AsMockDemo

Assuming SUT file

import { bar, baz } from "./bar";

export function foo() {
  return bar() + " - " + baz();
}

in your spec file

import { asMock } from "@jurijzahn8019/jest-mock-inference-helper";
import { foo } from "./foo";
import { bar, baz } from "./bar";

// Automock bar
jest.mock("bar");

const barMock = asMock(bar).mockReturnValue("bar");
const bazMock = asMock(baz).mockReturnValue("baz");

// same as
const barMock = (bar as jest.MockedFunction<typeof bar>).mockReturnValue("bar");
const bazMock = (baz as jest.MockedFunction<typeof baz>).mockReturnValue("baz");

describe("foo", () => {
  it("Should success", () => {
    const res = foo();

    await expect(barMock).toHaveBeenCalled();
  });
});

asMocks

works similar to asMock but provides inference sugar for multiple functions in a single call

AsMocksDemo

Assuming SUT file

import { bar, baz } from "./bar";

export function foo() {
  return bar() + " - " + baz();
}

in your spec file

import { asMock } from "@jurijzahn8019/jest-mock-inference-helper";
import { foo } from "./foo";
import { bar, baz } from "./bar";

// Automock bar
jest.mock("bar");

const { barMock, bazMock } = asMocks({ bar, baz });

// same as
const barMock = bar as jest.MockedFunction<typeof bar>;
const bazMock = baz as jest.MockedFunction<typeof baz>;

describe("foo", () => {
  beforeEach(() => {
    barMock.mockReturnValue("bar");
    barzock.mockReturnValue("baz");
  });

  it("Should success", () => {
    const res = foo();

    await expect(barMock).toHaveBeenCalled();
  });
});

asClassMock

Provides functionality to infer class mock type and also shortcuts to implement inner object fields and/or properties

see test file for extended usage

AsMocksDemo

Assuming SUT file

import { BarClass } from "./bar";

export function foo() {
  const bar = new BarClass();

  return bar.func1() + " - " + bar.func2();
}

in your spec file

import { asClassMock } from "@jurijzahn8019/jest-mock-inference-helper";
import { foo } from "./foo";
import { BarClass } from "./bar";

// Automock bar
jest.mock("bar");

const BarClassMock = asClassMock(BarClass);

// similar to
const BarClassMock = bar as jest.MockedClass<BarClassMock>;

describe("foo", () => {
  beforeEach(() => {
    BarClassMock.func1.mockReturnValue("bar");
  });

  it("Should success", () => {
    const res = foo();

    await expect(BarClassMock.func1).toHaveBeenCalled();
  });
});

Changelog

Changelog.

License

MIT Lizenz

1.2.52

1 year ago

1.2.53

1 year ago

1.2.54

1 year ago

1.2.41

1 year ago

1.2.42

1 year ago

1.2.40

1 year ago

1.2.45

1 year ago

1.2.46

1 year ago

1.2.43

1 year ago

1.2.44

1 year ago

1.2.49

1 year ago

1.2.47

1 year ago

1.2.48

1 year ago

1.2.50

1 year ago

1.2.51

1 year ago

1.2.38

2 years ago

1.2.39

1 year ago

1.2.34

2 years ago

1.2.35

2 years ago

1.2.33

2 years ago

1.2.36

2 years ago

1.2.37

2 years ago

1.2.27

2 years ago

1.2.28

2 years ago

1.2.26

2 years ago

1.2.29

2 years ago

1.2.30

2 years ago

1.2.31

2 years ago

1.2.32

2 years ago

1.2.16

2 years ago

1.2.17

2 years ago

1.2.18

2 years ago

1.2.19

2 years ago

1.2.20

2 years ago

1.2.23

2 years ago

1.2.24

2 years ago

1.2.21

2 years ago

1.2.22

2 years ago

1.2.25

2 years ago

1.2.9

2 years ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.10

2 years ago

1.2.11

2 years ago

1.2.14

2 years ago

1.2.15

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.16

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.1.9

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago