Socket
Book a DemoInstallSign in
Socket

loud-mock

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loud-mock

A typesafe Mock that always explodes to avoid forgetting to explicitly mock methods.

1.1.2
latest
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

Loud Mock

A mock tool that helps you not to forget.

When adding tests to a project you might need to mock a dependency of the object under test.

However, it's easy to forget to mock a method or a property, when you should have provided at least a dummy implementation: Either you care about the return value, in which case you should have provided one, or you care about the call happening, in which case you should remember to check the call.

Usage

Whenever you need to construct a mock for an object, just call loudMock<T>(). Optionally, you can pass an object containing initial values.


describe("The OUT", () => {
    it("Depends on some API call but I forgot to mock it", async () => {
        const mockedApi = createMock<SomeApi>()
        const service = new SomeService(mockedApi);

        await service.doWork(); // Oops! We forgot to mock the call.

        // It will throw an exception reminding you that `realCall` hasn't
        // been mocked, helping you find the issue

        expect(mockedApi.realCall).toHaveBeenCalled()
    });

    it("Depends on some API call but I remembered to mock it", async () => {
        const mockedApi = createMock<SomeApi>()
        const service = new SomeService(mockedApi);

        mockedApi.realCall = jest.fn(); // Just add a way to check the mock

        await service.doWork();

        expect(mockedApi.realCall).toHaveBeenCalled()
    });
})

For Nest.js

When creating your Testing Module, add useMocker:

const myTestModule = Test.createTestingModule({
    imports: [RealModule],
    controllers: [SomeController],
    providers: [TheService]
})
    .useMocker((injectionToken) =>
        createMock({ name: injectionToken?.toString()})
    )

And you'll see exactly what you need to mock.

Installation

Just add a dependency as you would in your package manager of choice:

$ npm install -D loud-mock
$ yarn add -D loud-mock
$ pnpm install -D loud-mock

License

LoudMock is licensed under the LGPL v2.1 License.

Don't worry! It doesn't force you to make your project (L)GPL, unless you create a derivative of this library.

Author

Guillermo O. «Tordek» Freschi

Keywords

jest

FAQs

Package last updated on 03 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.