From a1692cebbdb5598e6d3069a1115041ef348fa65d Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Mon, 10 Dec 2018 13:26:18 -0800 Subject: [PATCH] test: Add load-json-file auto-mock, employ in lifecycle scripts --- commands/__mocks__/load-json-file.js | 41 +++++++++++++++++++ .../publish-lifecycle-scripts.test.js | 8 ++++ .../version-lifecycle-scripts.test.js | 8 ++++ 3 files changed, 57 insertions(+) create mode 100644 commands/__mocks__/load-json-file.js diff --git a/commands/__mocks__/load-json-file.js b/commands/__mocks__/load-json-file.js new file mode 100644 index 0000000000..5babe2aedd --- /dev/null +++ b/commands/__mocks__/load-json-file.js @@ -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; diff --git a/commands/publish/__tests__/publish-lifecycle-scripts.test.js b/commands/publish/__tests__/publish-lifecycle-scripts.test.js index 279ace00de..23e62093f4 100644 --- a/commands/publish/__tests__/publish-lifecycle-scripts.test.js +++ b/commands/publish/__tests__/publish-lifecycle-scripts.test.js @@ -12,6 +12,7 @@ jest.mock("../../version/lib/remote-branch-exists"); // mocked modules const runLifecycle = require("@lerna/run-lifecycle"); +const loadJsonFile = require("load-json-file"); // helpers const initFixture = require("@lerna-test/init-fixture")(__dirname); @@ -67,5 +68,12 @@ describe("lifecycle scripts", () => { ["package-1", "postpublish"], ["lifecycle", "postpublish"], ]); + + expect(loadJsonFile.registry).toMatchInlineSnapshot(` +Map { + "/packages/package-1" => 2, + "/packages/package-2" => 2, +} +`); }); }); diff --git a/commands/version/__tests__/version-lifecycle-scripts.test.js b/commands/version/__tests__/version-lifecycle-scripts.test.js index fac0c56793..ecae5c7489 100644 --- a/commands/version/__tests__/version-lifecycle-scripts.test.js +++ b/commands/version/__tests__/version-lifecycle-scripts.test.js @@ -10,6 +10,7 @@ const path = require("path"); // mocked modules const runLifecycle = require("@lerna/run-lifecycle"); +const loadJsonFile = require("load-json-file"); // helpers const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname, "../../publish/__tests__")); @@ -45,5 +46,12 @@ describe("lifecycle scripts", () => { ["package-1", "postversion"], ["lifecycle", "postversion"], ]); + + expect(loadJsonFile.registry).toMatchInlineSnapshot(` +Map { + "/packages/package-1" => 1, + "/packages/package-2" => 1, +} +`); }); });