{"id":16260569,"url":"https://github.com/dword-design/with-local-tmp-dir","last_synced_at":"2025-03-16T13:31:04.035Z","repository":{"id":35129245,"uuid":"210366115","full_name":"dword-design/with-local-tmp-dir","owner":"dword-design","description":"Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.","archived":false,"fork":false,"pushed_at":"2025-02-26T05:04:39.000Z","size":2619,"stargazers_count":5,"open_issues_count":10,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T06:18:47.965Z","etag":null,"topics":["cleanup","cwd","directory","function","jest","local","mocha","path","remove","temp","temporary","test","tmp","with"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dword-design.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","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":{"custom":["buymeacoffee.com/dword","paypal.me/SebastianLandwehr"],"github":"dword-design","patreon":"dworddesign"}},"created_at":"2019-09-23T13:45:18.000Z","updated_at":"2025-02-26T05:04:43.000Z","dependencies_parsed_at":"2023-02-12T19:45:48.790Z","dependency_job_id":"ffe19d93-93cf-47c1-8e5f-7cc6844a72d2","html_url":"https://github.com/dword-design/with-local-tmp-dir","commit_stats":{"total_commits":221,"total_committers":12,"mean_commits":"18.416666666666668","dds":0.6832579185520362,"last_synced_commit":"c9b4700fcb6dee4e31486635d6e4693a665baa34"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dword-design%2Fwith-local-tmp-dir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dword-design%2Fwith-local-tmp-dir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dword-design%2Fwith-local-tmp-dir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dword-design%2Fwith-local-tmp-dir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dword-design","download_url":"https://codeload.github.com/dword-design/with-local-tmp-dir/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243815605,"owners_count":20352195,"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":["cleanup","cwd","directory","function","jest","local","mocha","path","remove","temp","temporary","test","tmp","with"],"created_at":"2024-10-10T16:20:16.334Z","updated_at":"2025-03-16T13:31:03.639Z","avatar_url":"https://github.com/dword-design.png","language":"JavaScript","funding_links":["buymeacoffee.com/dword","paypal.me/SebastianLandwehr","https://github.com/sponsors/dword-design","https://patreon.com/dworddesign","https://www.buymeacoffee.com/dword","https://paypal.me/SebastianLandwehr","https://www.patreon.com/dworddesign"],"categories":[],"sub_categories":[],"readme":"\u003c!-- TITLE/ --\u003e\n# with-local-tmp-dir\n\u003c!-- /TITLE --\u003e\n\n\u003c!-- BADGES/ --\u003e\n  \u003cp\u003e\n    \u003ca href=\"https://npmjs.org/package/with-local-tmp-dir\"\u003e\n      \u003cimg\n        src=\"https://img.shields.io/npm/v/with-local-tmp-dir.svg\"\n        alt=\"npm version\"\n      \u003e\n    \u003c/a\u003e\u003cimg src=\"https://img.shields.io/badge/os-linux%20%7C%C2%A0macos%20%7C%C2%A0windows-blue\" alt=\"Linux macOS Windows compatible\"\u003e\u003ca href=\"https://github.com/dword-design/with-local-tmp-dir/actions\"\u003e\n      \u003cimg\n        src=\"https://github.com/dword-design/with-local-tmp-dir/workflows/build/badge.svg\"\n        alt=\"Build status\"\n      \u003e\n    \u003c/a\u003e\u003ca href=\"https://codecov.io/gh/dword-design/with-local-tmp-dir\"\u003e\n      \u003cimg\n        src=\"https://codecov.io/gh/dword-design/with-local-tmp-dir/branch/master/graph/badge.svg\"\n        alt=\"Coverage status\"\n      \u003e\n    \u003c/a\u003e\u003ca href=\"https://david-dm.org/dword-design/with-local-tmp-dir\"\u003e\n      \u003cimg src=\"https://img.shields.io/david/dword-design/with-local-tmp-dir\" alt=\"Dependency status\"\u003e\n    \u003c/a\u003e\u003cimg src=\"https://img.shields.io/badge/renovate-enabled-brightgreen\" alt=\"Renovate enabled\"\u003e\u003cbr/\u003e\u003ca href=\"https://gitpod.io/#https://github.com/dword-design/with-local-tmp-dir\"\u003e\n      \u003cimg\n        src=\"https://gitpod.io/button/open-in-gitpod.svg\"\n        alt=\"Open in Gitpod\"\n        width=\"114\"\n      \u003e\n    \u003c/a\u003e\u003ca href=\"https://www.buymeacoffee.com/dword\"\u003e\n      \u003cimg\n        src=\"https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg\"\n        alt=\"Buy Me a Coffee\"\n        width=\"114\"\n      \u003e\n    \u003c/a\u003e\u003ca href=\"https://paypal.me/SebastianLandwehr\"\u003e\n      \u003cimg\n        src=\"https://sebastianlandwehr.com/images/paypal.svg\"\n        alt=\"PayPal\"\n        width=\"163\"\n      \u003e\n    \u003c/a\u003e\u003ca href=\"https://www.patreon.com/dworddesign\"\u003e\n      \u003cimg\n        src=\"https://sebastianlandwehr.com/images/patreon.svg\"\n        alt=\"Patreon\"\n        width=\"163\"\n      \u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\u003c!-- /BADGES --\u003e\n\n\u003c!-- DESCRIPTION/ --\u003e\nCreates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.\n\u003c!-- /DESCRIPTION --\u003e\n\nThis package can be used to write file-based tests with real files. See below for some examples.\n\n\u003c!-- INSTALL/ --\u003e\n## Install\n\n```bash\n# npm\n$ npm install with-local-tmp-dir\n\n# Yarn\n$ yarn add with-local-tmp-dir\n```\n\u003c!-- /INSTALL --\u003e\n\n## Basic Usage\n\n```js\nconst withLocalTmpDir = require('with-local-tmp-dir')\nconst fs = require('fs-extra')\n\nawait withLocalTmpDir(() =\u003e {\n  console.log(process.cwd())\n  //\u003e /Users/max/project/tmp-18815DudQxmdn03Rz\n\n  // Create some files\n  await fs.outputFile('foo.txt', 'foo bar')\n  await fs.outputFile('bar.txt', 'foo bar')\n})\n// Now the folder does not exist anymore\n\n// The folder is removed even if an exception is thrown\nawait withLocalTmpDir(() =\u003e throw new Error('File could not be found'))\n```\n\n## Reset\n\nInstead of a function you can reset the state yourself instead of passing a function, i.e. change to the previous folder and remove the folder:\n\n```js\nconst withLocalTmpDir = require('with-local-tmp-dir')\nconst fs = require('fs-extra')\n\n// You can still pass options\nconst reset = await withLocalTmpDir()\n\nconsole.log(process.cwd())\n//\u003e /Users/max/project/tmp-18815DudQxmdn03Rz\n\n// Create some files\nawait fs.outputFile('foo.txt', 'foo bar')\nawait fs.outputFile('bar.txt', 'foo bar')\n\nawait reset()\n\n// Now the folder does not exist anymore\n```\n\n## Options\n\nYou can specify an options object that is passed down to [tmp-promise](https://github.com/benjamingr/tmp-promise). Check the readme for details. Some default values are adjusted, but they still can be overridden.\n\n```js\nconst withLocalTmpDir = require('with-local-tmp-dir')\nconst fs = require('fs-extra')\n\n// do not cleanup if there are files\nawait withLocalTmpDir(() =\u003e {\n  await fs.outputFile('foo.txt', 'foo bar')\n}, { unsafeCleanup: false })\n\n// run in a subfolder\nawait withLocalTmpDir(() =\u003e {\n  console.log(process.cwd())\n  //\u003e /Users/max/project/foo/tmp-18815DudQxmdn03Rz\n}, { dir: 'foo' })\n\n// use a different prefix\nawait withLocalTmpDir(() =\u003e {\n  console.log(process.cwd())\n  //\u003e /Users/max/project/foo-18815DudQxmdn03Rz\n}, { prefix: 'foo' })\n```\n\n## Unit Tests with output-files and endent\n\nThe most common use case for this package is probably unit tests with Mocha or Jest. The package itself is framework-agnostic, so you can use any of them. To make life easier when writing multiple files with multi-line text content, there are two handy packages: [output-files](git@github.com:dword-design/output-files.git) and [endent](https://github.com/indentjs/endent).\n\nThe following is an example for Mocha:\n\n```js\nconst withLocalTmpDir = require('with-local-tmp-dir')\nconst outputFiles = require('output-files')\nconst endent = require('endent')\n\nconst funcToTest = require('.')\n\nbeforeEach(async function () {\n  this.resetWithLocalTmpDir = await withLocalTmpDir()\n})\n\nafterEach(async function () {\n  await this.resetWithLocalTmpDir()\n})\n\nit('works', async () =\u003e {\n  await outputFiles({\n    'foo.txt': endent`\n      Lorem ipsum\n      dolor sit\n    `,\n    'index.js': endent`\n      export default {\n        foo: 1,\n        bar: 2,\n      }\n    `,\n  })\n  funcToTest()\n})\n```\n\n## Git-based tests\n\nIt is also possible to test libraries that make use of Git. You can instantiate a local Git repository inside the temporary folder and run Git operations on it:\n\n```js\nconst withLocalTmpDir = require('with-local-tmp-dir')\nconst fs = require('fs-extra')\nconst execa = require('execa')\n\nbeforeEach(async function () {\n  this.resetWithLocalTmpDir = await withLocalTmpDir()\n})\n\nafterEach(async function () {\n  await this.resetWithLocalTmpDir()\n})\n\nit('works', async () =\u003e {\n  await execa.command('git init')\n  await execa.command('git config user.email \"foo@bar.de\"')\n  await execa.command('git config user.name \"foo\"')\n\n  await outputFile('foo.txt', 'foo bar')\n\n  await execa.command('git add .')\n  await execa.command('git commit -m \"feat: init\"')\n  funcToTest()\n})\n```\n\nBe careful though, if the repository is not properly initialized, your user Git config might be overridden!\n\n\u003c!-- LICENSE/ --\u003e\n## Contribute\n\nAre you missing something or want to contribute? Feel free to file an [issue](https://github.com/dword-design/with-local-tmp-dir/issues) or a [pull request](https://github.com/dword-design/with-local-tmp-dir/pulls)! ⚙️\n\n## Support\n\nHey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:\n\n\u003cp\u003e\n  \u003ca href=\"https://www.buymeacoffee.com/dword\"\u003e\n    \u003cimg\n      src=\"https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg\"\n      alt=\"Buy Me a Coffee\"\n      width=\"114\"\n    \u003e\n  \u003c/a\u003e\u0026nbsp;If you want to send me a one time donation. The coffee is pretty good 😊.\u003cbr/\u003e\n  \u003ca href=\"https://paypal.me/SebastianLandwehr\"\u003e\n    \u003cimg\n      src=\"https://sebastianlandwehr.com/images/paypal.svg\"\n      alt=\"PayPal\"\n      width=\"163\"\n    \u003e\n  \u003c/a\u003e\u0026nbsp;Also for one time donations if you like PayPal.\u003cbr/\u003e\n  \u003ca href=\"https://www.patreon.com/dworddesign\"\u003e\n    \u003cimg\n      src=\"https://sebastianlandwehr.com/images/patreon.svg\"\n      alt=\"Patreon\"\n      width=\"163\"\n    \u003e\n  \u003c/a\u003e\u0026nbsp;Here you can support me regularly, which is great so I can steadily work on projects.\n\u003c/p\u003e\n\nThanks a lot for your support! ❤️\n\n## See also\n\n* [output-files](https://github.com/dword-design/output-files): Output a tree of files and directories by providing an object. Especially useful for testing with real files.\n* [expect-mocha-image-snapshot](https://github.com/dword-design/expect-mocha-image-snapshot): A wrapper around jest-image-snapshot that makes it compatible to Mocha.\n* [jest-image-matcher](https://github.com/dword-design/jest-image-matcher): A Jest matcher for image comparisons based on pixelmatch. Can also be used with Mocha. Useful for visual regression testing.\n* [unify-mocha-output](https://github.com/dword-design/unify-mocha-output): Adjusts a Mocha output so that it is consistent across platforms and can be used for snapshot testing. Basically adjusts the checkmark symbol and removes time values.\n* [mock-argv](https://github.com/dword-design/mock-argv): Temporarily overrides the command line arguments. This is useful for testing.\n\n## License\n\n[MIT License](https://opensource.org/licenses/MIT) © [Sebastian Landwehr](https://sebastianlandwehr.com)\n\u003c!-- /LICENSE --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdword-design%2Fwith-local-tmp-dir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdword-design%2Fwith-local-tmp-dir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdword-design%2Fwith-local-tmp-dir/lists"}