Skip to content

Commit 01ec75f

Browse files
Added tests for the NPM package
1 parent 29ced3f commit 01ec75f

File tree

6 files changed

+317
-2
lines changed

6 files changed

+317
-2
lines changed

test/specs/action/errors.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const exec = require("../../utils/exec");
77
const { expect } = require("chai");
88
const { EOL } = require("os");
99

10-
describe("Failure tests", () => {
10+
describe("GitHub Action - failure tests", () => {
1111

1212
it("should fail if the NPM token isn't set", () => {
1313
let cli = exec.action({

test/specs/action/success.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { expect } = require("chai");
88
const { EOL } = require("os");
99
const { join } = require("path");
1010

11-
describe("Success tests", () => {
11+
describe("GitHub Action - success tests", () => {
1212

1313
it("should publish a new version to NPM", () => {
1414
files.create([

test/specs/lib/exports.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
const commonJSExport = require("../../../");
4+
const { expect } = require("chai");
5+
const { default: defaultExport, npmPublish: namedExport } = require("../../../");
6+
7+
describe("NPM package - exports", () => {
8+
9+
it("should export the npmPublish() function as the default CommonJS export", () => {
10+
expect(commonJSExport).to.be.a("function");
11+
expect(commonJSExport.name).to.equal("npmPublish");
12+
});
13+
14+
it("should export the npmPublish() function as the default ESM export", () => {
15+
expect(defaultExport).to.be.a("function");
16+
expect(defaultExport).to.equal(commonJSExport);
17+
});
18+
19+
it("should export the npmPublish() function as a named export", () => {
20+
expect(namedExport).to.be.a("function");
21+
expect(namedExport).to.equal(commonJSExport);
22+
});
23+
24+
it("should not export anything else", () => {
25+
expect(Object.keys(commonJSExport)).to.have.same.members([
26+
"default",
27+
"npmPublish",
28+
]);
29+
});
30+
31+
});

test/specs/lib/success.spec.js

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
"use strict";
2+
3+
const npmPublish = require("../../../");
4+
const npm = require("../../utils/npm");
5+
const files = require("../../utils/files");
6+
const paths = require("../../utils/paths");
7+
const { expect } = require("chai");
8+
const { EOL } = require("os");
9+
const { join } = require("path");
10+
11+
describe("NPM package - success tests", async () => {
12+
let previousCWD;
13+
14+
beforeEach(() => {
15+
previousCWD = process.cwd();
16+
process.chdir(paths.workspace);
17+
});
18+
19+
afterEach(() => {
20+
process.chdir(previousCWD);
21+
});
22+
23+
it("should publish a new version to NPM", async () => {
24+
files.create([
25+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
26+
]);
27+
28+
npm.mock({
29+
args: ["view", "my-lib", "version"],
30+
stdout: `1.0.0${EOL}`,
31+
});
32+
33+
npm.mock({
34+
args: ["config", "get", "userconfig"],
35+
stdout: `${paths.npmrc}${EOL}`,
36+
});
37+
38+
npm.mock({
39+
args: ["publish"],
40+
stdout: `my-lib 2.0.0${EOL}`,
41+
});
42+
43+
let results = await npmPublish({ quiet: true });
44+
45+
expect(results).to.deep.equal({
46+
type: "major",
47+
package: "my-lib",
48+
version: "2.0.0",
49+
oldVersion: "1.0.0"
50+
});
51+
52+
files.assert.contents("home/.npmrc",
53+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
54+
`registry=https://registry.npmjs.org/${EOL}`
55+
);
56+
57+
npm.assert.ran(3);
58+
});
59+
60+
it("should not publish a new version to NPM if the version number hasn't changed", async () => {
61+
files.create([
62+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
63+
]);
64+
65+
npm.mock({
66+
args: ["view", "my-lib", "version"],
67+
stdout: `1.0.0${EOL}`,
68+
});
69+
70+
let results = await npmPublish({ quiet: true });
71+
72+
expect(results).to.deep.equal({
73+
type: "none",
74+
package: "my-lib",
75+
version: "1.0.0",
76+
oldVersion: "1.0.0"
77+
});
78+
79+
files.assert.doesNotExist("home/.npmrc");
80+
npm.assert.ran(1);
81+
});
82+
83+
it("should use the specified NPM token to publish the package", async () => {
84+
files.create([
85+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0-beta.1" }},
86+
]);
87+
88+
npm.mock({
89+
args: ["view", "my-lib", "version"],
90+
stdout: `1.0.0${EOL}`,
91+
});
92+
93+
npm.mock({
94+
args: ["config", "get", "userconfig"],
95+
stdout: `${paths.npmrc}${EOL}`,
96+
});
97+
98+
npm.mock({
99+
args: ["publish"],
100+
env: { INPUT_TOKEN: "my-secret-token" },
101+
stdout: `my-lib 1.0.0-beta.1${EOL}`,
102+
});
103+
104+
let results = await npmPublish({
105+
quiet: true,
106+
token: "my-secret-token",
107+
});
108+
109+
expect(results).to.deep.equal({
110+
type: "prerelease",
111+
package: "my-lib",
112+
version: "1.0.0-beta.1",
113+
oldVersion: "1.0.0"
114+
});
115+
116+
files.assert.contents("home/.npmrc",
117+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
118+
`registry=https://registry.npmjs.org/${EOL}`
119+
);
120+
121+
npm.assert.ran(3);
122+
});
123+
124+
it("should append to an existing .npmrc file", async () => {
125+
files.create([
126+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.1.0" }},
127+
{ path: "home/.npmrc", contents: "This is my NPM config.\nThere are many like it,\nbut this one is mine." },
128+
]);
129+
130+
npm.mock({
131+
args: ["view", "my-lib", "version"],
132+
stdout: `1.0.0${EOL}`,
133+
});
134+
135+
npm.mock({
136+
args: ["config", "get", "userconfig"],
137+
stdout: `${paths.npmrc}${EOL}`,
138+
});
139+
140+
npm.mock({
141+
args: ["publish"],
142+
stdout: `my-lib 1.1.0${EOL}`,
143+
});
144+
145+
let results = await npmPublish({ quiet: true });
146+
147+
expect(results).to.deep.equal({
148+
type: "minor",
149+
package: "my-lib",
150+
version: "1.1.0",
151+
oldVersion: "1.0.0"
152+
});
153+
154+
files.assert.contents("home/.npmrc",
155+
`This is my NPM config.${EOL}` +
156+
`There are many like it,${EOL}` +
157+
`but this one is mine.${EOL}` +
158+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
159+
`registry=https://registry.npmjs.org/${EOL}`
160+
);
161+
162+
npm.assert.ran(3);
163+
});
164+
165+
it("should update an existing .npmrc file's settings", async () => {
166+
files.create([
167+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.1" }},
168+
{
169+
path: "home/.npmrc",
170+
contents:
171+
"# Use the GitHub package registry\n" +
172+
"registry=https://registry.github.com/\n" +
173+
"https://registry.github.com/:_authToken=my-github-token\n" +
174+
"\n" +
175+
"# Use the NPM registry with no auth\n" +
176+
"registry=https://registry.npmjs.org/\n" +
177+
"\n" +
178+
"# Use some other package registry\n" +
179+
"registry=https://registry.example.com/\n"
180+
},
181+
]);
182+
183+
npm.mock({
184+
args: ["view", "my-lib", "version"],
185+
stdout: `1.0.0${EOL}`,
186+
});
187+
188+
npm.mock({
189+
args: ["config", "get", "userconfig"],
190+
stdout: `${paths.npmrc}${EOL}`,
191+
});
192+
193+
npm.mock({
194+
args: ["publish"],
195+
stdout: `my-lib 1.0.1${EOL}`,
196+
});
197+
198+
let results = await npmPublish({ quiet: true });
199+
200+
expect(results).to.deep.equal({
201+
type: "patch",
202+
package: "my-lib",
203+
version: "1.0.1",
204+
oldVersion: "1.0.0"
205+
});
206+
207+
files.assert.contents("home/.npmrc",
208+
`# Use the GitHub package registry${EOL}` +
209+
`${EOL}` +
210+
`# Use the NPM registry with no auth${EOL}` +
211+
`${EOL}` +
212+
`# Use some other package registry${EOL}` +
213+
`${EOL}` +
214+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
215+
`registry=https://registry.npmjs.org/${EOL}`
216+
);
217+
218+
npm.assert.ran(3);
219+
});
220+
221+
it("should publish a package that's not in the root of the workspace directory", async () => {
222+
files.create([
223+
{ path: "workspace/subdir/my-lib/package.json", contents: { name: "my-lib", version: "1.0.0-beta" }},
224+
]);
225+
226+
npm.mock({
227+
args: ["view", "my-lib", "version"],
228+
stdout: `1.0.0${EOL}`,
229+
});
230+
231+
npm.mock({
232+
args: ["config", "get", "userconfig"],
233+
stdout: `${paths.npmrc}${EOL}`,
234+
});
235+
236+
npm.mock({
237+
args: ["publish"],
238+
cwd: join(paths.workspace, "subdir/my-lib"),
239+
stdout: `my-lib 1.0.0-beta${EOL}`,
240+
});
241+
242+
let results = await npmPublish({
243+
quiet: true,
244+
package: "subdir/my-lib/package.json",
245+
});
246+
247+
expect(results).to.deep.equal({
248+
type: "prerelease",
249+
package: "my-lib",
250+
version: "1.0.0-beta",
251+
oldVersion: "1.0.0"
252+
});
253+
254+
files.assert.contents("home/.npmrc",
255+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
256+
`registry=https://registry.npmjs.org/${EOL}`
257+
);
258+
259+
npm.assert.ran(3);
260+
});
261+
262+
});

test/utils/exec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ module.exports = {
2626

2727
return chaiExec("node", [paths.action], options);
2828
},
29+
30+
/**
31+
* Executes the CLI with the specified options
32+
*/
33+
cli (options) {
34+
// Deep merge the options object, since Chai Exec only does a shallow merge
35+
options = {
36+
cwd: paths.workspace,
37+
...options,
38+
env: {
39+
...process.env,
40+
...options.env,
41+
}
42+
};
43+
44+
return chaiExec("node", [paths.cli], options);
45+
},
2946
};

test/utils/paths.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ module.exports = {
1010
*/
1111
action: path.join(ROOT_DIR, "dist/index.js"),
1212

13+
/**
14+
* The path of the CLI JavaScript file
15+
*/
16+
cli: path.join(ROOT_DIR, "bin/npm-publish.js"),
17+
1318
/**
1419
* The path of our mock bin directory
1520
*/

0 commit comments

Comments
 (0)