artsy / benv

Stub a browser environment in node.js and headlessly test your client-side code.
MIT License
73 stars 19 forks source link

jQuery still holds the previous window object #9

Closed TYRONEMICHAEL closed 10 years ago

TYRONEMICHAEL commented 10 years ago

Strange issue which I tried to fork and resolve. Benv.teardown(false) cleans up the dom when using jQuery. I think this is because jQuery holds a reference to the window object when requiring it (preferredDoc = window.document, which is a self invoking function which only gets called once inside jQuery's source).

So I think when you delete the window document in the teardown function, jQuery still holds reference to the old document.

You can prove this by appending results to the dom, calling teardown, then in a new test console.log(document.getElementsByTagName('body')[0].innerHTML, $('body').html());. You will see that they retrieve different results. jQuery still shows the elements appended to the dom previously.

craigspaeth commented 10 years ago

Yeah I've seen this too and I thought I fixed it with benv.teardown(false) but apparently not :-/

Could you pull request a failing test or copy & paste some code reproducing the issue and I'll see if there's a sane way to work around it.

This is tricky b/c there's plenty of popular libraries that hold references to DOM globals like this and I'm not sure if benv should try to provide a way to work around node's require caching behavior to restore those references upon say a benv.require, or if benv shouldn't delete certain globals b/c libraries tend to cache references to them. ¯_(ツ)_/¯ I'd love to hear others' opinions.

craigspaeth commented 10 years ago

I think this can currently be handled, see my comments on the PR: https://github.com/artsy/benv/pull/10#issuecomment-39586731