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

@shigen/test

v0.2.1

Published

Tools for testing

Downloads

7

Readme

Test tools

NPM Version

A package for (potentially) various tools for testing. Each tool will have its own subpath for importing.

The minimum Node.js version supported is 18.18.0.

@shigen/test/fs

Create file system fixtures in a random temporary directory. This tool is inspired by and similar to fs-fixture but provides all methods from node:fs/promises relative to the fixture's directory.

Example

import assert from 'node:assert/strict';
import test from 'node:test';

import type { PackageJson } from 'type-fest';
import { createFixture, jsonData, textData };

test('fs fixture', async () => {
	// supports the explicit resource management proposal
	// note that this requires transpilation via Babel or TS 5.2+
	await using fixture = await createFixture({
		// if using TS a type argument can be provided to jsonData for auto-completion and strictness
		'package.json': jsonData<PackageJson>({
			name: 'test-package',
			version: '1.0.0',
		}),
		// directories are created by nesting objects ...
		'src': {
			'a.js': textData(`console.log('hi from a');`),
			'b.js': textData(`console.log('hi from b');`),
		},
		// ... or by writing the path
		'src/c.js': textData(`console.log('hi from c');`),
	});

	assert(await fixture.exists('package.json'));
	assert(await fixture.exists('src/a.js'));
	assert.equal(
		await fixture.fs.readFile('src/a.js', 'utf-8'),
		`console.log('hi from a');`,
	);
	// ...

	// unnecessary if initialized with await using
	await fixture.remove();
});

API

textData(contents: string, encoding?: BufferEncoding): Buffer

Semantic wrapper for Buffer.from. Useful for populating a DataTree input alongside the jsonData function.

jsonData<T extends Jsonifiable>(data: T): Buffer

Create UTF-8 encoded JSON data from input. The Jsonifiable type is imported from type-fest and matches all valid JSON data types or objects with a toJSON method. type-fest provides other useful types like PackageJson or TsConfigJson that can be used explicitly as type argument to provide auto-completion and validation for known properties.

createFixture(source: string | DataTree): Promise<Fixture>

Create fixture at a randomely unique path inside the OS's default temporary directory. The fixture can be populated in multiple ways:

  • Passing a path to a template directory will copy the templates contents recursively into the fixture's directory.
  • Passing a DataTree object with the defined directory structure and file contents.
  • Using the fixture's fs methods to write the files directly after creation.

The fixture can be initialized with the await using syntax from the Explicit Resource Management proposal. This syntax is currently supported by transpiling with Babel or TypeScript 5.2+. A polyfill is not needed since the supported Node.js versions already ship one.

DataTree

A recursive data structure that stores Buffers to write to specified file paths.

type DataTree = {
	[key: string]: Buffer | DataTree;
}

FsProxy

Proxies calls to node:fs/promises to be relative to the fixture's root. Escaping the root with relative paths (..) is not allowed and will result in an error being thrown. In contrast to Node's native fs module, all path arguments must be given as strings. The FsProxy type reflects this restriction. @types/node is defined as an optional peer dependency and needs to be installed for the typings to work correctly.

Fixture

Instances of the Fixture class manage access to the fixture on the file system. Shell commands are executed from the fixture's directory with execa's script interface like this:

await fixture.run`npm install`;
class Fixture {
	/** The fixture's UUID */
	readonly id: string;

	/** Absolute path on file system */
	readonly path: string;

	/** Access to the file system */
	readonly fs: FsProxy;

	/** Run shell commands inside the fixture's directory with execa's script interface */
	readonly run: Execa$;

	/** Convenience wrapper around fs.access */
	exists(path?: string): Promise<Boolean>;

	/** Remove the fixture's directory from file system */
	remove(): Promise<void>;
}