agilgur5 / physijs-webpack

PhysiJS port for bundlers with out-of-the-box support for Webpack and Browserify
Other
9 stars 3 forks source link

Add tests #4

Closed agilgur5 closed 5 years ago

agilgur5 commented 5 years ago

Definitely needed to make some after #2 . I don't have a project to test the browserify integration in and apparently webworkify usage differs quite significantly from webpack's worker-loader.

This issue is more to document my attempts at getting tests to work and failing spectacularly 😕

There's quite some difficulty in getting tests to work as there's a bunch of requirements on browser environments here:

agilgur5 commented 5 years ago

Ok, so jsdom actually doesn't support workers at all, so Worker.postMessage doesn't even exist. jsdom-worker would indeed be the right library to use.

This gave me a lead, because if Workers aren't even supported, how was it erroring on postMessage from physijs_worker.js? Ok, well turns out the browserify integration is even more complex, as not only does webworkify instantiate a Worker for you, it also requires that you export a function with no arguments as your Worker module because all code outside of the exported function will run in the main thread. So I have to create a stub for physijs_worker.js just for webworkify that just exports function () { require('./physijs_worker.js) }. Yea this is way more complex and more effort than I intended to support Browserify......

agilgur5 commented 5 years ago

Ok finally got it to work with browser-env and @react-frontend-developer/jsdom-worker after even more bugs.

Now the only annoying issue remaining is setting up a test runner. The reason it's annoying is because the code needs to be transpiled with browserify (or webpack) to test that the integration is working. But at least when bundling with browserify, all exports are lost -- it's just meant to be run as a script. So I might need to add yet another stub to export as a window global or something and then import it as a global in the test 😕 😕 😕

Unless there's some other way around this...

agilgur5 commented 5 years ago

I ended up adding some small stubs ¯\_(ツ)_/¯ . Resolved by #5 . For future work for Webpack tests see #7