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

@aesop-fables/containr-testing

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aesop-fables/containr-testing

Testing support (utilities, pattern examples) for containr

  • 0.3.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

@aesop-fables/containr-testing

containr-testing provides testing support when using containr for development.

Installation

npm install @aesop-fables/containr-testing
yarn add @aesop-fables/containr-testing

Example

One of the most common testing patterns (when using dependency injection) leverages mocks and/or stubs (we're generalizing quite a bit here). containr-testing provides the InteractionContext<T> class that allows you to automatically generate mocks for your dependencies using jest-mock-extended.

// MyEntityService.ts
import { inject } from '@aesop-fables/containr';

export interface IEvent {
    correlationId: string;
}

export interface IEventPublisher {
    publish(event: IEvent): Promise<void>;
}

export const MyServices = {
    Publisher: 'publisher',
};

export class MyEntityService implements IEntityService {
    constructor(
        @inject(MyServices.Publisher) private readonly publisher: IEventPublisher,
    ) {}

    createEntity(name: string): Promise<void> {
        const entity = {
            id: 'you would probably generate this',
            name,
        };

        await this.publisher.push({
            correlationId: entity.id,
        });
    }
}

// MyEntityService.test.ts
import { IEventPublisher, MyEntityService, MyServices } from './MyEntityService';
import { createInteractionContext, InteractionContext } from '@aesop-fables/containr-testing';

describe('InteractionContext example', () => {
  let context: InteractionContext<MyEntityService>;

  beforeEach(() => {
    // This will automatically configured the context
    context = createInteractionContext(MyEntityService);

    // If you need to modify the underlying collection
    // You can do it - only if you do it BEFORE you call `context.classUnderTest`

    // Calls to `context.services` will reset the instance of classUnderTest as well as the underlying service container (if it's already been instantiated)
  });

  test('demo auto-mocking', async () => {
    // classsUnderTest is lazily evaluated and is only instantiated once (the first time you call the property)
    await context.classUnderTest.createEntity('Hello');

    // the context allows you to retrieve the mocks
    // You could obviously inspect the invocation and do more elaborate assertions
    expect(context.mockFor<IEventPublisher>(MyServices.Publisher).publish).toBeCalledTimes(1);
  });
});


FAQs

Package last updated on 08 Jun 2023

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