Skip to content

Commit

Permalink
test: Add load-json-file auto-mock, employ in lifecycle scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 10, 2018
1 parent e209bcb commit a1692ce
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
41 changes: 41 additions & 0 deletions commands/__mocks__/load-json-file.js
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;
8 changes: 8 additions & 0 deletions commands/publish/__tests__/publish-lifecycle-scripts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
}
`);
});
});
8 changes: 8 additions & 0 deletions commands/version/__tests__/version-lifecycle-scripts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__"));
Expand Down Expand Up @@ -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,
}
`);
});
});

0 comments on commit a1692ce

Please sign in to comment.