Skip to content

Commit

Permalink
[Fix] improve "bad adapter" error message
Browse files Browse the repository at this point in the history
Fixes #1437
  • Loading branch information
ljharb committed Jan 13, 2018
1 parent 1c86262 commit f4f87c6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
27 changes: 25 additions & 2 deletions packages/enzyme-test-suite/test/Adapter-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import React from 'react';
import { expect } from 'chai';
import jsdom from 'jsdom';
import configuration from 'enzyme/build/configuration';
import { configure, shallow } from 'enzyme';

import './_helpers/setupAdapters';
import Adapter from './_helpers/adapter';
import { renderToString } from './_helpers/react-compat';
import { REACT013, REACT16 } from './_helpers/version';
import { itIf, describeWithDOM } from './_helpers';
Expand Down Expand Up @@ -36,8 +38,30 @@ function cleanNode(node) {
}

describe('Adapter', () => {
describeWithDOM('mounted render', () => {
describe('error message', () => {
afterEach(() => {
configure({ adapter });
});

it('fails to render when no adapter is configured', () => {
configure({ adapter: undefined });
expect(() => shallow(<div />)).to.throw(Error, /Enzyme expects an adapter to be configured, but found none/);
});

it('fails to render when an object that does not inherit from the base class is configured', () => {
expect(() => configure({ adapter: {} })).to.throw(Error, /configured enzyme adapter did not inherit from the EnzymeAdapter base class/);
});

it('fails to render when an adapter constructor is configured', () => {
expect(() => configure({ adapter: Adapter })).to.throw(Error, /you provided an adapter \*constructor\*/);
});

it('fails to render when a non-adapter-constructor function is configured', () => {
expect(() => configure({ adapter() {} })).to.throw(Error, /an enzyme adapter must be an object instance; you provided a function/);
});
});

describeWithDOM('mounted render', () => {
function hydratedTreeMatchesUnhydrated(element) {
const markup = renderToString(element);
const dom = jsdom.jsdom(`<div id="root">${markup}</div>`);
Expand Down Expand Up @@ -763,5 +787,4 @@ describe('Adapter', () => {
},
}));
});

});
29 changes: 29 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint global-require: 0, import/no-extraneous-dependencies: 0, import/no-unresolved: 0 */
/**
* This file is needed only because we run our unit tests on multiple
* versions of React at a time. This file basically figures out which
* version of React is loaded, and exports the correct adapter for configuring.
*/
const {
REACT013,
REACT014,
REACT15,
REACT155,
REACT16,
} = require('./version');

let Adapter = null;

if (REACT013) {
Adapter = require('enzyme-adapter-react-13');
} else if (REACT014) {
Adapter = require('enzyme-adapter-react-14');
} else if (REACT155) {
Adapter = require('enzyme-adapter-react-15');
} else if (REACT15) {
Adapter = require('enzyme-adapter-react-15.4');
} else if (REACT16) {
Adapter = require('enzyme-adapter-react-16');
}

module.exports = Adapter;
29 changes: 1 addition & 28 deletions packages/enzyme-test-suite/test/_helpers/setupAdapters.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
/* eslint global-require: 0, import/no-extraneous-dependencies: 0, import/no-unresolved: 0 */
/**
* This file is needed only because we run our unit tests on multiple
* versions of React at a time. This file basically figures out which
* version of React is loaded, and configures enzyme to use the right
* corresponding adapter.
*/
const {
REACT013,
REACT014,
REACT15,
REACT155,
REACT16,
} = require('./version');
const Enzyme = require('enzyme');

let Adapter = null;

if (REACT013) {
Adapter = require('enzyme-adapter-react-13');
} else if (REACT014) {
Adapter = require('enzyme-adapter-react-14');
} else if (REACT155) {
Adapter = require('enzyme-adapter-react-15');
} else if (REACT15) {
Adapter = require('enzyme-adapter-react-15.4');
} else if (REACT16) {
Adapter = require('enzyme-adapter-react-16');
}
const Adapter = require('./adapter');

Enzyme.configure({ adapter: new Adapter() });
30 changes: 28 additions & 2 deletions packages/enzyme/src/validateAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@ import EnzymeAdapter from './EnzymeAdapter';
export default function validateAdapter(adapter) {
if (!adapter) {
throw new Error(`
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. To
configure an adapter, you should call \`Enzyme.configure({ adapter: new Adapter() })\`
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none.
To configure an adapter, you should call \`Enzyme.configure({ adapter: new Adapter() })\`
before using any of Enzyme's top level APIs, where \`Adapter\` is the adapter
corresponding to the library currently being tested. For example:
import Adapter from 'enzyme-adapter-react-15';
To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html
`);
}
if (typeof adapter === 'function') {
if (Object.getPrototypeOf(adapter) === EnzymeAdapter) {
throw new Error(`
Enzyme Internal Error: Enzyme expects an adapter instance to be configured -
you provided an adapter *constructor*.
To configure an adapter, you should call \`Enzyme.configure({ adapter: new Adapter() })\`
before using any of Enzyme's top level APIs, where \`Adapter\` is the adapter
corresponding to the library currently being tested. For example:
import Adapter from 'enzyme-adapter-react-15';
To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html
`);
}
throw new Error(`
Enzyme Internal Error: Enzyme expects an adapter to be configured -
an enzyme adapter must be an object instance; you provided a function.
To configure an adapter, you should call \`Enzyme.configure({ adapter: new Adapter() })\`
before using any of Enzyme's top level APIs, where \`Adapter\` is the adapter
corresponding to the library currently being tested. For example:
Expand Down

0 comments on commit f4f87c6

Please sign in to comment.