Debian Codegraph @

packages/babel-register/test/cache.js

raw symbols SCIP
1 import fs from "fs";
2 import path from "path";
3 import { fileURLToPath } from "url";
4 import { createRequire, Module } from "module";
5
6 const require = createRequire(import.meta.url);
7
8 const testCacheFilename = path.join(
9 path.dirname(fileURLToPath(import.meta.url)),
10 ".cache.babel",
11 );
12 const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE;
13
14 process.env.BABEL_CACHE_PATH = testCacheFilename;
15 delete process.env.BABEL_DISABLE_CACHE;
16
17 function writeCache(data, mode = 0o666) {
18 if (typeof data === "object") {
19 data = JSON.stringify(data);
20 }
21
22 fs.writeFileSync(testCacheFilename, data, { mode });
23 }
24
25 function cleanCache() {
26 try {
27 fs.unlinkSync(testCacheFilename);
28 } catch (e) {
29 // It is convenient to always try to clear
30 }
31 }
32
33 function resetCache() {
34 process.env.BABEL_CACHE_PATH = null;
35 process.env.BABEL_DISABLE_CACHE = oldBabelDisableCacheValue;
36 }
37
38 const OLD_JEST_MOCKS = !!jest.doMock;
39
40 describe("@babel/register - caching", () => {
41 describe("cache", () => {
42 let load, get, setDirty, save;
43 let consoleWarnSpy;
44
45 if (!OLD_JEST_MOCKS) {
46 let oldModuleCache;
47 beforeAll(() => {
48 oldModuleCache = Module._cache;
49 });
50 afterAll(() => {
51 Module._cache = oldModuleCache;
52 });
53 }
54
55 beforeEach(() => {
56 // Since lib/cache is a singleton we need to fully reload it
57 if (OLD_JEST_MOCKS) {
58 jest.resetModules();
59 } else {
60 Module._cache = {};
61 }
62 const cache = require("../lib/cache.js");
63
64 load = cache.load;
65 get = cache.get;
66 setDirty = cache.setDirty;
67 save = cache.save;
68
69 consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
70 });
71
72 afterEach(() => {
73 cleanCache();
74 consoleWarnSpy.mockRestore();
75 });
76 afterAll(resetCache);
77
78 it("should load and get cached data", () => {
79 writeCache({ foo: "bar" });
80
81 load();
82
83 expect(get()).toEqual({ foo: "bar" });
84 });
85
86 it("should load and get an object with no cached data", () => {
87 load();
88
89 expect(get()).toEqual({});
90 });
91
92 it("should load and get an object with invalid cached data", () => {
93 writeCache("foobar");
94
95 load();
96
97 expect(get()).toEqual({});
98 });
99
100 it("should not create the cache if not dirty", () => {
101 save();
102
103 expect(fs.existsSync(testCacheFilename)).toBe(false);
104 expect(get()).toEqual({});
105 });
106
107 it("should create the cache on save if dirty", () => {
108 setDirty();
109 save();
110
111 expect(fs.existsSync(testCacheFilename)).toBe(true);
112 expect(get()).toEqual({});
113 });
114
115 it("should create the cache after dirty", () => {
116 load();
117 setDirty();
118 return new Promise(resolve => {
119 process.nextTick(() => {
120 expect(fs.existsSync(testCacheFilename)).toBe(true);
121 expect(get()).toEqual({});
122 resolve();
123 });
124 });
125 });
126
127 // Only write mode is effective on Windows
128 if (process.platform !== "win32") {
129 it("should be disabled when CACHE_PATH is not allowed to read", () => {
130 writeCache({ foo: "bar" }, 0o266);
131 load();
132
133 expect(get()).toEqual({});
134 expect(consoleWarnSpy.mock.calls[0][0]).toContain(
135 "Babel could not read cache file",
136 );
137 });
138 }
139
140 it("should be disabled when CACHE_PATH is not allowed to write", () => {
141 writeCache({ foo: "bar" }, 0o466);
142
143 load();
144 setDirty();
145
146 expect(get()).toEqual({ foo: "bar" });
147 return new Promise(resolve => {
148 process.nextTick(() => {
149 load();
150 expect(get()).toEqual({});
151 expect(consoleWarnSpy.mock.calls[0][0]).toContain(
152 "Babel could not write cache to file",
153 );
154 resolve();
155 });
156 });
157 });
158 });
159 });