npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@automock/jest

v2.1.0

Published

<p align="center"> <img width="200" src="https://raw.githubusercontent.com/automock/automock/master/logo.png" alt="Logo" /> </p>

Downloads

167,071

Readme

Automock optimizes the unit testing process by providing a virtual, isolated environment and automated mock generation, enabling developers to create efficient test suites and enhance their overall testing experience.

Codecov Coverage ci

↗️ Documentation    ↗️ API Reference

Core Features

🚀 Zero-Setup Mocking - Automatically generate mock objects, eliminate manual setup, reduce boilerplate code.

🔍 Type-Safe Mocks - Leverage TypeScript's power with mocks that retain the same type as real objects.

📄 Consistent Tests Structure - Test suites will follow a consistent syntax and structure, making them easier to read and maintain.

📈 Optimized Performance - By bypassing the actual DI container, unit tests run significantly faster.

🌐 Community & Support - Join a growing community of developers.

:package: Installation

To fully integrate Automock into your testing and dependency injection framework, you need to install two packages: @automock/jest, and the corresponding DI framework adapter.

  1. Install Automock's Jest package:
$ npm i -D @automock/jest
  1. And for your DI framework, install the appropriate Automock adapter (as a dev dependency):

| DI Framework | Package Name | |--------------|--------------------------------| | NestJS | @automock/adapters.nestjs | | Inversify | @automock/adapters.inversify |

For example:

$ npm i -D @automock/jest @automock/adapters.nestjs

No further configuration is required.

:computer: Quick Example

Take a look at the following example:

Consider the following UserService class:

export class Database {
  async getUsers(): Promise<User[]> { ... }
}

export class UserService {
  constructor(private database: Database) {}

  async getAllUsers(): Promise<User[]> {
    return this.database.getUsers();
  }
}

Let's create a unit test for this class:

import { TestBed } from '@automock/jest';
import { Database, UserService } from './user.service'; 

describe('User Service Unit Spec', () => {
  let userService: UserService;
  let database: jest.Mocked<Database>;

  beforeAll(() => {
    const { unit, unitRef } = TestBed.create(UserService).compile();
    userService = unit;
    database = unitRef.get(Database);
  });

  test('should return users from the database', async () => {
    const mockUsers: User[] = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }];
    database.getUsers.mockResolvedValue(mockUsers);

    const users = await userService.getAllUsers();

    expect(database.getUsers).toHaveBeenCalled();
    expect(users).toEqual(mockUsers);
  });
});

With the use of the TestBed, an instance of the UserService class can be created with mock objects automatically generated for its dependencies. During the test, we have direct access to the automatically generated mock object for the Database dependency (database). By stubbing the getUsers() method of the database mock object, we can define its behavior and make sure it resolves with a specific set of mock users.

Automock improves upon the existing unit testing procedures of DI frameworks by creating a virtual DI container. There is an array of advantages to this change:

  • Speed: By simulating the actual DI container in the testing environment, Automock speeds up execution times.

  • Efficiency: Developers are therefore able to focus on writing the test logic instead of grappling with the complexities of test setup.

  • Isolation: Each test runs independently with mock implementations automatically provided, creating a streamlined and interference-free testing environment.

:scroll: License

Distributed under the MIT License. See LICENSE for more information.