Skip to content

Commit

Permalink
Allow plugins to have a version property.
Browse files Browse the repository at this point in the history
Don't throw if a plugin is installed more than once as long as the version number is the same (despite the plugins not being === each other).
  • Loading branch information
papandreou committed Aug 5, 2015
1 parent a40822f commit bff84bc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions documentation/api/use.md
Expand Up @@ -12,6 +12,7 @@ Unexpected plugins are functions or objects that adhere to the following interfa
Optional properties:

* __name__: `String` - the name of the plugin.
* __version__: `String` - the semver version of the plugin (string).
* __dependencies__: `String array` - a list of dependencies.

Required:
Expand Down
13 changes: 10 additions & 3 deletions lib/Unexpected.js
Expand Up @@ -504,12 +504,18 @@ Unexpected.prototype.use = function (plugin) {
});

if (existingPlugin) {
if (existingPlugin === plugin) {
if (existingPlugin === plugin || (typeof plugin.version !== 'undefined' && plugin.version === existingPlugin.version)) {
// No-op
return this.expect;
} else {
throw new Error("Another instance of the plugin '" + plugin.name + "' is already installed. " +
"Please check your node_modules folder for unmet peerDependencies.");
throw new Error("Another instance of the plugin '" + plugin.name + "' " +
"is already installed" +
(typeof existingPlugin.version !== 'undefined' ?
' (version ' + existingPlugin.version +
(typeof plugin.version !== 'undefined' ?
', trying to install ' + plugin.version : '') +
')' : '') +
". Please check your node_modules folder for unmet peerDependencies.");
}
}

Expand All @@ -519,6 +525,7 @@ Unexpected.prototype.use = function (plugin) {
throw new Error('Plugins must be functions or adhere to the following interface\n' +
'{\n' +
' name: <an optional plugin name>,\n' +
' version: <an optional semver version string>,\n' +
' dependencies: <an optional list of dependencies>,\n' +
' installInto: <a function that will update the given expect instance>\n' +
'}');
Expand Down
54 changes: 53 additions & 1 deletion test/unexpected.spec.js
Expand Up @@ -5276,6 +5276,7 @@ describe('unexpected', function () {
}, 'to throw', 'Plugins must be functions or adhere to the following interface\n' +
'{\n' +
' name: <an optional plugin name>,\n' +
' version: <an optional semver version string>,\n' +
' dependencies: <an optional list of dependencies>,\n' +
' installInto: <a function that will update the given expect instance>\n' +
'}');
Expand Down Expand Up @@ -5389,7 +5390,58 @@ describe('unexpected', function () {
expect(callCount, 'to be', 1);
});

it('installing a plugin with the same name as another plugin (but not ===) throws an error', function () {
it('installing two different plugins that are identically named and have the same version (but not ===) will only install the first one', function () {
var callCount1 = 0;
var plugin1 = {
name: 'plugin',
version: '1.2.3',
installInto: function () {
callCount1 += 1;
}
};
var callCount2 = 0;
var plugin2 = {
name: 'plugin',
version: '1.2.3',
installInto: function () {
callCount2 += 1;
}
};
expect.use(plugin1).use(plugin2);
expect(callCount1, 'to be', 1);
expect(callCount2, 'to be', 0);
});

it('should throw an error when installing two different plugins that are identically named and have different versions', function () {
expect.use({
name: 'plugin',
version: '1.2.3',
installInto: function () {}
});
expect(function () {
expect.use({
name: 'plugin',
version: '1.5.6',
installInto: function () {}
});
}, 'to throw', "Another instance of the plugin 'plugin' is already installed (version 1.2.3, trying to install 1.5.6). Please check your node_modules folder for unmet peerDependencies.");
});

it('should throw an error when two identically named plugins where the first one has a version number', function () {
expect.use({
name: 'plugin',
version: '1.2.3',
installInto: function () {}
});
expect(function () {
expect.use({
name: 'plugin',
installInto: function () {}
});
}, 'to throw', "Another instance of the plugin 'plugin' is already installed (version 1.2.3). Please check your node_modules folder for unmet peerDependencies.");
});

it('installing a version-less plugin with the same name as another plugin (but not ===) throws an error', function () {
expect.use({
name: 'test',
installInto: function () {}
Expand Down

0 comments on commit bff84bc

Please sign in to comment.