/
update-lockfile-version.spec.js
75 lines (55 loc) · 2.84 KB
/
update-lockfile-version.spec.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"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('../../../core/src/project');
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const { updateClassicLockfileVersion, updateModernLockfileVersion } = require("../lib/update-lockfile-version");
describe('npm classic lock file', () => {
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 updateClassicLockfileVersion(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("updateClassicLockfileVersion with lockfile v2", async () => {
const cwd = await initFixture("lockfile-leaf-v2");
const [pkg] = await getPackages(cwd);
pkg.version = "2.0.0";
const returnedLockfilePath = await updateClassicLockfileVersion(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("updateClassicLockfileVersion 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 updateClassicLockfileVersion(pkg);
expect(returnedLockfilePath).toBeUndefined();
expect(fs.pathExistsSync(path.join(pkg.location, "package-lock.json"))).toBe(false);
});
});
describe('npm modern lock file', () => {
test("updateModernLockfileVersion v2 in project root", async () => {
const mockVersion = "2.4.0";
const cwd = await initFixture("lockfile-version2");
const rootLockFilePath = path.join(cwd, "package-lock.json");
const packages = await getPackages(cwd);
for (const pkg of packages) {
pkg.version = mockVersion;
const returnedLockfilePath = await updateModernLockfileVersion(pkg, { rootPath: cwd });
expect(returnedLockfilePath).toBe(rootLockFilePath);
}
expect(Array.from(loadJsonFile.registry.keys())).toStrictEqual(["/packages/package-1", "/packages/package-2", "/"]);
expect(fs.readJSONSync(rootLockFilePath)).toMatchSnapshot();
});
});