This describes how to build enzyme as a UMD (Universal Module Definition) module so it can be used in systems with legacy module loaders like RequireJS.
Browserify with the --standalone
option will be used to build the standalone Enzyme UMD module. This is a good post describing Browserify's --standalone
option: http://www.forbeslindesay.co.uk/post/46324645400/standalone-browserify-builds.
- Install npm dependencies
npm install --save-dev browserify
npm install --save-dev enzyme
npm install --save-dev react
npm install --save-dev react-addons-test-utils
npm install --save-dev react-dom
- Build using Browserify
From the command line:
./node_modules/.bin/browserify --standalone enzyme --out-dir -x 'react/addons' -x 'react/lib/ReactContext' -x 'react/lib/ExecutionEnvironment' node_modules/enzyme/build/index.js -o app_dir
Where app_dir
is the name of your require_js app dir. Alternatively, you could create a node script.
- Use in your App
define(["enzyme"], function(enzyme) {
let mount = enzyme.mount;
//...
});
The above content was working for me for the old enzyme 2.8.4 but now when I am trying to upgrade to enzyme 3.1.1 which needs adapter as well to make test running, gives error while running test. Can you please tell me how we can use enzyme-adapter-react-15.4 with AMD module. I tried to browserify the adapter module as well and try to use with new browserified enzyme AMD module but it didn't work for me. I see enzyme-adapter-react-15.4 has peer dependencies on these, If i exclude them while doing browserify, it complains during runtime.
enzyme
react
react-dom
react-addons-test-utils