Skip to content

Commit 75e16b1

Browse files
committed
Make throws test specific to error message
1 parent f0a24bb commit 75e16b1

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/compileStats.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ function loadStatsFromFile(webpackOutputPath) {
99
const data = fs.readFileSync(statsFile);
1010
const stats = JSON.parse(data);
1111

12-
if (!stats.stats) {
13-
const error = new this.serverless.classes.Error('Packaging: No stats information found');
14-
throw error;
12+
if (!stats.stats || !stats.stats.length) {
13+
throw new this.serverless.classes.Error('Packaging: No stats information found');
1514
}
1615

1716
const mappedStats = _.map(stats.stats, s =>
@@ -23,7 +22,7 @@ function loadStatsFromFile(webpackOutputPath) {
2322

2423
module.exports = {
2524
get() {
26-
const stats = this.stats || loadStatsFromFile(this.webpackOutputPath);
25+
const stats = this.stats || loadStatsFromFile.call(this, this.webpackOutputPath);
2726

2827
return stats;
2928
},

lib/compileStats.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const BbPromise = require('bluebird');
55
const chai = require('chai');
66
const sinon = require('sinon');
77
const path = require('path');
8+
const Serverless = require('serverless');
89

910
// Mocks
1011
const fsMockFactory = require('../tests/mocks/fs.mock');
@@ -19,6 +20,7 @@ describe('compileStats', () => {
1920
let baseModule;
2021
let module;
2122
let sandbox;
23+
let serverless;
2224
let fsMock;
2325

2426
before(() => {
@@ -35,8 +37,13 @@ describe('compileStats', () => {
3537
});
3638

3739
beforeEach(() => {
40+
serverless = new Serverless();
41+
serverless.cli = {
42+
log: sandbox.stub()
43+
};
3844
module = _.assign(
3945
{
46+
serverless,
4047
options: {}
4148
},
4249
baseModule
@@ -79,15 +86,15 @@ describe('compileStats', () => {
7986
it('should fail if compile stats are not loaded', () => {
8087
const webpackOutputPath = '.webpack';
8188

82-
const statsFile = {};
89+
const statsFile = { stats: [] };
8390

8491
module.webpackOutputPath = webpackOutputPath;
8592

8693
const fullStatsPath = `${webpackOutputPath}/stats.json`;
8794

8895
fsMock.readFileSync.withArgs(fullStatsPath).returns(JSON.stringify(statsFile));
8996

90-
expect(() => module.get()).throws();
97+
expect(() => module.get()).to.throw(/Packaging: No stats information found/);
9198
});
9299
});
93100

0 commit comments

Comments
 (0)