From d3ff91a5a2f7ff84141096bb47aa31ff73f81cca Mon Sep 17 00:00:00 2001 From: Yaroslav Admin Date: Thu, 11 Mar 2021 01:06:11 +0100 Subject: [PATCH] test(plugins): add missing tests for plugin resolution logic (#3661) --- test/unit/plugin.spec.js | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/unit/plugin.spec.js b/test/unit/plugin.spec.js index 3ec9824d1..eac0c6ecf 100644 --- a/test/unit/plugin.spec.js +++ b/test/unit/plugin.spec.js @@ -1,5 +1,7 @@ 'use strict' +const path = require('path') +const proxyquire = require('proxyquire') const createInstantiatePlugin = require('../../lib/plugin').createInstantiatePlugin describe('plugin', () => { @@ -58,4 +60,51 @@ describe('plugin', () => { expect(fakes.emitter.emit).to.have.been.calledWith('load_error', 'unknown', 'name') }) }) + + describe('resolve', () => { + // Base path should be the same as the one produced by the code under test. + const base = path.resolve(__dirname, '..', '..', '..') + const directories = { + [base]: ['karma-fancy-plugin', 'other-package', 'karma-powerful-plugin', 'yet-another-package', '@scope'], + [path.join(base, '@scope')]: ['karma-super-plugin', 'not-a-plugin'] + } + + const { resolve } = proxyquire( + '../../lib/plugin', + { + 'graceful-fs': { readdirSync: (dir) => directories[dir] }, + 'karma-fancy-plugin': { name: 'karma-fancy-plugin', '@noCallThru': true }, + 'karma-powerful-plugin': { name: 'karma-powerful-plugin', '@noCallThru': true }, + '@scope/karma-super-plugin': { name: '@scope/karma-super-plugin', '@noCallThru': true }, + // Plugins are require()'d using an absolute path if they were resolved from the glob. + [path.join(base, 'karma-fancy-plugin')]: { name: 'karma-fancy-plugin', '@noCallThru': true }, + [path.join(base, 'karma-powerful-plugin')]: { name: 'karma-powerful-plugin', '@noCallThru': true }, + [path.join(base, '@scope/karma-super-plugin')]: { name: '@scope/karma-super-plugin', '@noCallThru': true } + } + ) + + it('loads simple plugin', () => { + const modules = resolve(['karma-powerful-plugin'], null) + + expect(modules.map((m) => m.name)).to.deep.equal(['karma-powerful-plugin']) + }) + + it('loads scoped plugin', () => { + const modules = resolve(['@scope/karma-super-plugin'], null) + + expect(modules.map((m) => m.name)).to.deep.equal(['@scope/karma-super-plugin']) + }) + + it('loads simple plugins with globs', () => { + const modules = resolve(['karma-*'], null) + + expect(modules.map((m) => m.name)).to.deep.equal(['karma-fancy-plugin', 'karma-powerful-plugin']) + }) + + it('loads scoped plugins with globs', () => { + const modules = resolve(['@*/karma-*'], null) + + expect(modules.map((m) => m.name)).to.deep.equal(['@scope/karma-super-plugin']) + }) + }) })