Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(plugins): add missing tests for plugin resolution logic #3661

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions test/unit/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const path = require('path')
const proxyquire = require('proxyquire')
const createInstantiatePlugin = require('../../lib/plugin').createInstantiatePlugin

describe('plugin', () => {
Expand Down Expand Up @@ -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'])
})
})
})