Skip to content

No Real tmpdir

Error
small-rules/no-real-temporary-directory

Disallow real temp directories (os.tmpdir, fs.mkdtemp) in unit tests.

Rationale

A unit test that writes to a real temp directory runs slower and leaves files behind when it fails. Tests running in parallel can also collide there. The rule reports calls to tmpdir from os, and to mkdtemp or mkdtempSync from fs, fs.promises, or fs/promises, with or without node:. Pass an in-memory file system such as memfs through a parameter instead. A test that needs a real directory belongs in an integration test, so enable this rule for unit test files only.

Scope analysis follows the binding through named, aliased, namespace, and default imports, require(), await import(), and object destructuring. A local function or a memfs method with the same name passes.

additionalFunctions adds other functions that make a real temp directory, grouped by module. Write a function inside a namespace as a dotted path. For example, { source: "tmp", functions: ["dirSync", "promises.dir"] } covers the tmp package.

Diagnostic Messages

noRealTemporaryDirectory
Unit tests must not touch real temp directories. Use memfs through an injected fs seam, or move the test to a *.integration.test.ts file.

Configuration

This rule accepts one options object after the severity.

additionalFunctionsOptional

More functions that create or locate a real temp directory, grouped by the module that exports them

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-real-temporary-directory": [
"error",
{
"additionalFunctions": []
}
]
}
}

Examples

Real temp directory in a unit test
import { mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
const directory = await mkdtemp(join(tmpdir(), "case-"));
In-memory file system
import { fs } from "memfs";
const directory = fs.mkdtempSync("/case-");