Skip to content

Commit d3ff91a

Browse files
authoredMar 11, 2021
test(plugins): add missing tests for plugin resolution logic (#3661)
1 parent a2bca0d commit d3ff91a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
 

‎test/unit/plugin.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const path = require('path')
4+
const proxyquire = require('proxyquire')
35
const createInstantiatePlugin = require('../../lib/plugin').createInstantiatePlugin
46

57
describe('plugin', () => {
@@ -58,4 +60,51 @@ describe('plugin', () => {
5860
expect(fakes.emitter.emit).to.have.been.calledWith('load_error', 'unknown', 'name')
5961
})
6062
})
63+
64+
describe('resolve', () => {
65+
// Base path should be the same as the one produced by the code under test.
66+
const base = path.resolve(__dirname, '..', '..', '..')
67+
const directories = {
68+
[base]: ['karma-fancy-plugin', 'other-package', 'karma-powerful-plugin', 'yet-another-package', '@scope'],
69+
[path.join(base, '@scope')]: ['karma-super-plugin', 'not-a-plugin']
70+
}
71+
72+
const { resolve } = proxyquire(
73+
'../../lib/plugin',
74+
{
75+
'graceful-fs': { readdirSync: (dir) => directories[dir] },
76+
'karma-fancy-plugin': { name: 'karma-fancy-plugin', '@noCallThru': true },
77+
'karma-powerful-plugin': { name: 'karma-powerful-plugin', '@noCallThru': true },
78+
'@scope/karma-super-plugin': { name: '@scope/karma-super-plugin', '@noCallThru': true },
79+
// Plugins are require()'d using an absolute path if they were resolved from the glob.
80+
[path.join(base, 'karma-fancy-plugin')]: { name: 'karma-fancy-plugin', '@noCallThru': true },
81+
[path.join(base, 'karma-powerful-plugin')]: { name: 'karma-powerful-plugin', '@noCallThru': true },
82+
[path.join(base, '@scope/karma-super-plugin')]: { name: '@scope/karma-super-plugin', '@noCallThru': true }
83+
}
84+
)
85+
86+
it('loads simple plugin', () => {
87+
const modules = resolve(['karma-powerful-plugin'], null)
88+
89+
expect(modules.map((m) => m.name)).to.deep.equal(['karma-powerful-plugin'])
90+
})
91+
92+
it('loads scoped plugin', () => {
93+
const modules = resolve(['@scope/karma-super-plugin'], null)
94+
95+
expect(modules.map((m) => m.name)).to.deep.equal(['@scope/karma-super-plugin'])
96+
})
97+
98+
it('loads simple plugins with globs', () => {
99+
const modules = resolve(['karma-*'], null)
100+
101+
expect(modules.map((m) => m.name)).to.deep.equal(['karma-fancy-plugin', 'karma-powerful-plugin'])
102+
})
103+
104+
it('loads scoped plugins with globs', () => {
105+
const modules = resolve(['@*/karma-*'], null)
106+
107+
expect(modules.map((m) => m.name)).to.deep.equal(['@scope/karma-super-plugin'])
108+
})
109+
})
61110
})

0 commit comments

Comments
 (0)
Please sign in to comment.