/
update-lockfile-version.test.js
55 lines (38 loc) · 1.94 KB
/
update-lockfile-version.test.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";
const path = require("path");
const fs = require("fs-extra");
// mocked or stubbed modules
const loadJsonFile = require("load-json-file");
// helpers
const { getPackages } = require("@lerna/project");
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const { updateLockfileVersion } = require("../lib/update-lockfile-version");
test("updateLockfileVersion with lockfile v1", async () => {
const cwd = await initFixture("lockfile-leaf");
const [pkg] = await getPackages(cwd);
pkg.version = "2.0.0";
const returnedLockfilePath = await updateLockfileVersion(pkg);
expect(returnedLockfilePath).toBe(path.join(pkg.location, "package-lock.json"));
expect(Array.from(loadJsonFile.registry.keys())).toStrictEqual(["/packages/package-1"]);
expect(fs.readJSONSync(returnedLockfilePath)).toHaveProperty("version", "2.0.0");
});
test("updateLockfileVersion with lockfile v2", async () => {
const cwd = await initFixture("lockfile-leaf-v2");
const [pkg] = await getPackages(cwd);
pkg.version = "2.0.0";
const returnedLockfilePath = await updateLockfileVersion(pkg);
expect(returnedLockfilePath).toBe(path.join(pkg.location, "package-lock.json"));
expect(Array.from(loadJsonFile.registry.keys())).toStrictEqual(["/packages/package-1"]);
const updatedLockfile = fs.readJSONSync(returnedLockfilePath);
expect(updatedLockfile).toHaveProperty("version", "2.0.0");
expect(updatedLockfile).toHaveProperty(["packages", "", "version"], "2.0.0");
});
test("updateLockfileVersion without sibling lockfile", async () => {
const cwd = await initFixture("lifecycle", false);
const [pkg] = await getPackages(cwd);
pkg.version = "1.1.0";
loadJsonFile.mockImplementationOnce(() => Promise.reject(new Error("file not found")));
const returnedLockfilePath = await updateLockfileVersion(pkg);
expect(returnedLockfilePath).toBeUndefined();
expect(fs.pathExistsSync(path.join(pkg.location, "package-lock.json"))).toBe(false);
});