0.1.1 • Published 10 months ago

nest-tsb v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

codecov

NestJS Test Suite Builder

The goal of this library is to eliminate the necessity of duplicating setup and teardown procedures in your tests. During the development process, I found that utilizing a custom test runner greatly enhanced the API. However, I prefer to wait for native support of top-level async/await in Jest to avoid potential issues or incompatibilities that may arise in the future.

Getting Started

yarn add nestjs-tsb

Create a fixture

A fixture serves as a container for your shared configuration. Additionally, you can utilize Jest hooks within the fixture to execute top-level operations. However, please keep in mind the behavior of Jest's describe grouping when working with fixtures.

class MyFixture extends TestFixture {
  public constructor() {
    super({
      providers: [MyFakeService, Gateway],
    });
  }

  public override configureProviders = (
    builder: ITestingModuleBuilder): VoidType => 
      builder
        .overrideProvider(Gateway)
        .useClass(FakeGateway);
}

Setup your Test Suite

const myFixture = new MyFixture();
const suite = new TestSuiteBuilder(myFixture).compile();

describe("test suite should", () => {
  test("include the dependencies from the fixture", async () => {
    const service = suite.getProvider(MyFakeService);

    expect(service).toBeDefined();
    expect(service).toBeInstanceOf(MyFakeService);
  });
});

Customize your current Fixture

Please be aware that by using this approach, your other configuration will be overridden.

class CustomizedFixture extends MyFixture {
  public override configureProviders = (
    builder: ITestingModuleBuilder): VoidType => 
      builder
        .overrideProvider(Gateway)
        .useClass(NewGateway);
}

Upcoming Features

The APIs displayed here are not yet finalized; they are provided for contextual purposes.

TestContext extensions

Add metadata to your test context.

Customize your current Fixture through the builder

const myFixture = new MyFixture();
const suite = new TestSuiteBuilder(myFixture)
  .subsitute(Gateway, NewGateway)
  .compile();

Get your overrides from the suite directly

const newGateway = suite.get('gateway')
// or with some magic
const newGateway = suite.newGateway