Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hedia/test

Package Overview
Dependencies
Maintainers
0
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hedia/test

Tools for testing and reporting

  • 3.5.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8K
increased by192.74%
Maintainers
0
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 19 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc