npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nifty-lil-tricks/testing-plugin-nestjs

v0.6.0

Published

A nifty li'l plugin for setting up NestJS applications when testing

Downloads

391

Readme

nifty_lil_tricks_testing/plugin_nestjs

Note: this package is currently a work in progress

Latest Version GitHub License Buy us a tree codecov

A nifty li'l plugin for setting up PostgreSQL database instances when testing

Installation

Note: this package works with TypeScript v5 or later

Deno

import { nestJsPlugin } from "@nifty-lil-tricks/testing-plugin-nestjs";

Node.js

npm install @nifty-lil-tricks/testing-plugin-nestjs

TypeScript

The TypeScript tsconfig.json must contain the following recommended settings:

{
  "compilerOptions": {
    "target": "ES2022",
    "strict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}

Features

The following features are supported:

  • Setup a NestJS server for testing.
  • Override the NestJS server providers.
  • Override the NestJS server modules.

Quick start

Setup a NestJS server as follows:

// Copyright 2023-2023 the Nifty li'l' tricks authors. All rights reserved. MIT license.

import { assertEquals } from "testing/asserts.ts";
import {
  afterEach,
  beforeEach,
  describe,
  it,
} from "testing/bdd.ts";
import {
  setupTestsFactory,
  type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";
import {
  nestJsPlugin,
  type PluginConfig,
} from "@nifty-lil-tricks/testing-plugin-nestjs";
import { Controller, Get, Module } from "npm:@nestjs/common@^10.2.7";

// In another file, load plugins as follows to generate a setupTests function:
const { setupTests } = setupTestsFactory({ server: nestJsPlugin });

// In another file, define a NestJS app as follows:

@Controller()
export class BasicAppController {
  @Get("/hello")
  getHello(): string {
    return "Hello, world!";
  }
}

@Module({
  imports: [],
  controllers: [BasicAppController],
})
export class BasicAppModule {}

// Then one can use this in any test file as follows:
describe("Service", () => {
  let teardownTests: SetupTestsTeardown;
  let origin: string;

  beforeEach(async () => {
    // Setup tests with configured plugins
    const result = await setupTests({
      server: {
        appModule: BasicAppModule,
      } as PluginConfig,
    });
    teardownTests = result.teardownTests;
    origin = result.outputs.server.output.origin;
  });

  afterEach(async () => {
    // Teardown tests to restore environment after tests have run
    await teardownTests();
  });

  describe("method", () => {
    it("should test something that relies on the nestjs plugin", async () => {
      // Arrange & Act
      const response = await fetch(new URL("/hello", origin));

      // Assert
      assertEquals(response.status, 200);
      assertEquals(await response.text(), "Hello, world!");
    });
  });
});

With overrides

One can also use define a NestJS app with testing overrides to allow one to mock out dependencies where needed. For example, module overrides:

// Copyright 2023-2023 the Nifty li'l' tricks authors. All rights reserved. MIT license.

import { assertEquals } from "testing/asserts.ts";
import {
  afterEach,
  beforeEach,
  describe,
  it,
} from "testing/bdd.ts";
import {
  setupTestsFactory,
  type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";
import {
  nestJsPlugin,
  type PluginConfig,
} from "@nifty-lil-tricks/testing-plugin-nestjs";
import { Controller, Get, Module } from "npm:@nestjs/common@^10.2.7";

// In another file, load plugins as follows to generate a setupTests function:
const { setupTests } = setupTestsFactory({ server: nestJsPlugin });

// In another file, define a NestJS app as follows:

@Controller()
export class BasicAppController {
  @Get("/hello")
  getHello(): string {
    return "Hello, world!";
  }
}

@Module({
  imports: [],
  controllers: [BasicAppController],
})
export class BasicAppModule {}

// In another file, define a NestJS app overrides for testing as follows:
@Controller()
class NewAppController {
  @Get("/hello")
  getHello(): string {
    return "Ahoy!";
  }
}
@Module({
  controllers: [NewAppController],
})
class NewModule {}

// Then one can use this in any test file as follows:
describe("Service", () => {
  let teardownTests: SetupTestsTeardown;
  let origin: string;

  beforeEach(async () => {
    // Setup tests with configured plugins
    const result = await setupTests({
      server: {
        appModule: BasicAppModule,
        modules: [{
          moduleToOverride: BasicAppModule,
          newModule: NewModule,
        }],
      } as PluginConfig,
    });
    teardownTests = result.teardownTests;
    origin = result.outputs.server.output.origin;
  });

  afterEach(async () => {
    // Teardown tests to restore environment after tests have run
    await teardownTests();
  });

  describe("method", () => {
    it("should test something that relies on the nestjs plugin", async () => {
      // Arrange & Act
      const response = await fetch(new URL("/hello", origin));

      // Assert
      assertEquals(response.status, 200);
      assertEquals(await response.text(), "Ahoy!");
    });
  });
});

API

The API Docs can be found here.

Examples

Examples can be found here.

Support

| Platform Version | Supported | Notes | | ---------------- | ------------------ | -------------------------- | | Deno v1 | :white_check_mark: | | | Node.JS v18 | :white_check_mark: | TypeScript v5+ for typings | | Node.JS v20 | :white_check_mark: | TypeScript v5+ for typings | | Web Browsers | :x: | Coming soon |

Useful links

License

Nifty li'l tricks nestjs package is 100% free and open-source, under the MIT license.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Contributions

Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!