Skip to content

Commit

Permalink
initial commit for enzyme-adapter-react-helper
Browse files Browse the repository at this point in the history
this new package aims to make it easier to test multiple versions
of react and its related dependencies
  • Loading branch information
brucewpaul authored and ljharb committed Oct 25, 2017
1 parent 7f1099d commit 56a19aa
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-helper/.babelrc
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-helper/.eslintignore
7 changes: 7 additions & 0 deletions packages/enzyme-adapter-react-helper/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "airbnb-base",
"root": true,
"rules": {
"max-len": 0,
},
}
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-helper/.npmignore
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-helper/.npmrc
60 changes: 60 additions & 0 deletions packages/enzyme-adapter-react-helper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "enzyme-adapter-react-helper",
"version": "0.0.0",
"description": "JavaScript Testing utilities for React",
"homepage": "http://airbnb.io/enzyme/",
"main": "build",
"scripts": {
"prebuild": "npm run clean",
"prepublish": "npm run build",
"clean": "rimraf build",
"lint": "eslint --ext js,jsx .",
"pretest": "npm run lint",
"build": "babel src --out-dir build",
"watch": "npm run build -- -w"
},
"bin": {
"enzyme-adapter-react-install": "./build/enzyme-adapter-react-install.js"
},
"repository": {
"type": "git",
"url": "https://github.com/airbnb/enzyme.git"
},
"keywords": [
"javascript",
"shallow rendering",
"shallowRender",
"test",
"reactjs",
"react",
"flux",
"testing",
"test utils",
"assertion helpers",
"tdd",
"mocha"
],
"author": "Bruce Paul <brucewpaul@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-plugin-transform-replace-object-assign": "^0.2.1",
"babel-preset-airbnb": "^2.4.0",
"enzyme": "^3.1.0",
"eslint": "^4.9.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
"react": ">=0.13",
"rimraf": "^2.6.1"
},
"peerDependencies": {
"enzyme": "^3.0.0",
"react": ">=0.13"
},
"dependencies": {
"install-peerdeps": "^1.4.1",
"npm-run": "^4.1.2",
"object.assign": "^4.0.4",
"semver": "^5.4.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#! /usr/bin/env node
/* eslint no-console: 0 */

import semver from 'semver';
import npmRun from 'npm-run';

const reactEnv = process.env.REACT;
const reactArg = process.argv[2];

if (reactEnv && !semver.valid(reactEnv)) {
throw new Error('REACT environment variable is not a valid semver');
}

if (reactArg && !semver.valid(reactArg)) {
throw new Error('Argument supplied to enzyme-adapter-react-install is not a valid semver');
}

const reactVersion = reactEnv || reactArg;

if (!semver.satisfies(reactVersion, '>=0.13')) {
throw new Error('semver is not valid. Please provide a valid semver as an argument or environment variable.');
}

console.log('Cleaning up React and related packages...');
const commands = [
'npm uninstall --no-save react-dom react-test-renderer react-addons-test-utils enzyme-adapter-react-14 enzyme-adapter-react-15.4 enzyme-adapter-react-15 enzyme-adapter-react-16',
'rimraf node_modules/react-test-renderer node_modules/react',
'npm prune',
];

try {
commands.forEach((cmd) => {
npmRun.execSync(cmd, { stdio: 'inherit' });
});
} catch (e) {
console.error('An uninstallation failed');
console.log(e);
process.exit(1);
}

console.log(`Installing React@${reactVersion} and related packages...`);
if (semver.satisfies(reactVersion, '^16.0.0-0')) {
try {
npmRun.execSync('install-peerdeps -S enzyme-adapter-react-16', { stdio: 'inherit' });
} catch (e) {
console.error('An installation failed');
console.log(e);
process.exit(16);
}
} else if (semver.satisfies(reactVersion, '^15.5.0')) {
try {
npmRun.execSync('install-peerdeps -S enzyme-adapter-react-15', { stdio: 'inherit' });
} catch (e) {
console.error('An installation failed');
console.log(e);
process.exit(155);
}
} else if (semver.satisfies(reactVersion, '15.0.0-0 - 15.4.x')) {
try {
npmRun.execSync('install-peerdeps -S enzyme-adapter-react-15.4', { stdio: 'inherit' });
} catch (e) {
console.error('An installation failed');
console.log(e);
process.exit(15);
}
} else if (semver.satisfies(reactVersion, '^0.14.0')) {
try {
npmRun.execSync('install-peerdeps -S enzyme-adapter-react-14', { stdio: 'inherit' });
} catch (e) {
console.error('An installation failed');
console.log(e);
process.exit(14);
}
} else if (semver.satisfies(reactVersion, '^0.13.0')) {
try {
npmRun.execSync('install-peerdeps -S enzyme-adapter-react-13', { stdio: 'inherit' });
} catch (e) {
console.error('An installation failed');
console.log(e);
process.exit(13);
}
}
37 changes: 37 additions & 0 deletions packages/enzyme-adapter-react-helper/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { configure } from 'enzyme';

export default function setupEnzymeAdapter(enzymeOptions = {}, adapterOptions = {}) {
let Adapter;

try {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, import/no-unresolved
Adapter = require('enzyme-adapter-react-16');
} catch (r) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, import/no-unresolved
Adapter = require('enzyme-adapter-react-15');
} catch (e) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, import/no-unresolved
Adapter = require('enzyme-adapter-react-15.4');
} catch (a) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, import/no-unresolved
Adapter = require('enzyme-adapter-react-14');
} catch (c) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, import/no-unresolved
Adapter = require('enzyme-adapter-react-13');
} catch (t) {
throw new Error('It seems as though you don’t have any `enzyme-adapter-react-*` installed. Please install the relevant version and try again.');
}
}
}
}
}

configure({
adapter: new Adapter(adapterOptions),
...enzymeOptions,
});
}

0 comments on commit 56a19aa

Please sign in to comment.