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

snapshot-fixtures

v1.2.0

Published

Snapshot fixtures for node:test

Downloads

192

Readme

snapshot-fixtures

github actions codecov npm version npm downloads

Snapshot fixtures for node:test.

Table of Contents

Installation

npm install snapshot-fixtures

When to use this?

This package exposes a function for creating tests based on a directory containing test fixtures. This is useful if you want to run tests based on a string input and a string output, especially for multiline content.

Usage

Let’s say you have the following project structure:

my-project/
├── fixtures/
│   ├── fixture-1/
│   │   ├── input.txt
│   │   ├── expected-1.txt
│   │   ├── expected-2.txt
│   │   └── options.json
│   └── fixture-2/
│       ├── input.txt
│       ├── expected-1.txt
│       ├── expected-2.txt
│       └── options.js
└── test/
    └── fixtures.test.js

Then src/fixtures.test.js could look like this:

import { fn } from 'my-project'
import { testFixturesDirectory } from 'snapshot-fixtures'

testFixturesDirectory({
  directory: new URL('../fixtures', import.meta.url),
  tests: {
    'expected-1.txt'(file, options) {
      return fn(file, options)
    },

    'expected-2.txt'(file, options) {
      return fn(file, options, 'additional options')
    }
  }
})

This test asserts that the contents of the expected-1.txt and expected-2.txt tests of each fixture match the respective return value of matching test function.

API

assertEqual(actual, expected[, options])

Assert two strings are equal.

If the strings are not equal, an assertion error will be thrown. This assertion error contains a pretty diff of the two strings.

Arguments

  • actual (string) — The actual value that was produced.
  • expected (string) — The value that was expected.
  • options (object, optional) — An object with additional options. The following options are supported:
    • url (string | URL) — The file URL to include in the assertion error if expected is not equal to actual.

testFixturesDirectory(options)

Create a test suite for a fixtures directory based on node:test. A fixtures directory is a directory that contains other directories. For each of these directories a nested test suite is created. For each of these nested test suites, a test is created based on the tests passed. Each test reads the input file, uses the given test function to generate output, and compares it to the content of the expected output file.

Options

  • directory (string | URL) — The directory containing fixtures as a URL or URL string.
  • prettier (boolean, optional) — If true, format the generated value with Prettier. (Default: false)
  • tests (object) — A mapping of test name to a fixture test.
  • write (boolean) — If true, overwrite the expected content with the actual content. In CI, the output is never written. (Default: false)

Test

A test to run, either as a fixture test object, or as a generate function. The generate function takes a VFile as input, which is read from the input file and return a string that should match the expected output. A second argument options can be accepted. This may contain the options of the fixture. These options are read from the options.cjs, options.js, options.json or options.mjs file in the fixture directory.

A test may be an object with the generate and optional input, expected, and ignore properties. In this case input determines which input file to read, generate serves as the generate function, and expected refers to the expected output file. If ignore is true, the result is written, but assertion failures are ignored.

Compatibility

This project is compatible with Node.js 16 or greater.

License

MIT © Remco Haszing