diff --git a/test/fixtures/test-package/esm-export.js b/test/fixtures/test-package/esm-export.js new file mode 100644 index 000000000..4da61e98d --- /dev/null +++ b/test/fixtures/test-package/esm-export.js @@ -0,0 +1,7 @@ +import { PushActionPlugin } from '@finos/git-proxy/plugin'; + +// test default export (ESM syntax) +export default new PushActionPlugin(async (req, action) => { + console.log('Dummy plugin: ', action); + return action; +}); diff --git a/test/fixtures/test-package/esm-multiple-export.js b/test/fixtures/test-package/esm-multiple-export.js new file mode 100644 index 000000000..fb9c290bc --- /dev/null +++ b/test/fixtures/test-package/esm-multiple-export.js @@ -0,0 +1,18 @@ +import { PushActionPlugin, PullActionPlugin } from '@finos/git-proxy/plugin'; + +// test multiple exports (ESM syntax) +export default { + foo: new PushActionPlugin(async (req, action) => { + console.log('PushActionPlugin: ', action); + return action; + }), + bar: new PullActionPlugin(async (req, action) => { + console.log('PullActionPlugin: ', action); + return action; + }), + baz: { + exec: async (req, action) => { + console.log('not a real plugin object'); + }, + }, +}; diff --git a/test/fixtures/test-package/esm-subclass.js b/test/fixtures/test-package/esm-subclass.js new file mode 100644 index 000000000..164419d01 --- /dev/null +++ b/test/fixtures/test-package/esm-subclass.js @@ -0,0 +1,14 @@ +import { PushActionPlugin } from '@finos/git-proxy/plugin'; + +class DummyPlugin extends PushActionPlugin { + constructor(exec) { + super(); + this.exec = exec; + } +} + +// test default export (ESM syntax) +export default new DummyPlugin(async (req, action) => { + console.log('Dummy plugin: ', action); + return action; +}); diff --git a/test/fixtures/test-package/multiple-export.js b/test/fixtures/test-package/multiple-export.js index 9a6a5544a..672ffde28 100644 --- a/test/fixtures/test-package/multiple-export.js +++ b/test/fixtures/test-package/multiple-export.js @@ -9,4 +9,9 @@ module.exports = { console.log('PullActionPlugin: ', action); return action; }), + baz: { + exec: async (req, action) => { + console.log('not a real plugin object'); + }, + }, }; diff --git a/test/plugin/plugin.test.js b/test/plugin/plugin.test.js index c26929136..bb1acbdf0 100644 --- a/test/plugin/plugin.test.js +++ b/test/plugin/plugin.test.js @@ -2,12 +2,7 @@ import chai from 'chai'; import { spawnSync } from 'child_process'; import { rmSync } from 'fs'; import { join } from 'path'; -import { - isCompatiblePlugin, - PullActionPlugin, - PushActionPlugin, - PluginLoader, -} from '../../src/plugin.ts'; +import { isCompatiblePlugin, PushActionPlugin, PluginLoader } from '../../src/plugin.ts'; chai.should(); @@ -22,35 +17,73 @@ describe('loading plugins from packages', function () { spawnSync('npm', ['install'], { cwd: testPackagePath, timeout: 5000 }); }); - it('should load plugins that are the default export (module.exports = pluginObj)', async function () { - const loader = new PluginLoader([join(testPackagePath, 'default-export.js')]); - await loader.load(); - expect(loader.pushPlugins.length).to.equal(1); - expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; - expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to - .be.true; - }).timeout(10000); + describe('CommonJS syntax', () => { + it('should load plugins that are the default export (module.exports = pluginObj)', async function () { + const loader = new PluginLoader([join(testPackagePath, 'default-export.js')]); + await loader.load(); + expect(loader.pushPlugins.length).to.equal(1); + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))) + .to.be.true; + }).timeout(10000); - it('should load multiple plugins from a module that match the plugin class (module.exports = { pluginFoo, pluginBar })', async function () { - const loader = new PluginLoader([join(testPackagePath, 'multiple-export.js')]); - await loader.load(); - expect(loader.pushPlugins.length).to.equal(1); - expect(loader.pullPlugins.length).to.equal(1); - expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; - expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to - .be.true; - expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin'))).to - .be.true; - }).timeout(10000); + it('should load multiple plugins from a module that match the plugin class (module.exports = { pluginFoo, pluginBar })', async function () { + const loader = new PluginLoader([join(testPackagePath, 'multiple-export.js')]); + await loader.load(); - it('should load plugins that are subclassed from plugin classes', async function () { - const loader = new PluginLoader([join(testPackagePath, 'subclass.js')]); - await loader.load(); - expect(loader.pushPlugins.length).to.equal(1); - expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; - expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to - .be.true; - }).timeout(10000); + // Should load the foo and bar plugins, but not the baz object which isn't a plugin + expect(loader.pushPlugins.length).to.equal(1); + expect(loader.pullPlugins.length).to.equal(1); + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))) + .to.be.true; + expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin'))) + .to.be.true; + }).timeout(10000); + + it('should load plugins that are subclassed from plugin classes', async function () { + const loader = new PluginLoader([join(testPackagePath, 'subclass.js')]); + await loader.load(); + expect(loader.pushPlugins.length).to.equal(1); + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))) + .to.be.true; + }).timeout(10000); + }); + + describe('ESM syntax', () => { + it('should load plugins that are the default export (exports default pluginObj)', async function () { + const loader = new PluginLoader([join(testPackagePath, 'esm-export.js')]); + await loader.load(); + expect(loader.pushPlugins.length).to.equal(1); + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))) + .to.be.true; + }).timeout(10000); + + it('should load multiple plugins from a module that match the plugin class (exports default { pluginFoo, pluginBar })', async function () { + const loader = new PluginLoader([join(testPackagePath, 'esm-multiple-export.js')]); + await loader.load(); + + // Should load the foo and bar plugins, but not the baz object which isn't a plugin + expect(loader.pushPlugins.length).to.equal(1); + expect(loader.pullPlugins.length).to.equal(1); + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))) + .to.be.true; + expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin'))) + .to.be.true; + }).timeout(10000); + + it('should load plugins that are subclassed from plugin classes (exports default class DummyPlugin extends PushActionPlugin {})', async function () { + const loader = new PluginLoader([join(testPackagePath, 'esm-subclass.js')]); + await loader.load(); + expect(loader.pushPlugins.length).to.equal(1); + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true; + expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))) + .to.be.true; + }).timeout(10000); + }); it('should not load plugins that are not valid modules', async function () { const loader = new PluginLoader([join(__dirname, './dummy.js')]);