All Projects → omermorad → automock

omermorad / automock

Licence: MIT License
A library for testing classes with auto mocking capabilities using jest-mock-extended

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to automock

Sinon Jest Cheatsheet
Some examples on how to achieve the same goal with either of both libraries: sinon and jest. Also some of those goals achievable only by one of these tools.
Stars: ✭ 226 (+769.23%)
Mutual labels:  mock, unit-testing, jest, mocking
Moka
A Go mocking framework.
Stars: ✭ 53 (+103.85%)
Mutual labels:  mock, tdd, mocking
Graphql Query Test Mock
Easily mock GraphQL queries in your Relay Modern / Apollo / any-other-GraphQL-client tests.
Stars: ✭ 49 (+88.46%)
Mutual labels:  mock, jest, mocking
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+7323.08%)
Mutual labels:  mock, tdd, mocking
Enzyme Matchers
Jasmine/Jest assertions for enzyme
Stars: ✭ 881 (+3288.46%)
Mutual labels:  unit-testing, jest, tdd
Firebase Mock
Firebase mock library for writing unit tests
Stars: ✭ 319 (+1126.92%)
Mutual labels:  mock, unit-testing, mocking
Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (+284.62%)
Mutual labels:  mock, unit-testing, mocking
Js Unit Testing Guide
📙 A guide to unit testing in Javascript
Stars: ✭ 1,346 (+5076.92%)
Mutual labels:  unit-testing, tdd, unit-test
Pester
Pester is the ubiquitous test and mock framework for PowerShell.
Stars: ✭ 2,620 (+9976.92%)
Mutual labels:  mock, tdd, mocking
Python Mocket
a socket mock framework - for all kinds of socket animals, web-clients included
Stars: ✭ 209 (+703.85%)
Mutual labels:  mock, tdd, mocking
Jasmine Matchers
Write Beautiful Specs with Custom Matchers for Jest and Jasmine
Stars: ✭ 552 (+2023.08%)
Mutual labels:  unit-testing, jest, tdd
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+169.23%)
Mutual labels:  mock, mocking, unit-test
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+5069.23%)
Mutual labels:  mock, unit-testing, mocking
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+588.46%)
Mutual labels:  mock, unit-testing, mocking
EntityFrameworkCore.AutoFixture
A library aimed to minimize the boilerplate required to unit-test Entity Framework Core code using AutoFixture and in-memory providers.
Stars: ✭ 31 (+19.23%)
Mutual labels:  unit-testing, mocking, unit-test
umock-c
A pure C mocking library
Stars: ✭ 29 (+11.54%)
Mutual labels:  mock, unit-testing, mocking
angular-unit-testing-examples
Showroom for different Angular unit testing concepts
Stars: ✭ 19 (-26.92%)
Mutual labels:  unit-testing, jest
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (+53.85%)
Mutual labels:  jest, mocking
jest-how-do-i-mock-x
Runnable examples for common testing situations that often prove challenging
Stars: ✭ 63 (+142.31%)
Mutual labels:  mock, jest
mountebank-api-php
Working with mountebank api it's easy!
Stars: ✭ 17 (-34.62%)
Mutual labels:  mock, mocking

ISC license npm version Codecov Coverage ci


Logo

AutoMock

Standalone Library for Dependencies Auto Mocking (for TypeScript)

Works with any testing framework!

Create unit test simply and easily with 100% isolation of class dependencies

Installation

💡 Install jest-mock-extended as a peer dependency

With NPM:

npm i -D jest-unit jest-mock-extended

Or with Yarn:

yarn add -D jest-unit jest-mock-extended

Who can use this library? 🤩

TL;DR

If you are using this pattern in your framework (it doesn't matter which one):

export class AwesomeClass {
  public constructor(private readonly dependecy1: SomeOtherClass) {}
}

You can use Jest Unit :)

Tell me more 🤔

If you are using any TypeScript framework: Angular, React+TypeScript, NestJS, TypeDI, TsED or even if you are framework free, jest unit is for you.

Jest Unit is framework agnostic, so it's basically serves everyone! if you are using any implementation of dependency inversion (dependency injection on most cases) this library is for you.

The only assumption/requirement is that you are taking your class dependencies via the class constructor (like in the example above).

What is this library

This package helps isolate the dependencies of any given class, by using a simple reflection mechanism on the class constructor params metadata. When used in conjunction with jest-mock-extended library, all the class dependencies (constructor params) will be overridden automatically and become mocks (or deep mocks if you want it to).

Example and Usage 💁‍

import { DeepMockOf, MockOf, Spec } from 'jest-unit';

describe('SomeService Unit Test', () => {
  let someService: SomeService;
  let logger: MockOf<Logger>;
  let userService: MockOf<UserService>;

  const USERS_DATA = [{ name: 'user', email: '[email protected]' }];

  beforeAll(() => {
    const { unit, unitRef } = Spec.createUnit<SomeService>(SomeService)
      .mock(FeatureFlagService)
      .using({
        isFeatureOn: () => Promise.resolve(true),
      })
      // All the rest of the dependencies will be mocked
      // Pass true if you want to deep mock all of the rest
      .compile();

    someService = unit;
    userService = unitRef.get(UserService);
  });

  describe('When something happens', () => {
    beforeAll(() => (userService.getUsers.mockResolvedValueOnce(USERS_DATA));
    
    test('then check something', async () => {
      const result = await service.doSomethingNice();

      expect(logger.log).toHaveBeenCalledWith(USERS_DATA);
      expect(result).toEqual(USERS_DATA);
    });
  });
});
📄 Show me the source

@Reflectable()
export class SomeService {
  public constructor(
    private readonly logger: Logger,
    private readonly catsService: CatsService,
    private readonly userService: UserService,
    private readonly featureFlagService: FeatureFlagService,
  ) {}
  
  public async doSomethingNice() {
    if (this.featureFlagService.isFeatureOn()) {
      const users = await this.userService.getUsers('https://example.com/json.json');
      this.logger.log(users);

      return users;
    }
    
    return null;
  }
}


What is this @Reflectable() decorator?

In order to reflect the constructor class params it needs to be decorated with any class decorator, no matter what its original functionality. If you are not using any kind of decorator, you can just use the default decorator that does, literally, nothing; his purpose is to emit class metadata; so no w

But, for example, if you do use @Injecatable() (NestJS or Angular), @Service() (TypeDI), @Component() or any kind of decorator, you don't need to decorate your class with the @Reflectable() decorator.

Still need further example? Jump to full sample 📄

Motivation 💪

Unit tests exercise very small parts of the application in complete isolation.
"Complete isolation" means that, when unit testing, you don’t typically connect your application with external dependencies such as databases, the filesystem, or HTTP services. That allows unit tests to be fast and more stable since they won’t fail due to problems with those external services. (Thank you, Testim.io - jump to source)

More about jest-mock-extended package 📦

jest-mock-extended is a library which enables type safe mocking for Jest with TypeScript. It provides a complete Typescript type safety for interfaces, argument types and return types and has the ability to mock any interface or object.

License 📜

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

Acknowledgements 📙

jest-mock-extended

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].