Skip to content

Commit 5319910

Browse files
committed
add test cases
1 parent b1e84ea commit 5319910

File tree

2 files changed

+39
-67
lines changed

2 files changed

+39
-67
lines changed

bin/helpers/packageInstaller.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ const setupPackageFolder = (runSettings, directoryPath) => {
5252

5353
const packageInstall = (packageDir) => {
5454
return new Promise(function (resolve, reject) {
55-
nodeProcess = spawn('npm', ['install'], {cwd: packageDir});
5655
const nodeProcessCloseCallback = (code) => {
5756
if(code == 0) {
58-
resolve('Packages were installed');
57+
resolve('Packages were installed successfully.');
5958
} else {
60-
reject('Packages were not installed');
59+
reject('Packages were not installed successfully.');
6160
}
6261
};
6362
const nodeProcessErrorCallback = (error) => {
6463
logger.error(`Some error occurred while installing packages: ${error}`);
65-
reject('Packages were not installed');
64+
reject(`Packages were not installed successfully.`);
6665
};
67-
nodeProcess.on('error', nodeProcessErrorCallback);
66+
nodeProcess = spawn('npm', ['install'], {cwd: packageDir});
6867
nodeProcess.on('close', nodeProcessCloseCallback);
68+
nodeProcess.on('error', nodeProcessErrorCallback);
6969
});
7070
};
7171

test/unit/bin/helpers/packageInstaller.js

Lines changed: 34 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const chai = require("chai"),
44
sinon = require("sinon"),
55
fs = require('fs-extra'),
66
path = require('path'),
7-
npm = require('npm');
7+
cp = require('child_process');
88

99
const logger = require("../../../../bin/helpers/logger").winstonLogger,
1010
fileHelpers = require("../../../../bin/helpers/fileHelpers");
@@ -150,7 +150,7 @@ describe("packageInstaller", () => {
150150
sinon.assert.calledOnce(pathdirnameStub);
151151
sinon.assert.calledThrice(pathjoinStub);
152152
sinon.assert.calledWith(fswriteFileSyncStub, null, packageCreated);
153-
chai.assert.equal(data, "package file created");
153+
chai.assert.equal(data, "Package file created");
154154
})
155155
.catch((_error) => {
156156
console.log(_error)
@@ -202,7 +202,7 @@ describe("packageInstaller", () => {
202202
sinon.assert.calledOnce(pathdirnameStub);
203203
sinon.assert.calledThrice(pathjoinStub);
204204
sinon.assert.calledWith(fswriteFileSyncStub, null, packageCreated);
205-
chai.assert.equal(data, "package file created");
205+
chai.assert.equal(data, "Package file created");
206206
})
207207
.catch((_error) => {
208208
console.log(_error)
@@ -212,80 +212,52 @@ describe("packageInstaller", () => {
212212
});
213213

214214
context("packageInstall", () => {
215-
let npmInstallStub;
216215
const packageInstaller = rewire("../../../../bin/helpers/packageInstaller");
217-
beforeEach(() => {
218-
npmInstallStub = sandbox.stub(npm.commands, "install").returns(null);
219-
});
220216

221-
it("should reject if error in npm load", () => {
222-
packageInstaller.__set__({
223-
npm: {
224-
commands: {
225-
install: npmInstallStub
226-
},
227-
load: (_npmLoad, loadCallback) => {
228-
loadCallback("test error");
229-
}
230-
},
217+
it("should call npm install on directory and resolve if spawn is closed successfully", () => {
218+
let spawnStub = sandbox.stub(cp, 'spawn').returns({
219+
on: (_close, nodeProcessCloseCallback) => {
220+
nodeProcessCloseCallback(0);
221+
}
231222
});
232-
let packageInstallrewire = packageInstaller.__get__('packageInstall');
233-
let directoryPath = "/random/path";
234-
return packageInstallrewire(directoryPath)
235-
.then((_data) => {
236-
chai.assert.fail("Promise error");
237-
})
238-
.catch((error) => {
239-
chai.assert.equal(error, "test error")
240-
});
241-
});
242-
243-
it("should call npm install on directory", () => {
244223
packageInstaller.__set__({
245-
npm: {
246-
commands: {
247-
install: (_packageDir, [], installCallback) => {
248-
installCallback(null, "npm install done");
249-
}
250-
},
251-
load: (_npmLoad, loadCallback) => {
252-
loadCallback(null);
253-
}
254-
},
224+
nodeProcess: {},
225+
spawn: spawnStub
255226
});
256227
let packageInstallrewire = packageInstaller.__get__('packageInstall');
257228
let directoryPath = "/random/path";
258229
return packageInstallrewire(directoryPath)
259-
.then((data) => {
260-
chai.assert.equal(data, "npm install done")
261-
})
262-
.catch((_error) => {
263-
chai.assert.fail("Promise error");
264-
});
230+
.then((data) => {
231+
console.log(data);
232+
chai.assert.equal(data, "Packages were installed successfully.")
233+
spawnStub.restore();
234+
})
235+
.catch((_error) => {
236+
chai.assert.fail(`Promise error ${_error}`);
237+
});
265238
});
266239

267-
it("should reject if error in npm install", () => {
240+
it("should call npm install on directory and reject if spawn is not closed successfully", () => {
241+
let spawnStub = sandbox.stub(cp, 'spawn').returns({
242+
on: (_close, nodeProcessCloseCallback) => {
243+
nodeProcessCloseCallback(1);
244+
}
245+
});
268246
packageInstaller.__set__({
269-
npm: {
270-
commands: {
271-
install: (_packageDir, [], installCallback) => {
272-
installCallback("test error", "npm install failed");
273-
}
274-
},
275-
load: (_npmLoad, loadCallback) => {
276-
loadCallback(null);
277-
}
278-
},
247+
nodeProcess: {},
248+
spawn: spawnStub
279249
});
280250
let packageInstallrewire = packageInstaller.__get__('packageInstall');
281251
let directoryPath = "/random/path";
282252
return packageInstallrewire(directoryPath)
283-
.then((_data) => {
284-
chai.assert.fail("Promise error");
285-
})
286-
.catch((error) => {
287-
chai.assert.equal(error, "test error")
288-
});
253+
.then((_data) => {
254+
spawnStub.restore();
255+
chai.assert.fail("Promise error");
256+
})
257+
.catch((error) => {
258+
spawnStub.restore();
259+
chai.assert.equal(error, "Packages were not installed successfully.")
260+
});
289261
});
290262
});
291263

0 commit comments

Comments
 (0)