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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@natlibfi/fixura

v5.0.0

Published

Loading test fixtures is as easy as ABC

Readme

Loading test fixtures is as easy as ABC NPM Version

Fixura loads test fixtures.

Workflow status

| Branch | Workflow status | |--------|------------------------------------------------------------------------------------------------------------------------------------------------------------| | main | Workflow status badge for branch main | | test | Workflow status badge for branch test |

Usage

ES modules

The factory takes the fixture root as one or more path components. It returns {getFixture, getFixtures}:

import fixturesFactory from '@natlibfi/fixura';
const {getFixture, getFixtures} = fixturesFactory(import.meta.dirname, '..', 'test-fixtures');

// Load a single fixture
const fixture = getFixture('foo.txt');

// Load all .txt files in one directory
const fixtures = getFixtures(/\.txt$/u, 'some-dir');

You can also pass the root as an options object. See Configuration:

const {getFixture} = fixturesFactory({
    root: [import.meta.dirname, '..', 'test-fixtures'],
});

API

fixturesFactory(...)

Two call shapes:

  • Variadic root components: fixturesFactory(dir, '..', 'test-fixtures')
  • Options object: fixturesFactory({root, reader, failWhenNotFound})

The function returns {getFixture, getFixtures}.

getFixture(components, ...components) | getFixture({components, reader})

Loads a single fixture. The path uses one or more components joined to the root. A per-call reader can override the factory default in the object form.

The function returns a string (TEXT), an object (JSON), or a ReadStream (STREAM). See Readers.

getFixtures(regex, ...dirComponents) | getFixtures(components, ...components)

Loads multiple fixtures. The function always returns an array.

  • With a regex: Lists the directory given by dirComponents. It loads every entry whose file name matches the regex. Matching uses entry names only, not joined paths. It does not recurse into subdirectories. The array can be empty if nothing matches.
  • Without a regex: Loads the single fixture from the path components. It returns the fixture as a one-element array.

Configuration

Readers

The readers are exported as READERS:

import fixturesFactory, {READERS} from '@natlibfi/fixura'

Pass a default reader to the factory:

const {getFixture} = fixturesFactory({
    root: [import.meta.dirname, '..', 'test-fixtures'],
    reader: READERS.JSON
});

Set a fixture-specific reader:

getFixture({components: ['foo', 'bar.json'], reader: READERS.JSON})

Built-in readers

  • TEXT: Returns the fixture as text. This is the default reader.
  • JSON: Parses the fixture as JSON. Returns an object.
  • STREAM: Returns a read stream to the fixture. The stream emits error asynchronously. Always attach an error listener.

CAUTION: failWhenNotFound does not apply to streams. A missing file returns a ReadStream that emits ENOENT as an error event. This is an uncaught exception if no listener exists.

failWhenNotFound

Set failWhenNotFound to false to prevent the function from throwing an error when a fixture file is not found. The function returns undefined instead:

const {getFixture} = fixturesFactory({
    root: [import.meta.dirname, '..', 'test-fixtures'],
    failWhenNotFound: false
});

const foo = getFixture('foo', 'bar.txt'); // undefined

Only ENOENT (file not found) is suppressed. Other read errors, such as invalid JSON, always throw. Streams are not covered.

License and copyright

Copyright (c) 2019-2020, 2022-2026 University Of Helsinki (The National Library Of Finland)

This project uses the MIT license.