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

@lou.codes/test

v4.2.3

Published

✅ Equality test with enforced readability

Downloads

29

Readme

Coverage License NPM Version Open Issues Size

✅ Equality test with enforced readability, based on the concept of RITEway and inspired by uvu.

Usage

📦 Node

Install @lou.codes/tests as a dev dependency:

pnpm add -D @lou.codes/test
# or
npm install -D @lou.codes/test
# or
yarn add --dev @lou.codes/test

Add a test script to package.json:

{
	"scripts": {
		"test": "test"
	}
}

To support TypeScript, install tsx as a dev dependency:

pnpm add -D tsx
# or
npm install -D tsx
# or
yarn add --dev tsx

And update package.json:

{
	"scripts": {
		"test": "NODE_OPTIONS='--import tsx' test"
	}
}

To add coverage, install c8 as a dev dependency:

pnpm add -D c8
# or
npm install -D c8
# or
yarn add --dev c8

And update package.json:

{
	"scripts": {
		"test": "c8 test"
	}
}

If you added TypeScript support, then update package.json like this instead:

And update package.json:

{
	"scripts": {
		"test": "NODE_OPTIONS='--import tsx' c8 test"
	}
}

Run tests:

pnpm test
# or
npm test
# or
yarn test

🦕 Deno

Import @lou.codes/test using the npm: prefix, and use it directly:

import { evaluate } from "npm:@lou.codes/test";
import { add } from "../src/add.js";

evaluate({
	given: "a 1 and a 2",
	must: "return 3",
	received: () => add(2)(1),
	wanted: () => 3,
}).then(console.log);

🌎 Browser

Import @lou.codes/test using esm.sh, and use it directly:

<script type="module">
	import { evaluate } from "https://esm.sh/@lou.codes/test";
	import { add } from "../src/add.js";

	evaluate({
		given: "a 1 and a 2",
		must: "return 3",
		received: () => add(2)(1),
		wanted: () => 3,
	}).then(console.log);
</script>

Writing tests

TypeScript

import type { Tests } from "@lou.codes/test";
import { add } from "../src/add.js";

export default [
	{
		given: "a 1 and a 2",
		must: "return 3",
		received: () => add(2)(1),
		wanted: () => 3,
	},
	{
		given: "a 1 and a -2",
		must: "return -1",
		received: () => add(-2)(1),
		wanted: () => -1,
	},
] satisfies Tests<number>;

JavaScript

import { add } from "../src/add.js";

/** @satisfies {import("@lou.codes/test").Tests<number>} */
export default [
	{
		given: "a 1 and a 2",
		must: "return 3",
		received: () => add(2)(1),
		wanted: () => 3,
	},
	{
		given: "a 1 and a -2",
		must: "return -1",
		received: () => add(-2)(1),
		wanted: () => -1,
	},
];

Other alternatives

Instead of exporting an Array of Test as default, the export can also be a single Test:

import type { Test } from "@lou.codes/test";
import { add } from "../src/add.js";

export default {
	given: "a 1 and a 2",
	must: "return 3",
	received: () => add(2)(1),
	wanted: () => 3,
} satisfies Test<number>;

Or multiple exports with different tests:

import type { Test } from "@lou.codes/test";
import { add } from "../src/add.js";

export const test1: Test<number> = {
	given: "a 1 and a 2",
	must: "return 3",
	received: () => add(2)(1),
	wanted: () => 3,
};

export const test2: Test<number> = {
	given: "a 1 and a -2",
	must: "return -1",
	received: () => add(-2)(1),
	wanted: () => -1,
};

It can also be used directly without the test bin by importing the different utils directly (like with the Deno and Browser examples above):

import { evaluate } from "@lou.codes/test";
import { customFormatter } from "./customFormatter.js";

evaluate({
	given: "a 1 and a 2",
	must: "return 3",
	received: () => add(2)(1),
	wanted: () => 3,
}).then(customFormatter);

Default output

@lou.codes/tests provides a default output for the tests. It looks like this:

❯ ./tests/example.test.ts
× Given a 1 and a 2, must return 3, but…
└ it has the wrong value. Wanted 3 but received 4.

And if the wanted/received type is more complex, like an object, then the output goes into details about the error:

❯ ./tests/example.test.ts
× Given an object, must add a single property, but…
├ foo.bar has the wrong value. Wanted 1 but received 2.
├ foo.baz.1 is missing.
└ bar was set with the value "bar".

But developers can choose to run test directly and use their own formatter, as it was pointed out in the previous section.

Useful links