diff --git a/package.json b/package.json index 58dc4ee25..5c30361ae 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "check": "npm run lint && npm run test:all", "build": "babel src --out-dir build", "test:watch": "mocha --compilers js:babel/register --recursive src/**/__tests__/*.js --watch", - "test:all": "npm run react:13 && npm test && npm run react:14 && npm test", + "test:describeWithDOMOnly": "mocha --compilers js:babel/register --recursive src/**/__tests__/describeWithDOM/describeWithDOMOnly-spec.js", + "test:describeWithDOMSkip": "mocha --compilers js:babel/register --recursive src/**/__tests__/describeWithDOM/describeWithDOMSkip-spec.js", + "test:all": "npm run react:13 && npm test && npm run test:describeWithDOMOnly && npm run test:describeWithDOMSkip && npm run react:14 && npm test && npm run test:describeWithDOMOnly && npm run test:describeWithDOMSkip", "react:clean": "rimraf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils", "react:13": "npm run react:clean && npm i react@0.13", "react:14": "npm run react:clean && npm i react@0.14 react-dom@0.14 react-addons-test-utils@0.14", diff --git a/src/__tests__/describeWithDOM/describeWithDOMOnly-spec.js b/src/__tests__/describeWithDOM/describeWithDOMOnly-spec.js new file mode 100644 index 000000000..10a375bf2 --- /dev/null +++ b/src/__tests__/describeWithDOM/describeWithDOMOnly-spec.js @@ -0,0 +1,19 @@ +import { expect } from 'chai'; +import { describeWithDOM } from '../..'; + +describe('describeWithDOM', () => { + describe('.only()', () => { + describeWithDOM.only('will skip all tests not called with only', () => { + it('will run only this test', () => { + expect(true).to.equal(true); + }); + }); + + describeWithDOM('will not call other tests', () => { + it('will not run this test', () => { + // purposefully failing test that won't be called + expect(true).to.equal(false); + }); + }); + }); +}); diff --git a/src/__tests__/describeWithDOM/describeWithDOMSkip-spec.js b/src/__tests__/describeWithDOM/describeWithDOMSkip-spec.js new file mode 100644 index 000000000..6c4738fdc --- /dev/null +++ b/src/__tests__/describeWithDOM/describeWithDOMSkip-spec.js @@ -0,0 +1,19 @@ +import { expect } from 'chai'; +import { describeWithDOM } from '../..'; + +describe('describeWithDOM', () => { + describe('.skip()', () => { + describeWithDOM.skip('will skip tests called with skip', () => { + it('will not run this test', () => { + // purposefully failing test that won't be run + expect(true).to.equal(false); + }); + }); + + describeWithDOM('will still call describeWithDOM tests without .skip', () => { + it('will run this test', () => { + expect(true).to.equal(true); + }); + }); + }); +}); diff --git a/src/describeWithDOM.js b/src/describeWithDOM.js new file mode 100644 index 000000000..217032ad7 --- /dev/null +++ b/src/describeWithDOM.js @@ -0,0 +1,47 @@ +let jsdom; + +try { + require('jsdom'); // could throw + jsdom = require('mocha-jsdom'); +} catch (e) { + // jsdom is not supported... +} + +export function describeWithDOM(a, b) { + describe('(uses jsdom)', () => { + if (typeof jsdom === 'function') { + jsdom(); + describe(a, b); + } else { + // if jsdom isn't available, skip every test in this describe context + describe.skip(a, b); + } + }); +} + +function only(a, b) { + describe('(uses jsdom)', () => { + if (typeof jsdom === 'function') { + jsdom(); + describe.only(a, b); + } else { + // if jsdom isn't available, skip every test in this describe context + describe.skip(a, b); + } + }); +} + +function skip(a, b) { + describe('(uses jsdom)', () => { + if (typeof jsdom === 'function') { + jsdom(); + describe.skip(a, b); + } else { + // if jsdom isn't available, skip every test in this describe context + describe.skip(a, b); + } + }); +} + +describeWithDOM.only = only; +describeWithDOM.skip = skip; diff --git a/src/index.js b/src/index.js index 934725bac..8e9e261d5 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import cheerio from 'cheerio'; import Sinon from 'sinon'; import ReactWrapper from './ReactWrapper'; import ShallowWrapper from './ShallowWrapper'; +import { describeWithDOM } from './describeWithDOM'; import { onPrototype } from './Utils'; import { renderToStaticMarkup } from './react-compat'; @@ -9,28 +10,8 @@ import { renderToStaticMarkup } from './react-compat'; * @class Enzyme */ -let jsdom; -try { - require('jsdom'); // could throw - jsdom = require('mocha-jsdom'); -} catch (e) { - // jsdom is not supported... -} - export let sinon = Sinon.sandbox.create(); -export function describeWithDOM(a, b) { - describe('(uses jsdom)', () => { - if (typeof jsdom === 'function') { - jsdom(); - describe(a, b); - } else { - // if jsdom isn't available, skip every test in this describe context - describe.skip(a, b); - } - }); -} - export function useSetStateHack() { let cleanup = false; before(() => { @@ -106,3 +87,4 @@ export function render(node) { export { ShallowWrapper as ShallowWrapper }; export { ReactWrapper as ReactWrapper }; +export { describeWithDOM as describeWithDOM };