Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-mock-extended

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-extended

Type safe mocking extensions for jest

  • 2.0.2-beta1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
400K
decreased by-54.96%
Maintainers
1
Weekly downloads
 
Created

What is jest-mock-extended?

The jest-mock-extended package is a utility for creating strongly typed mocks in Jest. It allows you to create mocks, partial mocks, and deep mocks with type safety, ensuring that your tests are more reliable and maintainable.

What are jest-mock-extended's main functionalities?

Creating a simple mock

This feature allows you to create a simple mock of an interface or class. The mock object will have all the methods of the interface or class, and you can specify return values for these methods.

const { mock } = require('jest-mock-extended');

interface MyService {
  doSomething(): string;
}

const myServiceMock = mock<MyService>();
myServiceMock.doSomething.mockReturnValue('mocked value');

console.log(myServiceMock.doSomething()); // Output: 'mocked value'

Creating a partial mock

This feature allows you to create a partial mock of an interface or class. You can specify implementations for some methods, while the rest will be undefined.

const { mockPartial } = require('jest-mock-extended');

interface MyService {
  doSomething(): string;
  doSomethingElse(): number;
}

const myServicePartialMock = mockPartial<MyService>({
  doSomething: () => 'partial mock value'
});

console.log(myServicePartialMock.doSomething()); // Output: 'partial mock value'
console.log(myServicePartialMock.doSomethingElse()); // Output: undefined

Creating a deep mock

This feature allows you to create a deep mock of an interface or class. All nested properties and methods will be mocked, allowing you to specify return values for deeply nested methods.

const { mockDeep } = require('jest-mock-extended');

interface MyService {
  nestedService: {
    doSomething(): string;
  };
}

const myServiceDeepMock = mockDeep<MyService>();
myServiceDeepMock.nestedService.doSomething.mockReturnValue('deep mock value');

console.log(myServiceDeepMock.nestedService.doSomething()); // Output: 'deep mock value'

Other packages similar to jest-mock-extended

FAQs

Package last updated on 09 Aug 2021

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc