0
# File System Utilities
1
2
Core file system operations designed for Jest's testing needs, providing safe directory creation and path resolution that handle edge cases common in test environments.
3
4
## Capabilities
5
6
### Create Directory
7
8
Creates directories recursively, safely ignoring errors if the directory already exists.
9
10
```typescript { .api }
11
/**
12
* Creates a directory recursively, ignoring EEXIST errors
13
* @param path - Directory path to create
14
* @throws Error if directory creation fails for reasons other than already existing
15
*/
16
function createDirectory(path: string): void;
17
```
18
19
**Usage Examples:**
20
21
```typescript
22
import { createDirectory } from "jest-util";
23
24
// Create test directories safely
25
createDirectory("/tmp/jest-tests/snapshots");
26
createDirectory("./test-output/coverage");
27
28
// Safe to call multiple times - won't throw if directory exists
29
createDirectory("/tmp/jest-tests"); // First call creates it
30
createDirectory("/tmp/jest-tests"); // Second call is safe, no error
31
```
32
33
**Error Handling:**
34
- Ignores `EEXIST` errors (directory already exists)
35
- Re-throws all other errors (permission denied, invalid path, etc.)
36
37
### Try Real Path
38
39
Safely resolves the real path of a file or directory, falling back to the original path if resolution fails.
40
41
```typescript { .api }
42
/**
43
* Safely resolves real path, falling back to original on error
44
* @param path - Path to resolve
45
* @returns Real path if successful, or original path if resolution fails
46
*/
47
function tryRealpath(path: string): string;
48
```
49
50
**Usage Examples:**
51
52
```typescript
53
import { tryRealpath } from "jest-util";
54
55
// Resolve symlinks safely
56
const configPath = tryRealpath("./jest.config.js");
57
const packagePath = tryRealpath("./node_modules/some-package");
58
59
// Handles missing files gracefully
60
const missingFile = tryRealpath("./non-existent.js");
61
// Returns "./non-existent.js" instead of throwing
62
63
// Resolves symlinks when they exist
64
const symlinkPath = "/usr/bin/node"; // might be a symlink
65
const realPath = tryRealpath(symlinkPath); // resolves to actual binary path
66
```
67
68
**Error Handling:**
69
- Safely handles `ENOENT` errors (file not found)
70
- Safely handles `EISDIR` errors (is a directory when file expected)
71
- Returns original path unchanged if resolution fails
72
- Does not throw exceptions for common file system edge cases