Gathering detailed insights and metrics for with-local-tmp-dir
Gathering detailed insights and metrics for with-local-tmp-dir
Gathering detailed insights and metrics for with-local-tmp-dir
Gathering detailed insights and metrics for with-local-tmp-dir
Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
npm install with-local-tmp-dir
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (98.07%)
Shell (1.93%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
5 Stars
248 Commits
1 Forks
2 Watchers
11 Branches
6 Contributors
Updated on Apr 17, 2025
Latest Version
5.1.1
Package Id
with-local-tmp-dir@5.1.1
Unpacked Size
12.24 kB
Size
4.49 kB
File Count
5
NPM Version
7.24.2
Node Version
16.20.2
Published on
Mar 12, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
3
Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
This package can be used to write file-based tests with real files. See below for some examples.
1# npm 2$ npm install with-local-tmp-dir 3 4# Yarn 5$ yarn add with-local-tmp-dir
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3 4await withLocalTmpDir(() => { 5 console.log(process.cwd()) 6 //> /Users/max/project/tmp-18815DudQxmdn03Rz 7 8 // Create some files 9 await fs.outputFile('foo.txt', 'foo bar') 10 await fs.outputFile('bar.txt', 'foo bar') 11}) 12// Now the folder does not exist anymore 13 14// The folder is removed even if an exception is thrown 15await withLocalTmpDir(() => throw new Error('File could not be found'))
Instead 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:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3 4// You can still pass options 5const reset = await withLocalTmpDir() 6 7console.log(process.cwd()) 8//> /Users/max/project/tmp-18815DudQxmdn03Rz 9 10// Create some files 11await fs.outputFile('foo.txt', 'foo bar') 12await fs.outputFile('bar.txt', 'foo bar') 13 14await reset() 15 16// Now the folder does not exist anymore
You can specify an options object that is passed down to tmp-promise. Check the readme for details. Some default values are adjusted, but they still can be overridden.
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3 4// do not cleanup if there are files 5await withLocalTmpDir(() => { 6 await fs.outputFile('foo.txt', 'foo bar') 7}, { unsafeCleanup: false }) 8 9// run in a subfolder 10await withLocalTmpDir(() => { 11 console.log(process.cwd()) 12 //> /Users/max/project/foo/tmp-18815DudQxmdn03Rz 13}, { dir: 'foo' }) 14 15// use a different prefix 16await withLocalTmpDir(() => { 17 console.log(process.cwd()) 18 //> /Users/max/project/foo-18815DudQxmdn03Rz 19}, { prefix: 'foo' })
The 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 and endent.
The following is an example for Mocha:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const outputFiles = require('output-files') 3const endent = require('endent') 4 5const funcToTest = require('.') 6 7beforeEach(async function () { 8 this.resetWithLocalTmpDir = await withLocalTmpDir() 9}) 10 11afterEach(async function () { 12 await this.resetWithLocalTmpDir() 13}) 14 15it('works', async () => { 16 await outputFiles({ 17 'foo.txt': endent` 18 Lorem ipsum 19 dolor sit 20 `, 21 'index.js': endent` 22 export default { 23 foo: 1, 24 bar: 2, 25 } 26 `, 27 }) 28 funcToTest() 29})
It 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:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3const execa = require('execa') 4 5beforeEach(async function () { 6 this.resetWithLocalTmpDir = await withLocalTmpDir() 7}) 8 9afterEach(async function () { 10 await this.resetWithLocalTmpDir() 11}) 12 13it('works', async () => { 14 await execa.command('git init') 15 await execa.command('git config user.email "foo@bar.de"') 16 await execa.command('git config user.name "foo"') 17 18 await outputFile('foo.txt', 'foo bar') 19 20 await execa.command('git add .') 21 await execa.command('git commit -m "feat: init"') 22 funcToTest() 23})
Be careful though, if the repository is not properly initialized, your user Git config might be overridden!
Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️
Hey, 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:
If you want to send me a one time donation. The coffee is pretty good 😊.
Also for one time donations if you like PayPal.
Here you can support me regularly, which is great so I can steadily work on projects.
Thanks a lot for your support! ❤️
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/26 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
27 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More