diff --git a/lib/classes/PluginManager.test.js b/lib/classes/PluginManager.test.js index 595c2caf6..b1a9a14c5 100644 --- a/lib/classes/PluginManager.test.js +++ b/lib/classes/PluginManager.test.js @@ -1586,6 +1586,107 @@ describe('PluginManager', () => { }); }); + describe('#getCommand()', () => { + beforeEach(() => { + pluginManager.addPlugin(SynchronousPluginMock); + pluginManager.serverless.cli.loadedCommands = { + create: { + usage: 'Create new Serverless service', + lifecycleEvents: [ + 'create', + ], + options: { + template: { + usage: 'Template for the service. Available templates: ", "aws-nodejs", "..."', + shortcut: 't', + }, + }, + key: 'create', + pluginName: 'Create', + }, + deploy: { + usage: 'Deploy a Serverless service', + configDependent: true, + lifecycleEvents: [ + 'cleanup', + 'initialize', + ], + options: { + conceal: { + usage: 'Hide secrets from the output (e.g. API Gateway key values)', + }, + stage: { + usage: 'Stage of the service', + shortcut: 's', + }, + }, + key: 'deploy', + pluginName: 'Deploy', + commands: { + function: { + usage: 'Deploy a single function from the service', + lifecycleEvents: [ + 'initialize', + 'packageFunction', + 'deploy', + ], + options: { + function: { + usage: 'Name of the function', + shortcut: 'f', + required: true, + }, + }, + key: 'deploy:function', + pluginName: 'Deploy', + }, + list: { + usage: 'List deployed version of your Serverless Service', + lifecycleEvents: [ + 'log', + ], + key: 'deploy:list', + pluginName: 'Deploy', + commands: { + functions: { + usage: 'List all the deployed functions and their versions', + lifecycleEvents: [ + 'log', + ], + key: 'deploy:list:functions', + pluginName: 'Deploy', + }, + }, + }, + }, + }, + }; + }); + it('should give a suggestion for an unknown command', (done) => { + try { + pluginManager.getCommand(['creet']); + done('Test failed. Expected an error to be thrown'); + } catch (error) { + expect(error.name).to.eql('ServerlessError'); + expect(error.message).to.eql('Serverless command "creet" not found. ' + + 'Did you mean "create"? Run "serverless help" for a list of all available commands.'); + done(); + } + }); + + it('should not give a suggestion for valid top level command', (done) => { + try { + pluginManager.getCommand(['deploy', 'function-misspelled']); + done('Test failed. Expected an error to be thrown'); + } catch (error) { + expect(error.name).to.eql('ServerlessError'); + expect(error.message).to.eql('"function-misspelled" is not a valid sub command. ' + + 'Run "serverless deploy" to see a more helpful error message for this command.'); + done(); + } + }); + }); + describe('#spawn()', () => { it('should throw an error when the given command is not available', () => { pluginManager.addPlugin(EntrypointPluginMock);