Skip to content

Commit 2f26f4f

Browse files
Added failure tests for the NPM package
1 parent 3195142 commit 2f26f4f

File tree

2 files changed

+235
-1
lines changed

2 files changed

+235
-1
lines changed

test/specs/lib/failure.spec.js

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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, assert } = require("chai");
8+
const { EOL } = require("os");
9+
10+
describe("NPM package - failure tests", () => {
11+
let previousCWD;
12+
13+
beforeEach(() => {
14+
previousCWD = process.cwd();
15+
process.chdir(paths.workspace);
16+
});
17+
18+
afterEach(() => {
19+
process.chdir(previousCWD);
20+
});
21+
22+
it("should fail if the NPM registry URL is invalid", async () => {
23+
files.create([
24+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.2.3" }},
25+
]);
26+
27+
try {
28+
await npmPublish({ registry: "example.com" });
29+
assert.fail("An error should have been thrown!");
30+
}
31+
catch (error) {
32+
expect(error).to.be.an.instanceOf(TypeError);
33+
expect(error.message).to.equal("Invalid URL: example.com");
34+
}
35+
36+
files.assert.doesNotExist("home/.npmrc");
37+
npm.assert.ran(0);
38+
});
39+
40+
it("should fail if the package.json file does not exist", async () => {
41+
try {
42+
await npmPublish();
43+
assert.fail("An error should have been thrown!");
44+
}
45+
catch (error) {
46+
expect(error).to.be.an.instanceOf(Error);
47+
expect(error.message).to.include("Unable to read package.json \nENOENT: no such file or directory");
48+
}
49+
50+
files.assert.doesNotExist("home/.npmrc");
51+
files.assert.doesNotExist("workspace/package.json");
52+
npm.assert.didNotRun();
53+
});
54+
55+
it("should fail if the package.json file is invalid", async () => {
56+
files.create([
57+
{ path: "workspace/package.json", contents: "hello, world" },
58+
]);
59+
60+
try {
61+
await npmPublish();
62+
assert.fail("An error should have been thrown!");
63+
}
64+
catch (error) {
65+
expect(error).to.be.an.instanceOf(SyntaxError);
66+
expect(error.message).to.equal("Unable to parse package.json \nUnexpected token h in JSON at position 0");
67+
}
68+
69+
files.assert.doesNotExist("home/.npmrc");
70+
npm.assert.didNotRun();
71+
});
72+
73+
it("should fail if the package name is invalid", async () => {
74+
files.create([
75+
{ path: "workspace/package.json", contents: { name: "\n \t" }},
76+
]);
77+
78+
try {
79+
await npmPublish();
80+
assert.fail("An error should have been thrown!");
81+
}
82+
catch (error) {
83+
expect(error).to.be.an.instanceOf(TypeError);
84+
expect(error.message).to.equal("Unable to parse package.json \nInvalid package name");
85+
}
86+
87+
files.assert.doesNotExist("home/.npmrc");
88+
npm.assert.didNotRun();
89+
});
90+
91+
it("should fail if the package version is invalid", async () => {
92+
files.create([
93+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "hello, world" }},
94+
]);
95+
96+
try {
97+
await npmPublish();
98+
assert.fail("An error should have been thrown!");
99+
}
100+
catch (error) {
101+
expect(error).to.be.an.instanceOf(TypeError);
102+
expect(error.message).to.equal("Unable to parse package.json \nInvalid Version: hello, world");
103+
}
104+
105+
files.assert.doesNotExist("home/.npmrc");
106+
npm.assert.didNotRun();
107+
});
108+
109+
it('should fail if the "npm view" command errors', async () => {
110+
files.create([
111+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
112+
]);
113+
114+
npm.mock({
115+
args: ["view", "my-lib", "version"],
116+
stderr: "BOOM!",
117+
exitCode: 1,
118+
});
119+
120+
try {
121+
await npmPublish();
122+
assert.fail("An error should have been thrown!");
123+
}
124+
catch (error) {
125+
expect(error).to.be.an.instanceOf(Error);
126+
expect(error.message).to.equal(
127+
"Unable to determine the current version of my-lib on NPM. \n" +
128+
"npm view my-lib version exited with a status of 1.\n\n" +
129+
"BOOM!"
130+
);
131+
}
132+
133+
npm.assert.ran(1);
134+
});
135+
136+
it("should fail if the .npmrc file is invalid", async () => {
137+
files.create([
138+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
139+
{ path: "home/.npmrc/file.txt", contents: "~/.npmrc is a directory, not a file" },
140+
]);
141+
142+
npm.mock({
143+
args: ["view", "my-lib", "version"],
144+
stdout: `1.0.0${EOL}`,
145+
});
146+
147+
npm.mock({
148+
args: ["config", "get", "userconfig"],
149+
stdout: `${paths.npmrc}${EOL}`,
150+
});
151+
152+
try {
153+
await npmPublish();
154+
assert.fail("An error should have been thrown!");
155+
}
156+
catch (error) {
157+
expect(error).to.be.an.instanceOf(Error);
158+
expect(error.message).to.include("Unable to read the NPM config file: ");
159+
expect(error.message).to.include("EISDIR: illegal operation on a directory, read");
160+
}
161+
162+
npm.assert.ran(2);
163+
});
164+
165+
it('should fail if the "npm config" command errors', async () => {
166+
files.create([
167+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
168+
]);
169+
170+
npm.mock({
171+
args: ["view", "my-lib", "version"],
172+
stdout: `1.0.0${EOL}`,
173+
});
174+
175+
npm.mock({
176+
args: ["config", "get", "userconfig"],
177+
stderr: "BOOM!",
178+
exitCode: 1,
179+
});
180+
181+
try {
182+
await npmPublish();
183+
assert.fail("An error should have been thrown!");
184+
}
185+
catch (error) {
186+
expect(error).to.be.an.instanceOf(Error);
187+
expect(error.message).to.equal(
188+
"Unable to determine the NPM config file path. \n" +
189+
"npm config get userconfig exited with a status of 1.\n\n" +
190+
"BOOM!"
191+
);
192+
}
193+
194+
npm.assert.ran(2);
195+
});
196+
197+
it('should fail if the "npm publish" command errors', async () => {
198+
files.create([
199+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
200+
]);
201+
202+
npm.mock({
203+
args: ["view", "my-lib", "version"],
204+
stdout: `1.0.0${EOL}`,
205+
});
206+
207+
npm.mock({
208+
args: ["config", "get", "userconfig"],
209+
stdout: `${paths.npmrc}${EOL}`,
210+
});
211+
212+
npm.mock({
213+
args: ["publish"],
214+
stderr: "BOOM!",
215+
exitCode: 1,
216+
});
217+
218+
try {
219+
await npmPublish({ quiet: true });
220+
assert.fail("An error should have been thrown!");
221+
}
222+
catch (error) {
223+
expect(error).to.be.an.instanceOf(Error);
224+
expect(error.message).to.equal(
225+
"Unable to publish my-lib v2.0.0 to NPM. \n" +
226+
"npm publish exited with a status of 1.\n\n" +
227+
"BOOM!"
228+
);
229+
}
230+
231+
npm.assert.ran(3);
232+
});
233+
234+
});

test/specs/lib/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("NPM package - success tests", async () => {
11+
describe("NPM package - success tests", () => {
1212
let previousCWD;
1313

1414
beforeEach(() => {

0 commit comments

Comments
 (0)