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

@webmangler/testing

v0.1.7

Published

WebMangler testing utilities

Downloads

5

Readme

WebMangler Testing Utilities

A collection of testing utilities for WebMangler packages and plugins.

Usage

Install @webmangler/testing and its peer-dependencies, e.g.:

npm install @webmangler/testing sinon --save-dev

Import the testing utilities you want to use in you tests, e.g.:

// my-module.test.ts

import type { TestScenario } from "@webmangler/testing";

import { WebManglerPluginMock } from "@webmangler/testing";

suite("My test suite", function() {
  // ...
});

Documentation

The documentation uses Mocha with a Test Driven Development interface for examples as that is the testing style used by the WebMangler project.

Types

The testing utilities provide TypeScript types that can help structure your tests. The following types are available:

TestScenarios

The TestScenarios type provides a common interface for creating parameterized tests.

Example
import type { TestScenarios } from "@webmangler/testing";

import { makeStringLonger } from "../my-module.ts";

suite("My test suite", function() {
  interface TestCase {
    readonly input: string;
    readonly expected: string;
  }

  const scenarios: TestScenarios<TestCase> = [
    {
      testName: "example 1",
      getScenario: () => {
        const original = "foo";
        return {
          input: original,
          expected: `${original}bar`,
        };
      },
    },
    {
      testName: "example 2",
      getScenario: () => {
        const original = "Hello";
        return {
          input: original,
          expected: `${original} world!`,
        };
      },
    },
  ];

  for (const { testName, getScenario } of scenarios) {
    test(name, function() {
      const { input, expected } = getScenario();
      const result = makeStringLonger(input);
      expect(result).to.equal(expected);
    });
  }
});

Mocks

The testing utilities provide Sinon.JS-based mocks of types expected by the WebMangler core. You can use these mocks when your tests need to integrate with the WebMangler core. The following mocks are available:

MangleExpressionMock

A mocked implementation of the MangleExpression type. Can be instantiated with custom findAll() and replaceAll() behaviour if needed.

Example
import { MangleExpressionMock } from "@webmangler/testing";
import * as sinon from "sinon";

let mangleExpression;

// With default implementations of all methods.
mangleExpression = new MangleExpressionMock();

// With custom implementation of the `findAll` method.
const matches = ["foo", "bar"];
const stub = sinon.stub().returns(matches);
mangleExpression = new MangleExpressionMock({ findAll: stub });
Arguments

| Input | Type | Description | | ------------------ | ------ | --------------------------------- | | stubs.findAll | Stub | Implementation of findAll(). | | stubs.replaceAll | Stub | Implementation of replaceAll(). |

WebManglerLanguagePluginMock

A mocked implementation of the WebManglerPluginLanguage type. Can be instantiated with custom getEmbeds(), getExpressions() and getLanguages() behaviour if needed.

Example
import { WebManglerLanguagePluginMock } from "@webmangler/testing";
import * as sinon from "sinon";

let plugin;

// With default implementations of all methods.
plugin = new WebManglerLanguagePluginMock();

// With custom implementation of the `getExpressions` method.
const expressions = new Map();
const stub = sinon.stub().returns(expressions);
plugin = new WebManglerLanguagePluginMock({ getExpressions: stub });
Arguments

| Input | Type | Description | | ---------------------- | ------ | ------------------------------------- | | stubs.getEmbeds | Stub | Implementation of getEmbeds(). | | stubs.getExpressions | Stub | Implementation of getExpressions(). | | stubs.getLanguages | Stub | Implementation of getLanguages(). |

WebManglerPluginMock

A mocked implementation of the WebManglerPlugin type. Can be instantiated with custom options() behaviour if needed.

Example
import { WebManglerPluginMock } from "@webmangler/testing";
import * as sinon from "sinon";

let plugin;

// With default implementations of all methods.
plugin = new WebManglerPluginMock();

// With custom implementation of the `options` method.
const options = { patterns: "foo(bar|baz)" };
const stub = sinon.stub().returns(options);
plugin = new WebManglerPluginMock({ options: stub });
Arguments

| Input | Type | Description | | --------------- | ------ | ------------------------------ | | stubs.options | Stub | Implementation of options(). |