UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

121 lines (119 loc) 3.89 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _proxyquire = _interopRequireDefault(require("proxyquire")); /* eslint-env mocha */ describe('navy/plugin-interface', function () { describe('loadPlugins', function () { function loadModule(stubs) { return _proxyquire.default.noCallThru()('../plugin-interface', stubs); } it('should throw an invariant violation if the navy has no config provider', async function () { const { loadPlugins } = loadModule({}); const navy = { name: 'env', getConfigProvider: async () => null }; let caught; try { await loadPlugins(navy, { plugins: [] }); } catch (err) { caught = err; } (0, _chai.expect)(caught).to.be.an('error'); (0, _chai.expect)(caught.message).to.match(/NO_CONFIG_PROVIDER/); }); it('should return an empty array when navyFile has no plugins entry', async function () { const { loadPlugins } = loadModule({}); const navy = { getConfigProvider: async () => ({ getNavyPath: async () => '/some/path' }) }; (0, _chai.expect)(await loadPlugins(navy, {})).to.eql([]); }); it('should resolve plugins by name relative to the navy path and instantiate them with the navy', async function () { const fakePluginPath = '/abs/path/to/plugin'; const fakePluginInstance = { meta: 'instance' }; const PluginCtor = navy => ({ ...fakePluginInstance, navy }); const { loadPlugins } = loadModule({ resolve: (pluginName, opts, cb) => cb(null, fakePluginPath), [fakePluginPath]: PluginCtor }); const navy = { getConfigProvider: async () => ({ getNavyPath: async () => '/navy/path' }) }; const plugins = await loadPlugins(navy, { plugins: ['my-plugin'] }); (0, _chai.expect)(plugins).to.have.lengthOf(1); (0, _chai.expect)(plugins[0]).to.have.property('meta', 'instance'); (0, _chai.expect)(plugins[0]).to.have.property('navy', navy); }); it('should throw PLUGIN_RESOLVE_ERR with the joined plugin names when resolution fails', async function () { const { loadPlugins } = loadModule({ resolve: (pluginName, opts, cb) => cb(new Error('not found')) }); const navy = { getConfigProvider: async () => ({ getNavyPath: async () => '/navy/path' }) }; let caught; try { await loadPlugins(navy, { plugins: ['plugin-a', 'plugin-b'] }); } catch (err) { caught = err; } (0, _chai.expect)(caught).to.be.an('error'); (0, _chai.expect)(caught.message).to.match(/PLUGIN_RESOLVE_ERR/); (0, _chai.expect)(caught.message).to.contain('plugin-a, plugin-b'); }); it('should throw PLUGIN_DOESNT_EXPORT_FUNCTION when a plugin module is not a function', async function () { const fakePluginPath = '/abs/path/to/bad-plugin'; const { loadPlugins } = loadModule({ resolve: (pluginName, opts, cb) => cb(null, fakePluginPath), [fakePluginPath]: { notAFunction: true } }); const navy = { getConfigProvider: async () => ({ getNavyPath: async () => '/navy/path' }) }; let caught; try { await loadPlugins(navy, { plugins: ['bad-plugin'] }); } catch (err) { caught = err; } (0, _chai.expect)(caught).to.be.an('error'); (0, _chai.expect)(caught.message).to.match(/PLUGIN_DOESNT_EXPORT_FUNCTION/); (0, _chai.expect)(caught.message).to.contain('bad-plugin'); }); }); });