{"id":13847192,"url":"https://github.com/privatenumber/fs-fixture","last_synced_at":"2025-04-06T03:06:11.378Z","repository":{"id":49845963,"uuid":"512610645","full_name":"privatenumber/fs-fixture","owner":"privatenumber","description":"Simple API to create test fixtures on disk","archived":false,"fork":false,"pushed_at":"2025-02-18T02:53:23.000Z","size":1451,"stargazers_count":50,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T02:04:31.559Z","etag":null,"topics":["fixture","fs","json","object","template","test","utility"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/privatenumber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"privatenumber"}},"created_at":"2022-07-11T04:03:23.000Z","updated_at":"2025-03-02T04:16:15.000Z","dependencies_parsed_at":"2024-12-24T02:12:48.207Z","dependency_job_id":"05923565-0535-430f-8bf7-11e049412e36","html_url":"https://github.com/privatenumber/fs-fixture","commit_stats":{"total_commits":16,"total_committers":1,"mean_commits":16.0,"dds":0.0,"last_synced_commit":"0152da8cbe45c077de6950a0fa559f74a7392ca9"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-fixture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-fixture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-fixture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-fixture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/privatenumber","download_url":"https://codeload.github.com/privatenumber/fs-fixture/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427005,"owners_count":20937200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fixture","fs","json","object","template","test","utility"],"created_at":"2024-08-04T18:01:12.755Z","updated_at":"2025-04-06T03:06:11.363Z","avatar_url":"https://github.com/privatenumber.png","language":"TypeScript","funding_links":["https://github.com/sponsors/privatenumber","https://github.com/sponsors/privatenumber/sponsorships?tier_id=398771","https://github.com/sponsors/privatenumber/sponsorships?tier_id=397608"],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\t\u003cimg width=\"160\" src=\".github/logo.webp\"\u003e\n\u003c/p\u003e\n\u003ch1 align=\"center\"\u003e\n\t\u003csup\u003efs-fixture\u003c/sup\u003e\n\t\u003cbr\u003e\n\t\u003ca href=\"https://npm.im/fs-fixture\"\u003e\u003cimg src=\"https://badgen.net/npm/v/fs-fixture\"\u003e\u003c/a\u003e \u003ca href=\"https://npm.im/fs-fixture\"\u003e\u003cimg src=\"https://badgen.net/npm/dm/fs-fixture\"\u003e\u003c/a\u003e\n\u003c/h1\u003e\n\nSimple API to create disposable test fixtures on disk.\n\nTiny (`560 B` gzipped) and no dependencies!\n\n### Example\n```ts\nimport fs from 'node:fs/promises'\nimport { createFixture } from 'fs-fixture'\n\nconst fixture = await createFixture({\n    'dir-a': {\n        'file-b': 'hello world'\n    }\n})\n\nconst content = await fs.readFile(fixture.getPath('dir-a/file-b'))\nconsole.log(content)\n```\n\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"https://github.com/sponsors/privatenumber/sponsorships?tier_id=398771\"\u003e\u003cimg width=\"412\" src=\"https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/donate.webp\"\u003e\u003c/a\u003e\n\t\u003ca href=\"https://github.com/sponsors/privatenumber/sponsorships?tier_id=397608\"\u003e\u003cimg width=\"412\" src=\"https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/sponsor.webp\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\u003csup\u003e\u003ci\u003eAlready a sponsor?\u003c/i\u003e Join the discussion in the \u003ca href=\"https://github.com/pvtnbr/fs-fixture\"\u003eDevelopment repo\u003c/a\u003e!\u003c/sup\u003e\u003c/p\u003e\n\n## Usage\n\nPass in an object representing the file structure:\n\n```ts\nimport { createFixture } from 'fs-fixture'\n\nconst fixture = await createFixture({\n    // Nested directory syntax\n    'dir-a': {\n        'file-a.txt': 'hello world',\n        'dir-b': {\n            'file-b.txt': ({ fixturePath }) =\u003e `Fixture path: ${fixturePath}`,\n            'symlink-c': ({ symlink }) =\u003e symlink('../file-a.txt')\n        }\n    },\n\n    // Alternatively, use the directory path syntax - Same as above\n    'dir-a/dir-b/file-b.txt': 'goodbye world'\n})\n\n// Interact with the fixture\nconsole.log(fixture.path)\n\n// Cleanup fixture\nawait fixture.rm()\n```\n\n### Template path input\n\nPass in a path to a test fixture template directory to make a copy of it.\n\n```ts\n// Pass in a path to a fixture template path, and it will make a copy of it\nconst fixture = await createFixture('./fixtures/template-a')\n\n/* Your test code here... */\n\n// Cleanup fixture\nawait fixture.rm()\n```\n\n### `using` keyword (Explicit Resource Management)\n\n[TypeScript 5.2](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html) supports the [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) feature, which allows you to instantiate the fixture via `using`. When the fixture is declared this way, it gets automatically cleaned up when exiting the scope.\n\n```ts\nawait using fixture = await createFixture({ file: 'hello' })\n\n// No need to run fixture.rm()\n```\n\n## API\n\n### createFixture(source, options)\n\nAn async function that creates a fixture from the `source` you pass in, and returns a `FsFixture` instance.\n\n#### source\nType: `string | FileTree`\n\nPath to a template fixture path, or a `FileTree` object that represents the fixture content.\n\n\n#### options\n\n##### tempDir\n\nType: `string`\n\nDefault: `os.tmpdir()`\n\nThe directory where the fixture will be created.\n\n\n##### templateFilter\n\nType: `(source: string, destination: string) =\u003e boolean | Promise\u003cboolean\u003e`\n\nFunction to filter files to copy when using a template path. Return `true` to copy the item, `false` to ignore it.\n\n### Types\n#### FileTree\n\n```ts\ntype FileTree = {\n    [path: string]: string | FileTree | ((api: Api) =\u003e string)\n}\n\ntype Api = {\n    // Fixture root path\n    fixturePath: string\n\n    // Current file path\n    filePath: string\n\n    // Get path from the root of the fixture\n    getPath: (...subpaths: string[]) =\u003e string\n\n    // Create a symlink\n    symlink: (target: string) =\u003e Symlink\n}\n```\n\n#### FsFixture\n\n```ts\nclass FsFixture {\n    /**\n    Path to the fixture directory.\n    */\n    readonly path: string\n\n    /**\n    Create a Fixture instance from a path. Does not create the fixture directory.\n    */\n    constructor(fixturePath: string)\n\n    /**\n    Get the full path to a subpath in the fixture directory.\n    */\n    getPath(...subpaths: string[]): string\n\n    /**\n    Check if the fixture exists. Pass in a subpath to check if it exists.\n    */\n    exists(subpath?: string): Promise\u003cboolean\u003e\n\n    /**\n    Delete the fixture directory. Pass in a subpath to delete it.\n    */\n    rm(subpath?: string): Promise\u003cvoid\u003e\n\n    /**\n    Copy a path into the fixture directory.\n    */\n    cp(sourcePath: string, destinationSubpath?: string): Promise\u003cvoid\u003e\n\n    /**\n    Create a file in the fixture directory.\n    */\n    writeFile(filePath: string, content: string): Promise\u003cvoid\u003e\n\n    /**\n    Create a JSON file in the fixture directory.\n    */\n    writeJson(filePath: string, json: unknown): Promise\u003cvoid\u003e\n\n    /**\n    Read a file from the fixture directory.\n    */\n    readFile(filePath: string, encoding?: BufferEncoding): Promise\u003cstring | Buffer\u003e\n}\n```\n\n## Related\n\n### [manten](https://github.com/privatenumber/manten)\n\nLightweight testing library for Node.js\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprivatenumber%2Ffs-fixture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprivatenumber%2Ffs-fixture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprivatenumber%2Ffs-fixture/lists"}