Skip to content

Instantly share code, notes, and snippets.

@jonmbake
Last active July 29, 2018 16:49
Show Gist options
  • Save jonmbake/6362a9f54f57a4506057166aa5ec7027 to your computer and use it in GitHub Desktop.
Save jonmbake/6362a9f54f57a4506057166aa5ec7027 to your computer and use it in GitHub Desktop.
Building Enzyme for Use in Legacy Module Loading Systems (AMD e.g. RequireJS, SES, UMD)

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.

  1. 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
  1. 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.

  1. Use in your App
define(["enzyme"], function(enzyme) {
  let mount = enzyme.mount;
  //...
});
@soroushhakami
Copy link

soroushhakami commented Dec 6, 2016

What does app_dir mean in this case? Whatever I seem to put as argument to -o seems to be the bundled file. If I put the directory to my client-application, I get this errormessage:

Error: EISDIR: illegal operation on a directory, open 'src/main/client/app/alo'

I tried putting -o enzyme.js, and then requiring that file. But then, all I got was a dependency that was undefined unfortunatly.

It feels like I'm missing something here.

@jonmbake
Copy link
Author

jonmbake commented Dec 7, 2016

app_dir is where the module file will get written to. You usually want this to be the same the same as the baseURL require js config value.

@nikhilexe
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment