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

@hedia/test

v3.5.2

Published

Tools for testing and reporting

Downloads

5,241

Readme

Test

Tools for testing and reporting.

This package should always be installed as a development dependency wherever it is used.

Utilities

FetchMocker

FetchMocker is a class that helps to mock fetch requests in tests.

After creating an instance of FetchMocker, you can add any number of resources that it should intercept using the addMock method. If a fetch request is made to a resource that has been added to the FetchMocker, the fetch request will be intercepted and the fetch implementation provided in the mock will be used instead. If a fetch request is made to a resource that has not been added to the FetchMocker, the fetch request will be passed through to the real fetch implementation.

FetchMocker's constructor accepts two optional arguments:

  1. testContext: If a test context is provided, the FetchMocker will automatically remove all mocks after the test has run. See the example below. This is useful for ensuring that mocks do not interfere with other tests. If a test context is not provided, the mocks will not be removed automatically, and you will need to remove the mocks manually.
  2. verbose: If verbose is set to true, the mock will log information about the fetch requests it intercepts and the fetch requests it passes through to the real fetch implementation. This can be useful when first setting up a test to see which fetch requests are being made and which are being intercepted. Verbose mode should generally be turned off in the final test, as it can make the test output difficult to read.

The addMock method takes an object with the following properties:

  • resource is the url of the request you want to mock. It can be a string, a URL, or a regular expression. If a string or a URL is provided, the mock will match the url exactly with the resource except for any search query parameters. Search query parameters will be parsed so their order does not matter. Furthermore, all search query parameters given in the resource specification will be required but any extra query parameters used in the fetch call will be ignored. See example below. If a regular expression is provided, the mock will match the url with the resource using the regular expression.
  • fetchImplementation is a function that will be called when a fetch request is made to the resource.
  • method is an optional property that specifies the method of the request you want to mock. If it is not provided, the mock will match any method.
import { FetchMocker } from "@hedia/test/fetchMocker";

it("should fetch data", async (testContext) => {
	// When passing a TestContext to the FetchMocker constructor, the mock will be automatically removed after the test.
	const fetchMocker = new FetchMocker(testContext)
		.addMock({
			resource: "https://api.example.com/data?param1=value1&param2=value2",
			fetchImplementation: () => Response(JSON.stringify({ data: "test" })),
		})
		.addMock({
			resource: "https://api.example.com/data",
			method: "POST", // Optional. By default any method will be matched.
			fetchImplementation: () => Response(JSON.stringify({ error: "not allowed" }), { status: 403 }),
		});

	// Note that the order of search query parameters does not matter.
	// Furthermore, extra query parameters will be ignored.
	const response = await fetch("https://api.example.com/data?param2=value2&param1=value1&param3=value3");
	await fetch("https://api.example.com/data", { method: "POST" });

	// The underlying mock object can be accessed to make assertions about the calls to fetch.
	assert.equal(fetchMocker.mockedFetch.mock.calls, 2);

	// FetchMocker keeps track of which mocks have been used, so we can check if all mocks have been used.
	assert(fetchMocker.allMocksUsed());
});

randomString

Generates a random string of a given length.

import { randomString } from "@hedia/test";

const nameOfThing: string = randomString(10); // "dKS3ThyMAA"

matchObject

The matchObject function is used to compare two objects. It will throw an error if the object in the first argument does not match the object in the second argument, meaning that it has at least the same properties.

import { matchObject } from "@hedia/test";

matchObject([{ a: 1, b: 2 }], [{ a: 1 }]); // Won't throw an error
matchObject([{ a: 1, b: 2 }], [{ a: 1, b: 2 }]); // Won't throw an error
matchObject([{ a: 1 }], [{ a: 1, b: 2 }]); // Will throw an error

Custom Test Reporters

Test Reporter

Format and send test output to the test-service.

node --test --experimental-test-coverage --test-reporter @hedia/test/reporters/test

JSON Reporter

Format and save a test report for use by other services, for example Confluence

node --test --experimental-test-coverage --test-reporter @hedia/test/reporters/json

Sonarcloud Reporter - deprecated

Use Node 22 and lcov instead.

node --test --experimental-test-coverage --test-reporter lcov