Skip to content

Commit ca2747c

Browse files
committed
Validate that given register is a function
1 parent 317db28 commit ca2747c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/__tests__/plugin-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,21 @@ describe('Plugins', function() {
7676
instances[1].should.not.have.property('dirty')
7777
}, done)
7878
})
79+
80+
it ('throws an error if a register property is not a function', function(done) {
81+
let app = new Microcosm()
82+
83+
let Plugin = {
84+
register: 'booyah'
85+
}
86+
87+
app.addPlugin(Plugin)
88+
89+
try {
90+
app.start()
91+
} catch(error) {
92+
expect(error).to.be.instanceOf(TypeError)
93+
done()
94+
}
95+
})
7996
})

src/plugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77
* of a Microcosm.
88
*/
99

10-
function PluginFactory(config, options, app) {
10+
function PluginFactory (config, options, app) {
1111
return Object.assign({ app, options }, config)
1212
}
1313

14+
function checkPlugin (plugin) {
15+
if (process.env.NODE_ENV === 'development' && 'register' in plugin && typeof plugin.register !== 'function') {
16+
throw TypeError('Expected register property of plugin to be a function, instead got ' + plugin.register)
17+
}
18+
}
19+
1420
function installPlugin (next, plugin) {
21+
checkPlugin(plugin)
22+
1523
return function (error) {
1624
// Halt execution of all future plugin installation if there is an error
1725
if (error) {

0 commit comments

Comments
 (0)