Automock

A meta-framework that focuses on helping developers build solid test suites...

README

Logo

Suites (formerly Automock)


Suites is an opinionated, flexible testing meta-framework aim at elevating the software testing experience within
backend systems. By integrating a wide array of testing tools into a cohesive framework, Suites simplifies the process
of creating reliable tests, thereby ensuring the development of high-quality software.

Automock has been rebranded to Suites 🎉


We are excited to announce that Automock has been rebranded to Suites!

This change reflects our commitment to providing a comprehensive testing solution that caters to a broader range of
testing scenarios. The core features and functionality of the framework remain the same, but with a new name and a fresh
look.


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.

:computer: Quick Example


Suites suggest an alternative approach to writing unit tests for classes instead of using the traditional mocking
libraries and dependency injection frameworks.

Take a look at the following example:

Consider the following UserService and Database classes:

  1. ```typescript
  2. export class Database {
  3.   async getUsers(): Promise<User[]> { ... }
  4. }

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

  7.   async getAllUsers(): Promise<User[]> {
  8.     return this.database.getUsers();
  9.   }
  10. }
  11. ```

Let's create a unit test for this class:

  1. ```typescript
  2. import { TestBed, Mocked } from '@suites/unit';
  3. import { Database, UserService } from './user.service';

  4. describe('User Service Unit Spec', () => {
  5.   let userService: UserService; // 🧪 Declare the unit under test
  6.   let database: Mocked<Database>; // 🎭 Declare a mocked dependency

  7.   beforeAll(async () => {
  8.     // 🚀 Create an isolated test env for the unit (under test) + auto generated mock objects
  9.     const { unit, unitRef } = await TestBed.solitary(UserService).compile();

  10.     userService = unit;

  11.     // 🔍 Retreive a dependency (mock) from the unit reference
  12.     database = unitRef.get(Database);
  13.   });

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

  18.     const users = await userService.getAllUsers();

  19.     expect(database.getUsers).toHaveBeenCalled();
  20.     expect(users).toEqual(mockUsers);
  21.   });
  22. });
  23. ```

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).

:package: Installation


First, install Suites' core package:

  1. ```bash
  2. $ npm i -D @suites/unit
  3. ```

Then, to fully integrate Suites into your mocking and dependency injection frameworks, install the corresponding
adapters for your project. For example, to use Suites with Jest and NestJS you would run (alongside the core package):

  1. ```bash
  2. $ npm i -D @suites/doubles.jest @suites/di.nestjs
  3. ```

Suites will automatically detect the installed adapters and configure itself accordingly.

Supported DI Frameworks


DIPackage
|--------------|------------------------|
NestJS`@suites/di.nestjs`
Inversify`@suites/di.inversify`
TSyringeSoon!

Supported Mocking Libraries


DIPackage
|--------------|--------------------------|
Jest`@suites/doubles.jest`
Sinon`@suites/doubles.sinon`
Vitest`@suites/doubles.vitest`
BunSoon!
DenoSoon!


:scroll: License


Distributed under the Apache (Apache-2.0) License. See LICENSE for more information.