
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
fs-fixture
Advanced tools
Simple API to create disposable test fixtures on disk. Tiny (1.1 kB gzipped) with zero dependencies!
using keywordfs modulefs/promises-compatible APInpm install fs-fixture
import { createFixture } from 'fs-fixture'
// Create a temporary fixture
const fixture = await createFixture({
'package.json': JSON.stringify({ name: 'my-app' }),
'src/index.js': 'console.log("Hello world")'
})
// Read files
const content = await fixture.readFile('src/index.js', 'utf8')
// Cleanup when done
await fixture.rm()
using keywordUses TypeScript 5.2+ Explicit Resource Management for automatic cleanup:
await using fixture = await createFixture({
'config.json': '{ "setting": true }'
})
// Fixture is automatically cleaned up when exiting scope
Already a sponsor? Join the discussion in the Development repo!
From an object:
const fixture = await createFixture({
'package.json': '{ "name": "test" }',
'src/index.js': 'export default () => {}',
'src/utils': {
'helper.js': 'export const help = () => {}'
}
})
From a template directory:
// Copies an existing directory structure
const fixture = await createFixture('./test-templates/basic')
Empty fixture:
// Create an empty temporary directory
const fixture = await createFixture()
File methods (readFile, writeFile, readdir) inherit their type signatures directly from Node.js fs/promises, preserving all overloads and type narrowing behavior.
Read files:
// Read as string (type: Promise<string>)
const text = await fixture.readFile('config.txt', 'utf8')
// Read as buffer (type: Promise<Buffer>)
const binary = await fixture.readFile('image.png')
Write files:
await fixture.writeFile('output.txt', 'Hello world')
await fixture.writeFile('data.bin', Buffer.from([0x89, 0x50]))
JSON operations:
// Write JSON with formatting
await fixture.writeJson('config.json', { port: 3000 })
// Read and parse JSON with type safety
type Config = { port: number }
const config = await fixture.readJson<Config>('config.json')
// Create directories
await fixture.mkdir('nested/folders')
// List directory contents
const files = await fixture.readdir('src')
// Copy files into fixture
await fixture.cp('/path/to/file.txt', 'copied-file.txt')
// Move or rename files
await fixture.mv('old-name.txt', 'new-name.txt')
await fixture.mv('file.txt', 'src/file.txt')
// Check if path exists
if (await fixture.exists('optional-file.txt')) {
// ...
}
Dynamic content with functions:
const fixture = await createFixture({
'target.txt': 'original file',
'info.txt': ({ fixturePath }) => `Created at: ${fixturePath}`,
'link.txt': ({ symlink }) => symlink('./target.txt')
})
Symlinks:
const fixture = await createFixture({
'index.js': 'import pkg from \'pkg\'',
// Symlink individual file or directory
'node_modules/pkg': ({ symlink }) => symlink(process.cwd()),
// Symlink entire directory (useful for sharing node_modules)
node_modules: ({ symlink }) => symlink(path.resolve('node_modules'))
})
Binary files:
const fixture = await createFixture({
'image.png': Buffer.from(imageData),
'generated.bin': () => Buffer.from('dynamic binary content')
})
Path syntax:
const fixture = await createFixture({
// Nested object syntax
src: {
utils: {
'helper.js': 'export const help = () => {}'
}
},
// Or path syntax (creates same structure)
'src/utils/helper.js': 'export const help = () => {}'
})
Pass any fs/promises-compatible API via the fs option to use a virtual filesystem instead of disk:
import { create, MemoryProvider } from '@platformatic/vfs'
import { createFixture } from 'fs-fixture'
const fs = create(new MemoryProvider()).promises
const fixture = await createFixture({
'package.json': JSON.stringify({ name: 'test' }),
'src/index.js': 'export default 42'
}, { fs })
await fixture.readFile('src/index.js', 'utf8') // 'export default 42'
Works with any library that implements the fs/promises API shape, including @platformatic/vfs, the future node:vfs, and memfs.
[!NOTE] With a custom fs, files only exist in that fs instance. Use
fixture.readFile()orfixture.fsto access them —fixture.pathis a virtual path that doesn't exist on the real disk.
[!NOTE] Template directory sources (string paths) are not supported with custom filesystems because most virtual fs implementations lack recursive
cp. Use aFileTreeobject instead.
createFixture(source?, options?)Creates a temporary fixture directory and returns a FsFixture instance.
Parameters:
source (optional): String path to template directory, or FileTree object defining the structureoptions.tempDir (optional): Custom temp directory. Defaults to os.tmpdir()options.templateFilter (optional): Filter function when copying from template directoryoptions.fs (optional): Custom fs/promises-compatible API for virtual filesystem supportReturns: Promise<FsFixture>
const fixture = await createFixture()
const fixture = await createFixture({ 'file.txt': 'content' })
const fixture = await createFixture('./template-dir')
const fixture = await createFixture({}, { tempDir: './custom-temp' })
FsFixture Methods| Method | Description |
|---|---|
fixture.path | Absolute path to the fixture directory |
fixture.fs | The underlying fs/promises API used by the fixture |
getPath(...paths) | Get absolute path to file/directory in fixture |
exists(path?) | Check if file/directory exists |
rm(path?) | Delete file/directory (or entire fixture if no path) |
readFile(path, encoding?) | Read file as string or Buffer |
writeFile(path, content) | Write string or Buffer to file |
readJson<T>(path) | Read and parse JSON file |
writeJson(path, data, space?) | Write JSON with optional formatting |
readdir(path, options?) | List directory contents |
mkdir(path) | Create directory (recursive) |
cp(source, dest?) | Copy file/directory into fixture |
mv(source, dest) | Move or rename file/directory |
type FileTree = {
[path: string]: string | Buffer | FileTree | ((api: Api) => string | Buffer | Symlink)
}
type Api = {
fixturePath: string // Fixture root path
filePath: string // Current file path
getPath: (...paths: string[]) => string // Get path from fixture root
symlink: (target: string) => Symlink // Create a symlink
}
The subset of fs/promises methods that custom filesystem implementations must provide:
type FsPromises = {
// Required
readFile(path: string, options?): Promise<Buffer | string>
writeFile(path: string, data: string | Buffer, options?): Promise<void>
readdir(path: string, options?): Promise<string[] | Dirent[]>
mkdir(path: string, options?): Promise<string | undefined>
rename(oldPath: string, newPath: string): Promise<void>
access(path: string, mode?: number): Promise<void>
// Optional
rm?(path: string, options?): Promise<void>
unlink?(path: string): Promise<void>
rmdir?(path: string): Promise<void>
symlink?(target: string, path: string, type?: string): Promise<void>
cp?(source: string, destination: string, options?): Promise<void>
mkdtemp?(prefix: string): Promise<string>
}
If rm is not available, fs-fixture falls back to recursive removal using readdir({ withFileTypes }) + unlink + rmdir. If mkdtemp is not available, fixture paths are generated with a counter.
Lightweight testing library for Node.js
FAQs
Easily create test fixtures at a temporary file-system path
The npm package fs-fixture receives a total of 6,310 weekly downloads. As such, fs-fixture popularity was classified as popular.
We found that fs-fixture demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.