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

v0.6.0

Published

A selection of useful utilities (or nifty li'l tricks!) for all things testing

Downloads

397

Readme

Nifty li'l tricks Logo

nifty_lil_tricks_testing

Note: this package and selected plugins are currently a work in progress

Latest Version GitHub License Buy us a tree codecov

A selection of useful utilities (or nifty li'l tricks!) for all things testing.

Installation

Note: this package works with TypeScript v5 or later

Deno

import * as testing from "deps";

Node.js

npm install @nifty-lil-tricks/testing

TypeScript

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

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

Note, check the plugin READMEs for any additional TypeScript settings.

Features

The following features are supported

  • An extensible setup tests factory that allows one to declaratively setup a test suite for testing with loaded plugins through a simple to use interface.
  • Teardown functionality for restoring the state of the environment after tests have run.
  • Ready-made plugins to get started with straight-away.

Plugins

The following plugins are available to that make use of the setup tests plugin system.

| Plugin | Description | Status | Npm | Docs | | ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ | | PostgreSQl | Setup the World's Most Advanced Open Source Relational Database for testing. It has the following features: Setup a Postgresql server in Docker for testing.Setup an existing Postgresql server for testing.Run migrations on the configured Postgresql server.Seed the configured Postgresql server with data. | ✅ | Latest Version | Docs. | | NestJS Server | Setup a progressive Node.js framework for building efficient, reliable and scalable server-side applications for testing. | ✅ | Latest Version | Docs | | Express Server | Setup a minimal and flexible Node.js web Express application for testing. | 🚧 | Latest Version | Docs |

Setup tests

Quick start

import {
  afterEach,
  beforeEach,
  describe,
  it,
} from "testing/bdd.ts";
import { assertEquals } from "testing/asserts.ts";
import {
  setupTestsFactory,
  type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";

// Define or import a plugin as follows:
const helloWorldPlugin = {
  setup: (config: { message: string }) => {
    // Setup plugin according to config
    return {
      output: config,
      teardown: () => {},
    };
  },
};

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

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

  beforeEach(async () => {
    // Setup tests with configured plugins
    const result = await setupTests({
      helloWorld: { message: "Hello, world!" },
    });
    message = result.outputs.helloWorld.output.message;
    teardownTests = result.teardownTests;
  });

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

  describe("method", () => {
    it("should test something that relies on the plugin being configured", () => {
      // Some other testing
      assertEquals(message, "Hello, world!");
    });
  });
});

Setup tests overview

setupTestsFactory

One can use the setupTestsFactory to register or load defined plugins. It is only needed to load these once so it is recommended to share the returned setupTests function across all test files.

Each plugin must be given a name that is not one of the reserved plugin names. This returns a function that when run, sets up the tests to use the loaded plugins according to the provided config.

Each plugin must contain the following functions in order to be correctly loaded:

  • setup
  • teardown

An example of a plugin is as follows:

import {
  type Plugin,
} from "@nifty-lil-tricks/testing";

interface HelloWorldConfig {
  message: string;
}

type HelloWorldResult = string;

const helloWorldPlugin: Plugin<HelloWorldConfig, HelloWorldResult> = {
  setup(config: HelloWorldConfig) {
    // Setup plugin according to config
    return {
      output: config.message,
      teardown() {
        // Teardown any setup resources
      },
    };
  },
};
setupTests

One can use the returned setupTests function use the loaded plugins.

The loaded plugins available to the setupTests function returned from the factory can be configured using config namespaced to the name of the plugin.

For example, if the plugin is named helloWorld, then the config for that plugin must be provided under the helloWorld namespace.

When run, setupTests will return an object with the data returned from the plugin invocation. The data will be namespaced to the plugin name. For example:

import {
  setupTestsFactory,
  type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";

// Define or import a plugin as follows:
const helloWorldPlugin = {
  setup: (config: { message: string }) => {
    // Setup plugin according to config
    return {
      output: config.message,
      teardown: () => {
        // Teardown any setup resources
      },
    };
  },
};

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

const result = await setupTests({
  helloWorld: { message: "Hello, world!" },
});

result.outputs.helloWorld.output; // "Hello, world!"

Only plugins that are configured will be run. If a plugin is not configured, then it will not be run. The order of the plugins in the config is defined the order in which they defined in the config object. This follows the rules as defined here.

The returned object will also contain a teardown function that when run, will teardown the plugins in the reverse order that they were setup.

teardownTests

One can use the returned teardownTests function to restore the environment to its original state after the tests have run.

For example:

import {
  setupTestsFactory,
  type SetupTestsTeardown,
} from "@nifty-lil-tricks/testing";

// Define or import a plugin as follows:
const helloWorldPlugin = {
  setup: (config: { message: string }) => {
    // Setup plugin according to config
    return {
      output: config.message,
      teardown: () => {
        // Teardown any setup resources
      },
    };
  },
};

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

const result = await setupTests({
  helloWorld: { message: "Hello, world!" },
});

// Teardown tests to restore environment after tests have run
await result.teardownTests();

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 packages are 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!