-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
node.ts
46 lines (38 loc) · 1.22 KB
/
node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { existsSync, promises as fs } from 'node:fs'
import { basename, dirname, isAbsolute, join, resolve } from 'pathe'
import type { SnapshotEnvironment } from '../types'
export class NodeSnapshotEnvironment implements SnapshotEnvironment {
getVersion(): string {
return '1'
}
getHeader(): string {
return `// Snapshot v${this.getVersion()}`
}
async resolveRawPath(testPath: string, rawPath: string) {
return isAbsolute(rawPath)
? rawPath
: resolve(dirname(testPath), rawPath)
}
async resolvePath(filepath: string): Promise<string> {
return join(
join(
dirname(filepath), '__snapshots__'),
`${basename(filepath)}.snap`,
)
}
async prepareDirectory(filepath: string): Promise<void> {
await fs.mkdir(filepath, { recursive: true })
}
async saveSnapshotFile(filepath: string, snapshot: string): Promise<void> {
await fs.writeFile(filepath, snapshot, 'utf-8')
}
async readSnapshotFile(filepath: string): Promise<string | null> {
if (!existsSync(filepath))
return null
return fs.readFile(filepath, 'utf-8')
}
async removeSnapshotFile(filepath: string): Promise<void> {
if (existsSync(filepath))
await fs.unlink(filepath)
}
}