|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const sinon = require('sinon'); |
| 4 | + |
| 5 | +const GoogleProvider = require('../provider/googleProvider'); |
| 6 | +const GoogleInvokeLocal = require('./googleInvokeLocal'); |
| 7 | +const Serverless = require('../test/serverless'); |
| 8 | + |
| 9 | +describe('GoogleInvokeLocal', () => { |
| 10 | + let serverless; |
| 11 | + const functionName = 'myFunction'; |
| 12 | + const rawOptions = { |
| 13 | + f: functionName, |
| 14 | + }; |
| 15 | + const processedOptions = { |
| 16 | + function: functionName, |
| 17 | + }; |
| 18 | + let googleInvokeLocal; |
| 19 | + |
| 20 | + beforeAll(() => { |
| 21 | + serverless = new Serverless(); |
| 22 | + serverless.setProvider('google', new GoogleProvider(serverless)); |
| 23 | + googleInvokeLocal = new GoogleInvokeLocal(serverless, rawOptions); |
| 24 | + serverless.processedInput.options = processedOptions; |
| 25 | + }); |
| 26 | + |
| 27 | + describe('#constructor()', () => { |
| 28 | + it('should set the serverless instance', () => { |
| 29 | + expect(googleInvokeLocal.serverless).toEqual(serverless); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should set the raw options if provided', () => { |
| 33 | + expect(googleInvokeLocal.options).toEqual(rawOptions); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should make the provider accessible', () => { |
| 37 | + expect(googleInvokeLocal.provider).toBeInstanceOf(GoogleProvider); |
| 38 | + }); |
| 39 | + |
| 40 | + it.each` |
| 41 | + method |
| 42 | + ${'validate'} |
| 43 | + ${'setDefaults'} |
| 44 | + ${'getDataAndContext'} |
| 45 | + ${'invokeLocalNodeJs'} |
| 46 | + ${'loadFileInOption'} |
| 47 | + ${'validateEventsProperty'} |
| 48 | + ${'addEnvironmentVariablesToProcessEnv'} |
| 49 | + `('should declare $method method', ({ method }) => { |
| 50 | + expect(googleInvokeLocal[method]).toBeDefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('hooks', () => { |
| 54 | + let validateStub; |
| 55 | + let setDefaultsStub; |
| 56 | + let getDataAndContextStub; |
| 57 | + let invokeLocalStub; |
| 58 | + |
| 59 | + beforeAll(() => { |
| 60 | + validateStub = sinon.stub(googleInvokeLocal, 'validate').resolves(); |
| 61 | + setDefaultsStub = sinon.stub(googleInvokeLocal, 'setDefaults').resolves(); |
| 62 | + getDataAndContextStub = sinon.stub(googleInvokeLocal, 'getDataAndContext').resolves(); |
| 63 | + invokeLocalStub = sinon.stub(googleInvokeLocal, 'invokeLocal').resolves(); |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + googleInvokeLocal.validate.resetHistory(); |
| 68 | + googleInvokeLocal.setDefaults.resetHistory(); |
| 69 | + googleInvokeLocal.getDataAndContext.resetHistory(); |
| 70 | + googleInvokeLocal.invokeLocal.resetHistory(); |
| 71 | + }); |
| 72 | + |
| 73 | + afterAll(() => { |
| 74 | + googleInvokeLocal.validate.restore(); |
| 75 | + googleInvokeLocal.setDefaults.restore(); |
| 76 | + googleInvokeLocal.getDataAndContext.restore(); |
| 77 | + googleInvokeLocal.invokeLocal.restore(); |
| 78 | + }); |
| 79 | + |
| 80 | + it.each` |
| 81 | + hook |
| 82 | + ${'initialize'} |
| 83 | + ${'before:invoke:local:invoke'} |
| 84 | + ${'invoke:local:invoke'} |
| 85 | + `('should declare $hook hook', ({ hook }) => { |
| 86 | + expect(googleInvokeLocal.hooks[hook]).toBeDefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('initialize hook', () => { |
| 90 | + it('should override raw options with processed options', () => { |
| 91 | + googleInvokeLocal.hooks.initialize(); |
| 92 | + expect(googleInvokeLocal.options).toEqual(processedOptions); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('before:invoke:local:invoke hook', () => { |
| 97 | + it('should validate the configuration', async () => { |
| 98 | + await googleInvokeLocal.hooks['before:invoke:local:invoke'](); |
| 99 | + expect(validateStub.calledOnce).toEqual(true); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should set the defaults values', async () => { |
| 103 | + await googleInvokeLocal.hooks['before:invoke:local:invoke'](); |
| 104 | + expect(setDefaultsStub.calledOnce).toEqual(true); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should resolve the data and the context of the invocation', async () => { |
| 108 | + await googleInvokeLocal.hooks['before:invoke:local:invoke'](); |
| 109 | + expect(getDataAndContextStub.calledOnce).toEqual(true); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('invoke:local:invoke hook', () => { |
| 114 | + it('should invoke the function locally', () => { |
| 115 | + googleInvokeLocal.hooks['invoke:local:invoke'](); |
| 116 | + expect(invokeLocalStub.calledOnce).toEqual(true); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('#invokeLocal()', () => { |
| 123 | + const functionObj = Symbol('functionObj'); |
| 124 | + const data = Symbol('data'); |
| 125 | + const context = Symbol('context'); |
| 126 | + const runtime = 'nodejs14'; |
| 127 | + let getFunctionStub; |
| 128 | + let validateEventsPropertyStub; |
| 129 | + let getRuntimeStub; |
| 130 | + let invokeLocalNodeJsStub; |
| 131 | + |
| 132 | + beforeAll(() => { |
| 133 | + googleInvokeLocal.options = { |
| 134 | + ...processedOptions, // invokeLocal is called after the initialize hook which override the options |
| 135 | + data, // data and context are populated by getDataAndContext in before:invoke:local:invoke hook |
| 136 | + context, |
| 137 | + }; |
| 138 | + getFunctionStub = sinon.stub(serverless.service, 'getFunction').returns(functionObj); |
| 139 | + validateEventsPropertyStub = sinon |
| 140 | + .stub(googleInvokeLocal, 'validateEventsProperty') |
| 141 | + .returns(); |
| 142 | + getRuntimeStub = sinon.stub(googleInvokeLocal.provider, 'getRuntime').returns(runtime); |
| 143 | + |
| 144 | + invokeLocalNodeJsStub = sinon.stub(googleInvokeLocal, 'invokeLocalNodeJs').resolves(); |
| 145 | + }); |
| 146 | + |
| 147 | + afterEach(() => { |
| 148 | + serverless.service.getFunction.resetHistory(); |
| 149 | + googleInvokeLocal.validateEventsProperty.resetHistory(); |
| 150 | + googleInvokeLocal.provider.getRuntime.resetHistory(); |
| 151 | + googleInvokeLocal.invokeLocalNodeJs.resetHistory(); |
| 152 | + }); |
| 153 | + |
| 154 | + afterAll(() => { |
| 155 | + serverless.service.getFunction.restore(); |
| 156 | + googleInvokeLocal.validateEventsProperty.restore(); |
| 157 | + googleInvokeLocal.provider.getRuntime.restore(); |
| 158 | + googleInvokeLocal.invokeLocalNodeJs.restore(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should get the function configuration', async () => { |
| 162 | + await googleInvokeLocal.invokeLocal(); |
| 163 | + expect(getFunctionStub.calledOnceWith(functionName)).toEqual(true); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should validate the function configuration', async () => { |
| 167 | + await googleInvokeLocal.invokeLocal(); |
| 168 | + expect( |
| 169 | + validateEventsPropertyStub.calledOnceWith(functionObj, functionName, ['event']) |
| 170 | + ).toEqual(true); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should get the runtime', async () => { |
| 174 | + await googleInvokeLocal.invokeLocal(); |
| 175 | + expect(getRuntimeStub.calledOnceWith(functionObj)).toEqual(true); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should invoke locally the function with node js', async () => { |
| 179 | + await googleInvokeLocal.invokeLocal(); |
| 180 | + expect(invokeLocalNodeJsStub.calledOnceWith(functionObj, data, context)).toEqual(true); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should throw if the runtime is not node js', async () => { |
| 184 | + getRuntimeStub.returns('python3'); |
| 185 | + await expect(googleInvokeLocal.invokeLocal()).rejects.toThrow( |
| 186 | + 'Local invocation with runtime python3 is not supported' |
| 187 | + ); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments