Skip to content

Commit 77121fc

Browse files
author
Frank Schmid
committed
Use sinon and chai in unit tests
1 parent ecec627 commit 77121fc

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

lib/validate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ module.exports = {
107107
if (_.isString(this.webpackConfig)) {
108108
const webpackConfigFilePath = path.join(this.serverless.config.servicePath, this.webpackConfig);
109109
if (!this.serverless.utils.fileExistsSync(webpackConfigFilePath)) {
110-
throw new this.serverless.classes
111-
.Error('The webpack plugin could not find the configuration file at: ' + webpackConfigFilePath);
110+
return BbPromise.reject(new this.serverless.classes
111+
.Error('The webpack plugin could not find the configuration file at: ' + webpackConfigFilePath));
112112
}
113113
try {
114114
this.webpackConfig = require(webpackConfigFilePath);
@@ -223,7 +223,7 @@ module.exports = {
223223

224224
// Webpack config can be a Promise, If it's a Promise wait for resolved config object.
225225
if (this.webpackConfig && _.isFunction(this.webpackConfig.then)) {
226-
return BbPromise.resolve(this.webpackConfig.then(processConfig));
226+
return BbPromise.resolve(this.webpackConfig.then(config => processConfig(config)));
227227
} else {
228228
return processConfig(this.webpackConfig);
229229
}

tests/validate.test.js

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const _ = require('lodash');
4+
const BbPromise = require('bluebird');
45
const chai = require('chai');
56
const sinon = require('sinon');
67
const mockery = require('mockery');
@@ -232,14 +233,15 @@ describe('validate', () => {
232233
entry: 'testentry',
233234
};
234235
mockery.registerMock(requiredPath, loadedConfig);
235-
return module
236-
.validate()
237-
.then(() => {
238-
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
239-
expect(module.webpackConfig).to.eql(loadedConfig);
240-
mockery.deregisterMock(requiredPath);
241-
return null;
242-
});
236+
return expect(module.validate()).to.fulfilled
237+
.then(() => {
238+
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
239+
expect(module.webpackConfig).to.eql(loadedConfig);
240+
return null;
241+
})
242+
.finally(() => {
243+
mockery.deregisterMock(requiredPath);
244+
});
243245
});
244246

245247
it('should load a async webpack config from file if `custom.webpack` is a string', () => {
@@ -252,16 +254,17 @@ describe('validate', () => {
252254
const loadedConfig = {
253255
entry: 'testentry',
254256
};
255-
const loadedConfigPromise = Promise.resolve(loadedConfig);
257+
const loadedConfigPromise = BbPromise.resolve(loadedConfig);
256258
mockery.registerMock(requiredPath, loadedConfigPromise);
257-
return module
258-
.validate()
259-
.then(() => {
260-
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
261-
expect(module.webpackConfig).to.eql(loadedConfig);
262-
mockery.deregisterMock(requiredPath);
263-
return null;
264-
});
259+
return expect(module.validate()).to.be.fulfilled
260+
.then(() => {
261+
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
262+
expect(module.webpackConfig).to.deep.equal(loadedConfig);
263+
return null;
264+
})
265+
.finally(() => {
266+
mockery.deregisterMock(requiredPath);
267+
});
265268
});
266269

267270
it('should catch errors while loading a async webpack config from file if `custom.webpack` is a string', () => {
@@ -271,16 +274,16 @@ describe('validate', () => {
271274
module.serverless.config.servicePath = testServicePath;
272275
module.serverless.service.custom.webpack = testConfig;
273276
serverless.utils.fileExistsSync = sinon.stub().returns(true);
274-
const loadedConfigPromise = Promise.reject('config failed to load');
277+
const loadedConfigPromise = BbPromise.reject('config failed to load');
275278
mockery.registerMock(requiredPath, loadedConfigPromise);
276-
return module
277-
.validate()
278-
.catch(e => {
279-
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
280-
expect(e).to.eql('config failed to load');
281-
mockery.deregisterMock(requiredPath);
282-
return null;
283-
});
279+
return expect(module.validate()).to.be.rejectedWith('config failed to load')
280+
.then(() => {
281+
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
282+
return null;
283+
})
284+
.finally(() => {
285+
mockery.deregisterMock(requiredPath);
286+
});
284287
});
285288

286289
it('should load a wrong thenable webpack config as normal object from file if `custom.webpack` is a string', () => {
@@ -295,14 +298,15 @@ describe('validate', () => {
295298
entry: 'testentry',
296299
};
297300
mockery.registerMock(requiredPath, loadedConfig);
298-
return module
299-
.validate()
300-
.then(() => {
301-
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
302-
expect(module.webpackConfig).to.eql(loadedConfig);
303-
mockery.deregisterMock(requiredPath);
304-
return null;
305-
});
301+
return expect(module.validate()).to.be.fulfilled
302+
.then(() => {
303+
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
304+
expect(module.webpackConfig).to.deep.equal(loadedConfig);
305+
return null;
306+
})
307+
.finally(() => {
308+
mockery.deregisterMock(requiredPath);
309+
});
306310
});
307311

308312
it('should throw if providing an invalid file', () => {
@@ -311,7 +315,7 @@ describe('validate', () => {
311315
module.serverless.config.servicePath = testServicePath;
312316
module.serverless.service.custom.webpack = testConfig;
313317
serverless.utils.fileExistsSync = sinon.stub().returns(false);
314-
expect(module.validate.bind(module)).to.throw(/could not find/);
318+
return expect(module.validate()).to.be.rejectedWith(/could not find/);
315319
});
316320

317321
it('should load a default file if no custom config is provided', () => {
@@ -324,14 +328,15 @@ describe('validate', () => {
324328
entry: 'testentry',
325329
};
326330
mockery.registerMock(requiredPath, loadedConfig);
327-
return module
328-
.validate()
329-
.then(() => {
330-
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
331-
expect(module.webpackConfig).to.eql(loadedConfig);
332-
mockery.deregisterMock(requiredPath);
333-
return null;
334-
});
331+
return expect(module.validate()).to.be.fulfilled
332+
.then(() => {
333+
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
334+
expect(module.webpackConfig).to.eql(loadedConfig);
335+
return null;
336+
})
337+
.finally(() => {
338+
mockery.deregisterMock(requiredPath);
339+
});
335340
});
336341

337342
it('should fail when importing a broken configuration file', () => {

0 commit comments

Comments
 (0)