/
npm-dist-tag.test.js
155 lines (130 loc) · 4.06 KB
/
npm-dist-tag.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
jest.mock("npm-registry-fetch");
jest.mock("@lerna/otplease", () => (cb, opts) => Promise.resolve(cb(opts)));
// mocked modules
const fetch = require("npm-registry-fetch");
// file under test
const npmDistTag = require("..");
expect.extend(require("@lerna-test/figgy-pudding-matchers"));
const stubLog = {
verbose: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
};
const baseOptions = new Map([["log", stubLog], ["tag", "latest"]]);
fetch.mockImplementation(() => Promise.resolve());
fetch.json.mockImplementation(() => Promise.resolve({}));
describe("npmDistTag.add()", () => {
it("adds a dist-tag for a given package@version", async () => {
const opts = new Map(baseOptions);
const tags = await npmDistTag.add("@scope/some-pkg@1.0.1", "added-tag", opts);
expect(tags).toEqual({
"added-tag": "1.0.1",
});
expect(fetch).toHaveBeenLastCalledWith(
"/-/package/@scope%2fsome-pkg/dist-tags/added-tag",
expect.figgyPudding({
method: "PUT",
body: JSON.stringify("1.0.1"),
headers: {
"content-type": "application/json",
},
})
);
});
it("does not attempt to add duplicate of existing tag", async () => {
fetch.json.mockImplementationOnce(() =>
Promise.resolve({
latest: "1.0.0",
"dupe-tag": "1.0.1",
})
);
const opts = new Map(baseOptions);
const tags = await npmDistTag.add("@scope/some-pkg@1.0.1", "dupe-tag", opts);
expect(tags).toEqual({
latest: "1.0.0",
"dupe-tag": "1.0.1",
});
expect(fetch).not.toHaveBeenCalled();
expect(stubLog.warn).toHaveBeenLastCalledWith(
"dist-tag",
"@scope/some-pkg@dupe-tag already set to 1.0.1"
);
});
it("defaults tag argument to opts.tag", async () => {
fetch.json.mockImplementationOnce(() =>
Promise.resolve({
latest: "1.0.0",
})
);
const opts = new Map(baseOptions);
const tags = await npmDistTag.add("@scope/some-pkg@1.0.1", undefined, opts);
expect(tags).toEqual({
latest: "1.0.1",
});
});
});
describe("npmDistTag.remove()", () => {
it("removes an existing dist-tag for a given package", async () => {
fetch.json.mockImplementationOnce(() =>
Promise.resolve({
latest: "1.0.0",
"removed-tag": "1.0.1",
})
);
const opts = new Map(baseOptions);
const tags = await npmDistTag.remove("@scope/some-pkg@1.0.1", "removed-tag", opts);
expect(tags).not.toHaveProperty("removed-tag");
expect(fetch).toHaveBeenLastCalledWith(
"/-/package/@scope%2fsome-pkg/dist-tags/removed-tag",
expect.figgyPudding({
method: "DELETE",
})
);
});
it("does not attempt removal of nonexistent tag", async () => {
const opts = new Map(baseOptions);
const tags = await npmDistTag.remove("@scope/some-pkg@1.0.1", "missing-tag", opts);
expect(tags).toEqual({});
expect(fetch).not.toHaveBeenCalled();
expect(stubLog.info).toHaveBeenLastCalledWith(
"dist-tag",
'"missing-tag" is not a dist-tag on @scope/some-pkg'
);
});
});
describe("npmDistTag.list()", () => {
it("returns dictionary of dist-tags", async () => {
fetch.json.mockImplementationOnce(() =>
Promise.resolve({
latest: "1.0.0",
"other-tag": "1.0.1",
_etag: "should-be-removed",
})
);
const opts = new Map(baseOptions);
const tags = await npmDistTag.list("@scope/some-pkg", opts);
expect(tags).toEqual({
latest: "1.0.0",
"other-tag": "1.0.1",
});
expect(fetch.json).toHaveBeenLastCalledWith(
"/-/package/@scope%2fsome-pkg/dist-tags",
expect.figgyPudding({
"prefer-online": true,
spec: expect.objectContaining({
name: "@scope/some-pkg",
}),
})
);
});
it("handles disastrous results gracefully", async () => {
fetch.json.mockImplementationOnce(() =>
// i mean, wut
Promise.resolve(null)
);
const opts = new Map(baseOptions);
const tags = await npmDistTag.list("@scope/some-pkg", opts);
expect(tags).toEqual({});
});
});