-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
load-json-file.js
41 lines (31 loc) · 1.24 KB
/
load-json-file.js
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
"use strict";
const path = require("path");
const normalizePath = require("normalize-path");
const loadJsonFile = jest.requireActual("load-json-file");
const asyncRegistry = new Map();
const syncRegistry = new Map();
function incrementCalled(registry, manifestLocation) {
// tempy creates dirnames that are 32 characters long, but we want a readable key
const subPath = manifestLocation.split(/[0-9a-f]{32}/).pop();
const key = normalizePath(path.dirname(subPath));
// keyed off directory subpath, _not_ pkg.name (we don't know it yet)
registry.set(key, (registry.get(key) || 0) + 1);
}
// by default, act like a spy that counts number of times each location was loaded
const mockLoadJsonFile = jest.fn(manifestLocation => {
incrementCalled(asyncRegistry, manifestLocation);
return loadJsonFile(manifestLocation);
});
const mockLoadJsonFileSync = jest.fn(manifestLocation => {
incrementCalled(syncRegistry, manifestLocation);
return loadJsonFile.sync(manifestLocation);
});
// keep test data isolated
afterEach(() => {
asyncRegistry.clear();
syncRegistry.clear();
});
module.exports = mockLoadJsonFile;
module.exports.registry = asyncRegistry;
module.exports.sync = mockLoadJsonFileSync;
module.exports.sync.registry = syncRegistry;