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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import { v4 as uuidv4 } from "uuid";
import path from "path";
import fs from "fs-extra";
import yaml from "js-yaml";
import JSON5 from "json5";
export default function setupTestFixtures({
directory,
test = "",
}: {
directory: string;
test?: string;
}) {
const fixtures: Array<string> = [];
const parentDirectory = path.join(directory, test ? test : "test-runs");
afterEach(() => {
fixtures.forEach((fixture) => {
fs.rmSync(fixture, { recursive: true, force: true });
});
});
afterAll(() => {
fs.rmSync(parentDirectory, { recursive: true, force: true });
});
const useFixture = ({ fixture }: { fixture: string }) => {
const directoryName = uuidv4();
const testDirectory = path.join(parentDirectory, directoryName);
if (!fs.existsSync(testDirectory)) {
fs.mkdirSync(testDirectory, { recursive: true });
}
// keep track of it
fixtures.push(testDirectory);
// copy fixture to test directory
const fixturePath = path.join(directory, "__fixtures__", test, fixture);
fs.copySync(fixturePath, testDirectory, {
recursive: true,
});
const getFilePath = (filename: string) => {
return path.isAbsolute(filename)
? filename
: path.join(testDirectory, filename);
};
const readGenerator = (method: (filePath: string) => unknown) => {
return <T>(filename: string) => {
try {
return method(getFilePath(filename)) as T;
} catch (e) {
return undefined;
}
};
};
const write = (
filename: string,
content: string | NodeJS.ArrayBufferView
) => {
fs.writeFileSync(getFilePath(filename), content);
};
const exists = (filename: string): boolean => {
return fs.existsSync(getFilePath(filename));
};
const read = readGenerator((filePath) => fs.readFileSync(filePath, "utf8"));
const readJson = readGenerator((filePath) =>
JSON5.parse(fs.readFileSync(filePath, "utf8"))
);
const readYaml = readGenerator((filePath) =>
yaml.load(fs.readFileSync(filePath, "utf8"))
);
return {
root: testDirectory,
read,
readJson,
readYaml,
write,
exists,
directoryName,
};
};
return { useFixture };
}
|