### Basic Usage Example with fs-fixture Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md This snippet demonstrates how to create a simple file system fixture with a nested directory and a file, then read its content using Node.js's built-in file system module. ```typescript import fs from 'node:fs/promises' import { createFixture } from 'fs-fixture' const fixture = await createFixture({ 'dir-a': { 'file-b': 'hello world' } }) const content = await fs.readFile(fixture.getPath('dir-a/file-b')) console.log(content) ``` -------------------------------- ### Creating Fixtures with Nested Directories and Symlinks Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md This example illustrates creating more complex file structures, including nested directories, files with dynamic content generated by a callback, and symbolic links. It also shows how to access the fixture's root path and clean it up manually. ```typescript import { createFixture } from 'fs-fixture' const fixture = await createFixture({ // Nested directory syntax 'dir-a': { 'file-a.txt': 'hello world', 'dir-b': { 'file-b.txt': ({ fixturePath }) => `Fixture path: ${fixturePath}`, 'symlink-c': ({ symlink }) => symlink('../file-a.txt') } }, // Alternatively, use the directory path syntax - Same as above 'dir-a/dir-b/file-b.txt': 'goodbye world' }) // Interact with the fixture console.log(fixture.path) // Cleanup fixture await fixture.rm() ``` -------------------------------- ### Creating Fixtures from a Template Directory Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md This snippet shows how to create a fixture by copying an existing directory structure from a specified template path. This is useful for tests that require a predefined set of files and directories. ```typescript // Pass in a path to a fixture template path, and it will make a copy of it const fixture = await createFixture('./fixtures/template-a') /* Your test code here... */ // Cleanup fixture await fixture.rm() ``` -------------------------------- ### FsFixture Class API Reference Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md Detailed API documentation for the FsFixture class, including its properties, constructor, and methods for file system operations like creating, reading, writing, copying, and deleting files and directories within a designated fixture path. ```APIDOC class FsFixture { /** Path to the fixture directory. */ readonly path: string /** Create a Fixture instance from a path. Does not create the fixture directory. */ constructor(fixturePath: string) /** Get the full path to a subpath in the fixture directory. */ getPath(...subpaths: string[]): string /** Check if the fixture exists. Pass in a subpath to check if it exists. */ exists(subpath?: string): Promise /** Delete the fixture directory. Pass in a subpath to delete it. */ rm(subpath?: string): Promise /** Copy a path into the fixture directory. */ cp(sourcePath: string, destinationSubpath?: string): Promise /** Create a new folder in the fixture directory. */ mkdir(folderPath: string): Promise /** Create a file in the fixture directory. */ writeFile(filePath: string, content: string): Promise /** Create a JSON file in the fixture directory. */ writeJson(filePath: string, json: unknown): Promise /** Read a file from the fixture directory. */ readFile(filePath: string, encoding?: BufferEncoding): Promise } ``` -------------------------------- ### API: createFixture(source, options) Function Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md An asynchronous function that creates a file system fixture based on the provided source and returns an `FsFixture` instance. It supports defining content directly or copying from a template path. ```APIDOC createFixture(source, options) source: Type: string | FileTree Description: Path to a template fixture directory, or a FileTree object representing the fixture content. options: tempDir: Type: string Default: os.tmpdir() Description: The directory where the fixture will be created. templateFilter: Type: (source: string, destination: string) => boolean | Promise Description: A function to filter files when copying from a template path. Return true to copy, false to ignore. ``` -------------------------------- ### Automatic Fixture Cleanup with TypeScript `using` Keyword Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md Demonstrates using TypeScript 5.2's Explicit Resource Management feature (`using` keyword) to automatically clean up the fixture when it goes out of scope, eliminating the need for explicit `fixture.rm()` calls. ```typescript await using fixture = await createFixture({ file: 'hello' }) // No need to run fixture.rm() ``` -------------------------------- ### API: FileTree and Api Types Source: https://github.com/privatenumber/fs-fixture/blob/master/README.md Definitions for the `FileTree` type, used to specify the structure and content of the fixture, and the `Api` type, which provides utility functions accessible within file content callbacks. ```APIDOC type FileTree = { [path: string]: string | FileTree | ((api: Api) => string) } type Api = { fixturePath: string filePath: string getPath: (...subpaths: string[]) => string symlink: (target: string) => Symlink } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.