|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BbPromise = require('bluebird'); |
| 4 | +const _ = require('lodash'); |
| 5 | +const chai = require('chai'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const mockery = require('mockery'); |
| 8 | +const Serverless = require('serverless'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +chai.use(require('chai-as-promised')); |
| 12 | +chai.use(require('sinon-chai')); |
| 13 | + |
| 14 | +const expect = chai.expect; |
| 15 | + |
| 16 | +describe('runPluginSupport', () => { |
| 17 | + let sandbox; |
| 18 | + let baseModule; |
| 19 | + let serverless; |
| 20 | + let module; |
| 21 | + let chdirStub; |
| 22 | + let getLocalRootUrlStub; |
| 23 | + let deployFunctionsToLocalEmulatorStub; |
| 24 | + |
| 25 | + before(() => { |
| 26 | + sandbox = sinon.createSandbox(); |
| 27 | + sandbox.usingPromise(BbPromise.Promise); |
| 28 | + |
| 29 | + const pluginRunUtils = path.join( |
| 30 | + '.', |
| 31 | + 'plugins', |
| 32 | + 'run', |
| 33 | + 'utils' |
| 34 | + ); |
| 35 | + |
| 36 | + deployFunctionsToLocalEmulatorStub = sandbox.stub().resolves(); |
| 37 | + getLocalRootUrlStub = sandbox.stub(); |
| 38 | + |
| 39 | + mockery.enable({ warnOnUnregistered: false }); |
| 40 | + mockery.registerMock(path.join(pluginRunUtils, 'deployFunctionsToLocalEmulator'), deployFunctionsToLocalEmulatorStub); |
| 41 | + mockery.registerMock(path.join(pluginRunUtils, 'getLocalRootUrl'), getLocalRootUrlStub); |
| 42 | + baseModule = require('../lib/runPluginSupport'); |
| 43 | + Object.freeze(baseModule); |
| 44 | + }); |
| 45 | + |
| 46 | + after(() => { |
| 47 | + mockery.disable(); |
| 48 | + mockery.deregisterAll(); |
| 49 | + }); |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + serverless = new Serverless(); |
| 53 | + serverless.cli = { |
| 54 | + log: sandbox.stub(), |
| 55 | + consoleLog: sandbox.stub() |
| 56 | + }; |
| 57 | + |
| 58 | + module = _.assign({ |
| 59 | + serverless, |
| 60 | + options: {}, |
| 61 | + }, baseModule); |
| 62 | + |
| 63 | + _.set(serverless, 'config.serverlessPath', '.'); |
| 64 | + |
| 65 | + chdirStub = sandbox.stub(process, 'chdir'); |
| 66 | + }); |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + chdirStub.reset(); |
| 70 | + sandbox.restore(); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('prepareRun', () => { |
| 74 | + it('should prepare environment and save original values', () => { |
| 75 | + const prepareRun = module.prepareRun.bind(module); |
| 76 | + const servicePath = path.join('my', 'servicePath'); |
| 77 | + const webpackOutputPath = path.join('webpack', 'output', 'path'); |
| 78 | + |
| 79 | + _.set(serverless, 'config.servicePath', servicePath); |
| 80 | + _.set(module, 'webpackOutputPath', webpackOutputPath); |
| 81 | + _.unset(module, 'keepOutputDirectory'); |
| 82 | + |
| 83 | + return expect(prepareRun()).to.be.fulfilled |
| 84 | + .then(() => BbPromise.join( |
| 85 | + expect(module.originalServicePath).to.equal(servicePath), |
| 86 | + expect(module.originalWebpackOutputPath).to.equal(webpackOutputPath), |
| 87 | + expect(module.keepOutputDirectory).to.be.true, |
| 88 | + expect(serverless.config.servicePath).to.equal(path.join(webpackOutputPath, 'service')), |
| 89 | + expect(chdirStub).to.have.been.calledWith(serverless.config.servicePath) |
| 90 | + )); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('watchRun', () => { |
| 95 | + beforeEach(() => { |
| 96 | + _.set(module, 'webpackConfig.output.path', 'outputPath'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should invoke hook and deploy functions', () => { |
| 100 | + const watchRun = module.watchRun.bind(module); |
| 101 | + const service = { |
| 102 | + name: 'testService', |
| 103 | + functions: {} |
| 104 | + }; |
| 105 | + _.set(module, 'hooks[before:run:run]', sandbox.stub().resolves()); |
| 106 | + _.set(serverless, 'service', service); |
| 107 | + |
| 108 | + return expect(watchRun()).to.be.fulfilled |
| 109 | + .then(() => BbPromise.join( |
| 110 | + expect(deployFunctionsToLocalEmulatorStub).to.have.been.calledOnce, |
| 111 | + expect(getLocalRootUrlStub).to.have.been.calledOnce, |
| 112 | + expect(deployFunctionsToLocalEmulatorStub).to.have.been.calledWith(service) |
| 113 | + )); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments