0.1.7 • Published 1 year ago

@webmangler/testing v0.1.7

Weekly downloads
-
License
Unlicense
Repository
github
Last release
1 year ago

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
InputTypeDescription
stubs.findAllStubImplementation of findAll().
stubs.replaceAllStubImplementation 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
InputTypeDescription
stubs.getEmbedsStubImplementation of getEmbeds().
stubs.getExpressionsStubImplementation of getExpressions().
stubs.getLanguagesStubImplementation 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
InputTypeDescription
stubs.optionsStubImplementation of options().
0.1.7

1 year ago

0.1.6

2 years ago

0.1.4

3 years ago

0.1.5

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago