|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const BbPromise = require('bluebird'); |
| 5 | +const chai = require('chai'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const path = require('path'); |
| 8 | +const Serverless = require('serverless'); |
| 9 | + |
| 10 | +// Mocks |
| 11 | +const fsMockFactory = require('../tests/mocks/fs.mock'); |
| 12 | +const mockery = require('mockery'); |
| 13 | + |
| 14 | +chai.use(require('chai-as-promised')); |
| 15 | +chai.use(require('sinon-chai')); |
| 16 | + |
| 17 | +const expect = chai.expect; |
| 18 | + |
| 19 | +describe('compileStats', () => { |
| 20 | + let baseModule; |
| 21 | + let module; |
| 22 | + let sandbox; |
| 23 | + let serverless; |
| 24 | + let fsMock; |
| 25 | + |
| 26 | + before(() => { |
| 27 | + sandbox = sinon.createSandbox(); |
| 28 | + sandbox.usingPromise(BbPromise.Promise); |
| 29 | + |
| 30 | + fsMock = fsMockFactory.create(sandbox); |
| 31 | + |
| 32 | + mockery.enable({ warnOnUnregistered: false }); |
| 33 | + mockery.registerMock('fs', fsMock); |
| 34 | + |
| 35 | + baseModule = require('./compileStats'); |
| 36 | + Object.freeze(baseModule); |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + serverless = new Serverless(); |
| 41 | + serverless.cli = { |
| 42 | + log: sandbox.stub() |
| 43 | + }; |
| 44 | + module = _.assign( |
| 45 | + { |
| 46 | + serverless, |
| 47 | + options: {} |
| 48 | + }, |
| 49 | + baseModule |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + mockery.disable(); |
| 55 | + mockery.deregisterAll(); |
| 56 | + sandbox.restore(); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('get', () => { |
| 60 | + it('should return this.stats if available', () => { |
| 61 | + const stats = { stats: [{}] }; |
| 62 | + module.stats = stats; |
| 63 | + |
| 64 | + const result = module.get(); |
| 65 | + |
| 66 | + expect(result).to.equal(stats); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should load stats from file if this.stats is not present', () => { |
| 70 | + const webpackOutputPath = '.webpack'; |
| 71 | + |
| 72 | + const statsFile = { stats: [{ outputPath: 'service/path' }] }; |
| 73 | + const mappedFile = { stats: [{ outputPath: path.resolve(webpackOutputPath, 'service/path') }] }; |
| 74 | + module.webpackOutputPath = webpackOutputPath; |
| 75 | + |
| 76 | + const fullStatsPath = `${webpackOutputPath}/stats.json`; |
| 77 | + |
| 78 | + fsMock.readFileSync.withArgs(fullStatsPath).returns(JSON.stringify(statsFile)); |
| 79 | + |
| 80 | + const stats = module.get(); |
| 81 | + |
| 82 | + expect(fsMock.readFileSync).to.be.calledWith(fullStatsPath); |
| 83 | + expect(stats).to.deep.equal(mappedFile); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should fail if compile stats are not loaded', () => { |
| 87 | + const webpackOutputPath = '.webpack'; |
| 88 | + |
| 89 | + const statsFile = { stats: [] }; |
| 90 | + |
| 91 | + module.webpackOutputPath = webpackOutputPath; |
| 92 | + |
| 93 | + const fullStatsPath = `${webpackOutputPath}/stats.json`; |
| 94 | + |
| 95 | + fsMock.readFileSync.withArgs(fullStatsPath).returns(JSON.stringify(statsFile)); |
| 96 | + |
| 97 | + expect(() => module.get()).to.throw(/Packaging: No stats information found/); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('save', () => { |
| 102 | + it('should set this.stats', () => { |
| 103 | + // TODO: |
| 104 | + return; |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments