-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add load-json-file auto-mock, employ in lifecycle scripts
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
const normalizePath = require("normalize-path"); | ||
|
||
const loadJsonFile = require.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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters