Skip to content

Conversation

lukechilds
Copy link
Contributor

Fixes #882

```
$ npm install --save-dev jsdom
$ npm install --save-dev node-browser-environment
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very verbose module name. Why not simply browser-env? Node is already implied.

Copy link
Contributor Author

@lukechilds lukechilds Oct 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose it because I thought that was the exact string most people would search for trying to find something like this.

browser-env is already taken, in hind site browser-environment would probably of been better. I'm reluctant to change it now as it's already had a fair amount of downloads and I don't think this can be changed in a backwards compatible way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty easy. You just bump major on the existing one and deprecate with a message saying it's been renamed. We bump major first so the deprecation notice doesn't annoy existing users until they bump a version. I would personally do it as that module is going to be used a lot and browser-environment would look nicer in the import statement, but your choice ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, https://github.com/CamShaft/browser-env is deprecated, so you could just request the module name.

https://docs.npmjs.com/misc/disputes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot on the deprecation, yeah I'll rename it. If you wanna hold off on merging this for now I'll update you when it's sorted 👍

Quick Q: Why bump major first? Wouldn't it be better to notify existing users? If they've installed with the default ^ range they'll be expecting to get SemVer minor/patch updates automatically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukechilds Not very relevant in this situation, as it's a top-level module, so maybe not necessary, but when your module is deep in a dependency tree, people will start opening issues on every module that depends on it somewhere in the dependency tree about updating, which is very annoying for the maintainers and a lot of wasted time. Users think warnings are critical.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus node-browser-environment is dead, long live browser-env! 🎉

global.document = require('jsdom').jsdom('<body></body>');
global.window = document.defaultView;
global.navigator = window.navigator;
require('node-browser-environment')();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use import, not require.

By default `node-browser-environment` will add all global browser variables to node's global scope creating a full browser environment. This should have good compatibility with most front end libraries, however it's generally not a good idea to create loads of global variables if you don't need to. If you know exactly which browser variables you need you can pass an array of required properties.

```js
require('node-browser-environment')(['window', 'document', 'navigator']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import

```

## Configure tests to use jsdom
By default `node-browser-environment` will add all global browser variables to node's global scope creating a full browser environment. This should have good compatibility with most front end libraries, however it's generally not a good idea to create loads of global variables if you don't need to. If you know exactly which browser variables you need you can pass an array of required properties.
Copy link
Member

@sindresorhus sindresorhus Oct 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node => Node.js

front end => front-end

loads of => lots of

@sindresorhus
Copy link
Member

@lukechilds Are you committed to keeping this module up to date with jsdom?

@lukechilds
Copy link
Contributor Author

@sindresorhus Yep, I've pinned the version number for jsdom to stop any jsdom breaking changes breaking AVA tests and I've got Greenkeeper set up to notify me of jsdom updates. If you look through the commit history you'll see I normally update this within 24 hours.

I've got no intention of abandoning this module but I'm happy to add you or anyone else on the AVA team as a maintainer if you want.

Working on your requested changes now 👍

@lukechilds
Copy link
Contributor Author

@sindresorhus Done requested changes 🙂

@lukechilds
Copy link
Contributor Author

This should be good to go now.

Might be worth also adding a simpler example just to illustrate the browser globals are available? The current React example hides all the DOM stuff inside React.

@lukechilds
Copy link
Contributor Author

I'm thinking something along the lines of:

test/my.dom.test.js:

import test from 'ava';

test('Insert to DOM', t => {
  const div = document.createElement('div');
  document.body.appendChild(div);

  t.is(document.querySelector('div'), div);
});

It could be as well as the current React test or instead of. What do you think?

@sindresorhus
Copy link
Member

Good idea. Should be in addition to the React one.

@lukechilds
Copy link
Contributor Author

@sindresorhus Done 👍

@sindresorhus sindresorhus changed the title Replace jsdom with node-browser-environment in browser recipe Replace jsdom with browser-env in browser recipe Oct 5, 2016
@sindresorhus sindresorhus merged commit 106a4be into avajs:master Oct 5, 2016
@sindresorhus
Copy link
Member

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants