fixturez
raw JSON → 1.1.0 verified Fri May 01 auth: no javascript
Easily create and maintain test fixtures in the file system. Version 1.1.0 is the latest stable release. It allows placing fixtures in any parent directory, finding them by name via upward search, copying into temporary directories, and auto-cleanup on process exit. Differentiators: supports multiple test frameworks (Jest, AVA, Mocha), fixture sharing between tests, and customizable glob patterns to locate fixture directories.
Common errors
error Error: Could not find fixture "file" ↓
cause f.find('file') called but the fixture name must include the file extension.
fix
Use f.find('file.txt') with the full basename including extension.
error TypeError: fixtures is not a function ↓
cause Importing fixturez incorrectly using destructuring or as a class.
fix
Use default import: const fixtures = require('fixturez'); or import fixtures from 'fixturez';
Warnings
gotcha fixturez searches up the directory tree from __dirname; if no fixture directory is found, it will throw an error. ↓
fix Ensure a 'fixtures' or '__fixtures__' directory exists in a parent directory, or set 'root' option.
deprecated Options 'glob' and 'root' may be removed in future versions. ↓
fix Use the current options as documented; future API might change.
gotcha The 'copy' method copies the fixture into a new temporary directory each invocation; the path changes each time. ↓
fix Call f.find() if you only need the original path without copying.
Install
npm install fixturez yarn add fixturez pnpm add fixturez Imports
- fixturez wrong
const { fixturez } = require('fixturez')correctimport fixturez from 'fixturez' - fixturez wrong
const fixtures = require('fixturez').defaultcorrectconst fixtures = require('fixturez') - fixturez wrong
const f = new fixtures(__dirname)correctconst f = fixtures(__dirname)
Quickstart
const test = require('ava');
const fixtures = require('fixturez');
const f = fixtures(__dirname, { root: __dirname });
test('find fixture', t => {
const filePath = f.find('sample.txt');
t.truthy(filePath);
});
test('copy fixture', t => {
const tmpDir = f.copy('sample');
t.truthy(tmpDir);
f.cleanup();
});
test('temp directory', t => {
const tmpDir = f.temp();
t.truthy(tmpDir);
f.cleanup();
});