What is fixturify?
Fixturify is an npm package that allows you to create and manipulate file system fixtures programmatically. It is particularly useful for testing purposes, where you need to set up a specific file system state before running tests.
What are fixturify's main functionalities?
Create File System Fixtures
This feature allows you to create a directory structure with files and content programmatically. The `writeSync` method writes the specified files and directories to the file system.
const fixturify = require('fixturify');
const files = {
'index.js': 'console.log("Hello, world!");',
'lib': {
'module.js': 'module.exports = {};'
}
};
fixturify.writeSync('my-fixture', files);
Read File System Fixtures
This feature allows you to read a directory structure and its contents into a JavaScript object. The `readSync` method reads the specified directory and returns an object representing the file system structure.
const fixturify = require('fixturify');
const files = fixturify.readSync('my-fixture');
console.log(files);
Update File System Fixtures
This feature allows you to update an existing directory structure with new files or changes to existing files. The `writeSync` method can be used to overwrite or add new files to the specified directory.
const fixturify = require('fixturify');
const updates = {
'index.js': 'console.log("Updated content");',
'lib': {
'newModule.js': 'module.exports = { new: true };'
}
};
fixturify.writeSync('my-fixture', updates);
Other packages similar to fixturify
mock-fs
Mock-fs is a package that allows you to mock the file system in Node.js. It is useful for testing purposes where you need to simulate different file system states. Unlike fixturify, which creates real files and directories, mock-fs creates an in-memory file system.
memfs
Memfs is a package that provides an in-memory file system that can be used for testing and other purposes. It is similar to mock-fs in that it does not create real files on the disk, but instead simulates a file system in memory. This can be useful for performance reasons and for tests that should not affect the real file system.
fs-extra
Fs-extra is a package that extends the native Node.js file system module with additional methods for working with the file system. It includes methods for copying, moving, and removing files and directories, as well as reading and writing JSON files. While it does not provide the same fixture creation capabilities as fixturify, it offers a wide range of file system utilities.
node-fixturify
Convert JSON objects into directory structures on the file system, and back
again. This package is primarily useful for writing tests.
Installation
yarn add fixturify
Usage
const fs = require('fs')
const fixturify = require('fixturify')
const obj = {
'foo.txt': 'foo.txt contents',
'subdir': {
'bar.txt': 'bar.txt contents'
}
}
fixturify.writeSync('testdir', obj)
fixturify.readSync('testdir')
fixturify.readSync('testdir', { include: ['foo*'] })
fixturify.readSync('testdir', { exclude: ['foo*'] })
fixturify.writeSync('testDir', {
'subdir': { 'bar.txt': null }
})
fixturify.readSync('testdir')
fixturify.writeSync('testDir', {
'subdir': null
})
const fs = require('fs')
const fixturify = require('fixturify')
const obj = {
'subdir': {
'foo.txt': 'foo.txt contents'
},
'emptydir': {}
}
fixturify.writeSync('testdir', obj)
fixturify.readSync('testdir', { ignoreEmptyDirs: true })
File contents are decoded and encoded with UTF-8.
fixture.readSync
follows symlinks. It throws an error if it encounters a
broken symlink.
Limitations
To keep the API simple, node-fixturify has the following limitations:
-
Reading or setting file stats (last-modified time, permissions, etc.) is
not supported.
-
Creating symlinks is not supported. Symlinks are traversed when reading. Broken symlinks throw.
-
Special files like FIFOs, sockets, or devices are not supported.
-
File contents are automatically encoded/decoded into strings. Binary files
are not supported.