Skip to content

Commit 16bf8df

Browse files
committed
refactor: use consts, async/await, etc in rollingcache spec
- basically using pre-defined fixture vars instead of ones defined inside the test - which is more straightforward, easier to read, and less fragile - shorter names too - left a couple of ones as is where they were only used once very quickly -- could make them fixture vars too but 🤷 - use async/await instead of Promises with `done()` etc - also use more `fs-extra` functions that support Promises instead of synchronous `fs` functions (e.g. `existsSync` -> `pathExists`) - async should be a small optimization for tests too - fix: use __temp dir for auto-generated files instead of creating them in a fixtures dir and breaking actual fixtures - format: a few multi-line statements were able to be condensed to a single line, so do so - esp as multi-line was not helping readability since this is just irrelevant test data (may have hampered readability actually)
1 parent c90c930 commit 16bf8df

File tree

1 file changed

+41
-57
lines changed

1 file changed

+41
-57
lines changed

__tests__/rollingcache.spec.ts

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
11
import { beforeEach, afterAll, test, expect } from "@jest/globals";
22
import * as path from "path";
3-
import * as fs from "fs";
4-
import { remove, ensureDir } from "fs-extra";
3+
import { remove, ensureDir, writeFile, pathExists } from "fs-extra";
54

65
import { RollingCache } from "../src/rollingcache";
76

87

9-
const defaultTestFileShape = {
10-
a: 1,
11-
b: 2,
12-
c: 3,
13-
};
148
const local = (x: string) => path.resolve(__dirname, x);
9+
const testDir = local("__temp/rollingcache");
10+
const oldCacheDir = `${testDir}/cache`;
11+
const newCacheDir = `${testDir}/cache_`;
12+
const testFile = "file.json";
13+
const oldTestFile = `${oldCacheDir}/${testFile}`;
1514

16-
beforeEach(() => {
17-
// pre-create oldCacheRoot
18-
return ensureDir(local("fixtures/cache")).then(() => {
19-
// and newCacheRoot
20-
return ensureDir(local("fixtures/cache_")).then(() => {
21-
const testfile = local("fixtures/cache/testfile.json");
22-
fs.writeFileSync(testfile, JSON.stringify(defaultTestFileShape), "utf8");
23-
});
24-
});
15+
const nonExistentFile = "this-does-not-exist.json";
16+
const emptyCacheRoot = `${testDir}/empty-cache-root`;
17+
18+
const testFileShape = { a: 1, b: 2, c: 3 };
19+
20+
21+
beforeEach(async () => {
22+
await ensureDir(oldCacheDir);
23+
await ensureDir(newCacheDir);
24+
await writeFile(oldTestFile, JSON.stringify(testFileShape), "utf8");
2525
});
26+
afterAll(() => remove(testDir));
2627

27-
afterAll(
28-
() => Promise.all([
29-
remove(local("fixtures")),
30-
remove(local("not-real")),
31-
]),
32-
);
3328

34-
test("RollingCache", () => {
29+
test("RollingCache", async () => {
3530
expect(RollingCache).toBeTruthy();
3631

37-
const cacheRoot = local("fixtures");
38-
const cache = new RollingCache(cacheRoot, true);
32+
const cache = new RollingCache(testDir, true);
3933
expect(Object.keys(cache)).toEqual([
4034
"cacheRoot",
4135
"checkNewCache",
@@ -44,35 +38,30 @@ test("RollingCache", () => {
4438
"newCacheRoot",
4539
]);
4640

47-
expect(cache.exists("fake-file.json")).toBeFalsy();
48-
expect(cache.exists("testfile.json")).toBeTruthy();
49-
expect(cache.path("x")).toEqual(local("fixtures/cache/x"));
50-
expect(cache.match(["fake-file.json"])).toBeFalsy();
51-
expect(cache.match(["testfile.json"])).toBeTruthy();
52-
expect(cache.read("testfile.json")).toEqual(defaultTestFileShape);
41+
expect(cache.exists(nonExistentFile)).toBeFalsy();
42+
expect(cache.exists(testFile)).toBeTruthy();
43+
expect(cache.path("x")).toEqual(`${oldCacheDir}/x`);
44+
expect(cache.match([nonExistentFile])).toBeFalsy();
45+
expect(cache.match([testFile])).toBeTruthy();
46+
expect(cache.read(testFile)).toEqual(testFileShape);
5347

5448
cache.write("write-test.json", {a: 2, b: 2, c: 2});
55-
expect(
56-
cache.read("write-test.json"),
57-
).toEqual({a: 2, b: 2, c: 2});
49+
expect(cache.read("write-test.json")).toEqual({a: 2, b: 2, c: 2});
5850

5951
cache.write("write-fail.json", (undefined as any));
60-
expect(
61-
cache.read("write-fail.json"),
62-
).toBeFalsy();
52+
expect(cache.read("write-fail.json")).toBeFalsy();
6353

6454
cache.touch("touched.json");
65-
expect(fs.existsSync(local("fixtures/cache_/touched.json"))).toBeTruthy();
55+
expect(await pathExists(`${newCacheDir}/touched.json`)).toBeTruthy();
6656
expect((cache as any).rolled).toBeFalsy();
6757

6858
cache.roll();
6959
expect((cache as any).rolled).toBeTruthy();
70-
expect(fs.existsSync(local("fixtures/cache_"))).toBeFalsy();
60+
expect(await pathExists(newCacheDir)).toBeFalsy();
7161
});
7262

73-
test("RollingCache, rolled", () => {
74-
const cacheRoot = local("fixtures");
75-
const cache = new RollingCache(cacheRoot, true);
63+
test("RollingCache, rolled", async () => {
64+
const cache = new RollingCache(testDir, true);
7665
// roll the cache
7766
cache.roll();
7867
// rolling again hits coverage for this.rolled being true already
@@ -81,28 +70,23 @@ test("RollingCache, rolled", () => {
8170
expect(cache.match([])).toBeFalsy();
8271

8372
cache.write("whatever.json", {whatever: true});
84-
expect(fs.existsSync(local("fixtures/cache/whatever.json"))).toBeFalsy();
73+
expect(await pathExists(`${oldCacheDir}/whatever.json`)).toBeFalsy();
8574

8675
cache.touch("touched.json");
87-
expect(fs.existsSync(local("fixtures/cache/touched.json"))).toBeFalsy();
76+
expect(await pathExists(`${oldCacheDir}/touched.json`)).toBeFalsy();
8877
});
8978

90-
test("RollingCache, test checkNewCache", (done) => {
91-
const cacheRoot = local("fixtures");
92-
const cache = new RollingCache(cacheRoot, true);
79+
test("RollingCache, test checkNewCache", async () => {
80+
const cache = new RollingCache(testDir, true);
9381

94-
const preExistingTestFile = local("fixtures/cache_/pre-existing.json");
95-
fs.writeFile(preExistingTestFile, JSON.stringify({}), "utf8", (e?: Error) => {
96-
if (e) console.log(e);
97-
expect(cache.exists("pre-existing.json")).toBeTruthy();
98-
done();
99-
});
82+
const preExistingFile = `${newCacheDir}/pre-existing.json`;
83+
await writeFile(preExistingFile, JSON.stringify({}));
84+
expect(cache.exists("pre-existing.json")).toBeTruthy();
10085
});
10186

102-
test("RollingCache, test match when oldCacheRoot is empty", () => {
103-
const cacheRoot = local("not-real");
104-
const cache = new RollingCache(cacheRoot, true);
87+
test("RollingCache, test match when oldCacheDir is empty", () => {
88+
const cache = new RollingCache(emptyCacheRoot, true);
10589

10690
expect(cache.match([])).toBeTruthy();
107-
expect(cache.match(["file.json"])).toBeFalsy();
91+
expect(cache.match([testFile])).toBeFalsy();
10892
});

0 commit comments

Comments
 (0)