•  


GitHub - automock/automock: Easier Unit Testing for TypeScript Dependency Injection Frameworks
Skip to content

automock/automock

Logo

Automock

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

npm downloads npm downloads 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.

Quick Example

Take a look at the following example (using Jest, but the same applies for Sinon):

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 using Automock:

import
 {
 TestBed
 }
 from
 '@automock/jest'
;

import
 {
 Database
,
 UserService
 }
 from
 './user.service'
;
 

describe
(
'User Service Unit Spec'
,
 (
)
 =>
 {

  let
 userService
: 
UserService
;
 // ?? Declare the unit under test

  let
 database
: 
jest
.
Mocked
<
Database
>
;
 // ?? Declare a mocked dependency


  beforeAll
(
(
)
 =>
 {

    // ?? Create an isolated test env for the unit (under test) + auto generated mock objects

    const
 {
 unit
,
 unitRef 
}
 =
 TestBed
.
create
(
UserService
)
.
compile
(
)
;
 

    userService
 =
 unit
;


    // ?? Retreive a dependency (mock) from the unit

    database
 =
 unitRef
.
get
(
Database
)
;

  }
)
;


  // ? Test test test

  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.

There is a lot more that Automock does rather than just generating mock objects, we recommend you to read the Getting Started guide to learn more.

↗? For a full Step-by-Step example

?? Installation

To fully integrate Automock into your testing and dependency injection framework, you'll need to install two packages: Automock package for your chosen testing framework, and the corresponding adapter for your DI framework.

  1. Install the corresponding package for your testing framework:
$ npm i -D @automock/jest

For Sinon :

$ npm i -D @automock/sinon
  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

No further configuration is required.

?? License

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

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본