-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rip out sinon, mocha, and jsdom dependencies
This refactor addresses several issues regarding enzyme working in a variety of environments, and the library just generally not making any assumptions about the environment that tests will be run in. For the most part, this amounts to: - remove direct dependency on jsdom - remove direct dependency on sinon - remove assumed dependency on mocha In addition to this, I would like to create several "example" projects that are some basic boilerplate to getting enzyme up and running with some combination of bundler/test runner/etc. These projects can end up being devDependencies to enzyme and we can run their tests as part of our tests, which will ensure that changes we make to enzyme will be compatible with environments we claim to support moving forward. Lastly, as a matter of organization, tests have been moved from `src/__tests__/*` to `tests/*`. Left to do for this to be mergable is: [ ] Add a "guides" section in the docs explaining how to use enzyme in different environments [ ] Add example projects as dev dependencies, include their tests in enzyme's test script
- Loading branch information
1 parent
ac165c8
commit 1105c81
Showing
26 changed files
with
393 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Enzyme Guides | ||
|
||
- [*Using Enzyme with Browserify*](guides/browserify.md) | ||
- [*Using Enzyme with WebPack*](guides/webpack.md) | ||
- [*Using Enzyme with JSDOM*](guides/jsdom.md) | ||
- [*Using Enzyme with React Native*](guides/react-native.md) | ||
- [*Using Enzyme with Jest*](guides/jest.md) | ||
- [*Using Enzyme with Karma*](guides/karma.md) | ||
- [*Using Enzyme with Mocha*](guides/mocha.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Using Enzyme with Browserify | ||
|
||
If you are using a test runner that runs code in a browser-based environment, you may be using | ||
[browserify]() in order to bundle your React code. | ||
|
||
Browserify uses static analysis to create a dependency graph at build-time of your source code to | ||
build a bundle. Enzyme has a hand full of conditional `require()` calls in it in order to remain | ||
compatible with React 0.13 and React 0.14. | ||
|
||
Unfortunately, these conditional requires mean there is a bit of extra setup with bundlers like | ||
browserify. | ||
|
||
In your browserify configuration, you simply need to make sure that the following two files are | ||
labeled as "external", which means they will be ignored: | ||
|
||
``` | ||
react/lib/ReactContext | ||
react/lib/ExecutionEnvironment | ||
``` | ||
|
||
Here is an example piece of configuration code marking these as external: | ||
|
||
```js | ||
var browserify = require('browserify'); | ||
|
||
var b = browserify(); | ||
|
||
// make sure to mark these as external! | ||
b.external('react/lib/ReactContext'); | ||
b.external('react/lib/ExecutionEnvironment'); | ||
|
||
// the rest of your browserify configuration | ||
``` | ||
|
||
|
||
## Example Projects | ||
|
||
- [enzyme-example-karma](https://github.com/lelandrichardson/enzyme-example-karma) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Using Jest with Enzyme | ||
|
||
If you are using Jest with enzyme and using Jest's "automocking" feature, you will need to mark | ||
several modules to be unmocked in your `package.json`: | ||
|
||
```js | ||
/* package.json */ | ||
|
||
"jest": { | ||
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest", | ||
"unmockedModulePathPatterns": [ | ||
"react", | ||
"react-dom", | ||
"react-addons-test-utils", | ||
"fbjs", | ||
"enzyme", | ||
"cheerio", | ||
"htmlparser2", | ||
"underscore", | ||
"lodash", | ||
"domhandler", | ||
"object.assign", | ||
"define-properties", | ||
"function-bind", | ||
"object-keys" | ||
] | ||
} | ||
``` | ||
|
||
## Example Projects | ||
|
||
- [enzyme-example-ject](https://github.com/lelandrichardson/enzyme-example-jest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Using Enzyme with JSDOM | ||
|
||
JSDOM is a JavaScript based headless browser that can be used to create a realistic testing environment. | ||
|
||
Since enzyme's [`mount`](../api/mount.md) API requires a DOM, JSDOM is required in order to use | ||
`mount` if you are not already in a browser environment (ie, a Node environment). | ||
|
||
For the best experience with enzyme, it is recommended that you load a document into the global | ||
scope *before* requiring React for the first time. It is very important that the below script | ||
gets run *before* React's code is run. | ||
|
||
As a result, a standalone script like the one below is generally a good approach: | ||
|
||
```js | ||
/* setup.js */ | ||
|
||
var jsdom = require('jsdom').jsdom; | ||
|
||
var exposedProperties = ['window', 'navigator', 'document']; | ||
|
||
global.document = jsdom(''); | ||
global.window = document.defaultView; | ||
Object.keys(document.defaultView).forEach((property) => { | ||
if (typeof global[property] === 'undefined') { | ||
exposedProperties.push(property); | ||
global[property] = document.defaultView[property]; | ||
} | ||
}); | ||
|
||
global.navigator = { | ||
userAgent: 'node.js' | ||
}; | ||
|
||
documentRef = document; | ||
``` | ||
|
||
|
||
## `describeWithDOM` API and clearing the document after every test | ||
|
||
In previous versions of enzyme, there was a public `describeWithDOM` API which loaded in a new | ||
JSDOM documnent into the global namespace before every test, ensuring that tests were deterministic | ||
and did not have side-effects. | ||
|
||
This approach is no longer recommended. React's source code makes several assumptions about the | ||
environment it is running in, and one of them is that the `global.document` that is found at | ||
"require time" is going to be the one and only document it ever needs to worry about. As a result, | ||
this type of "reloading" ends up causing more pain than it prevents. | ||
|
||
It is important, however, to make sure that your tests using the global DOM APIs do not have leaky | ||
side-effects which could change the results of other tests. Until there is a better option, this is | ||
left to you to ensure. | ||
|
||
|
||
## JSDOM + Mocha | ||
|
||
When testing with JSDOM, the `setup.js` file above needs to be run before the test suite runs. If | ||
you are using mocha, this can be done from the command line using the `--require` option: | ||
|
||
```bash | ||
mocha --require setup.js --recursive path/to/test/dir | ||
``` | ||
|
||
|
||
## Node.js Compatibility | ||
|
||
Jsdom requires node 4 or above. As a result, if you want to use it with `mount`, you will need to | ||
make sure node 4 or iojs is on your machine. If you are stuck using an older version of Node, you | ||
may want to try using a browser-based test runner such as [Karma](../guides/karma.md). | ||
|
||
|
||
### Switching between node versions | ||
|
||
Some times you may need to switch between different versions of node, you can use a CLI tool called | ||
`nvm` to quickly switch between node versions. | ||
|
||
To install NVM: | ||
|
||
```bash | ||
brew install nvm | ||
nvm install 4 | ||
``` | ||
|
||
Now your machine will be running Node 4. You can use the `nvm use` command to switch between the two | ||
environments: | ||
|
||
```bash | ||
nvm use 0.12 | ||
``` | ||
|
||
```bash | ||
nvm use 4 | ||
``` | ||
|
||
|
||
|
||
## Example Projects | ||
|
||
- [enzyme-example-mocha](https://github.com/lelandrichardson/enzyme-example-mocha) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Using Enzyme with Karma | ||
|
||
|
||
|
||
## Enzyme + Karma + Webpack | ||
|
||
See the [webpack guide](webpack.md). | ||
|
||
```js | ||
/* karma.conf.js */ | ||
browserify: { | ||
debug: true, | ||
transform: [ | ||
['babelify', { presets: ['airbnb'] }] | ||
], | ||
configure: function(bundle) { | ||
bundle.on('prebundle', function() { | ||
bundle.external('react/lib/ReactContext'); | ||
bundle.external('react/lib/ExecutionEnvironment'); | ||
}); | ||
} | ||
}, | ||
``` | ||
|
||
## Enzyme + Karma + Browserify | ||
|
||
See the [browserify guide](browserify.md). | ||
|
||
|
||
```js | ||
/* karma.conf.js */ | ||
|
||
webpack: { //kind of a copy of your webpack config | ||
devtool: 'inline-source-map', //just do inline source maps instead of the default | ||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /\/node_modules\//, | ||
loader: 'babel', | ||
query: { | ||
presets: ['airbnb'] | ||
} | ||
} | ||
] | ||
}, | ||
externals: { | ||
'cheerio': 'window', | ||
'react/lib/ExecutionEnvironment': true, | ||
'react/lib/ReactContext': true | ||
} | ||
}, | ||
``` | ||
|
||
|
||
## Example Projects | ||
|
||
- [enzyme-example-karma](https://github.com/lelandrichardson/enzyme-example-karma) | ||
- [enzyme-example-karma-webpack](https://github.com/lelandrichardson/enzyme-example-karma-webpack) |
Oops, something went wrong.