Skip to content

Commit

Permalink
Duck type enzyme wrappers
Browse files Browse the repository at this point in the history
There's no guarantee that my version of enzyme is the version the user
is _actually using_. Really. For example, `yarn install --production`
was broken for quite a while. Since enzyme is a dev dependency, there's
a chance it could be installed on the user's machine accidentally.
  • Loading branch information
PsychoLlama committed Oct 21, 2017
1 parent 690e410 commit 5f3f0ee
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/assertions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ShallowWrapper, ReactWrapper } from 'enzyme';
import elementToString from './element-to-string';
import getDisplayName from 'react-display-name';
import stringifyObject from 'stringify-object';
Expand Down Expand Up @@ -74,8 +73,18 @@ const negate = methodName =>
* @param {Any} actual - The value to check.
* @return {Boolean} - Whether it's enzyme.
*/
const isEnzymeWrapper = actual =>
actual instanceof ShallowWrapper || actual instanceof ReactWrapper;
const isEnzymeWrapper = actual => {
if (!actual) {
return false;
}

// Duck typed (instanceof can break if duplicate package instances exist).
return Boolean(
typeof actual.setState === 'function' &&
typeof actual.findWhere === 'function' &&
typeof actual.debug === 'function',
);
};

/**
* Throws an error if the given argument isn't an enzyme wrapper.
Expand Down

0 comments on commit 5f3f0ee

Please sign in to comment.