Skip to main content

TestContainer

The TestContainer class is an isolated Container for tests.

Info

A test framework like Jest, supports parallel test executions in different cores of the machine. Each test is a single test file, whose it() blocks are executed serially.

TestContainer is created with testing framework's parallel execution in mind. A TestContainer should be created in the beforeAll() block of your test, which modifies the Container for the duration of the test. This TestContainer does not affect other tests in parallel since they are on a separate machine core. Once tests are done executing, the afterAll() block cleans up the Container.

Index

Constructors

Methods

Constructors

constructor

Methods

staticcreate

  • create(subjects: TestContainerSubjects, options?: TestContainerOptions): Promise<void>
  • The TestContainer.create() method allows you to mock factories.

    @example
    beforeAll(async () => {
    await TestContainer.create({
    importFrom: [OctoAwsCdkPackageMock],
    mocks: [
    { type: MyClass, value: jest.fn() },
    { metadata: { key: 'value' }, type: AnotherClass, value: new AnotherClass() },
    ],
    }, { factoryTimeoutInMs: 500 });
    });

    Parameters

    • subjects: TestContainerSubjects

      The subjects being mocked.

      • importFrom is an array of IPackageMock classes, to import custom mocks, such as from octo-aws-cdk package - OctoAwsCdkPackageMock.
      • mocks is an array of objects, to override the default factories.
        • Use metadata?: { [key: string]: string } to identify the factory being mocked.
        • Use type: Constructable<T> | string to identify the class being mocked.
        • Use value: T to provide the mocked value.
    • optionaloptions: TestContainerOptions

      Options to configure TestContainer.

      • factoryTimeoutInMs?: number is to override the default container timeout.

    Returns Promise<void>

staticreset

  • reset(): Promise<void>
  • The TestContainer.reset() method will completely destroy all factories from the Container. Because the entire Container is destroyed, and not just the mocks, it must always be called in your afterAll() block.

    @example
    afterAll(async () => {
    await TestContainer.reset();
    });

    Returns Promise<void>