I'm working on some
documentation for writing unit tests in JavaScript using our
typical mix of RequireJS, jQuery, and Backbone. I ran into an annoying
problem that I hadn't anticipated: loading jQuery via RequireJS in node
isn't exactly straightforward. When I include jQuery in the list of my
module's dependencies and run make mocha
I get this error:
module "jquery" failed with error: TypeError: Cannot call method 'createElement' of undefined
jQuery has been compatible with node.js for a while (see:
[1],
[2]), so I assume this has something to do with how I'm loading
jQuery. It's being loaded with RequireJS instead of node's
require()
.
So, this is one of the quirks I should've known I'd run into when I
decided to test our front-end code in node.js instead of in a web
browser. However, I may still be able to continue down this track.
I could figure out why jQuery isn't being loaded here. Maybe it's
some stupid thing I haven't thought of yet, or maybe it's possible
to set up the requirejs config in test-runner.js
to
behave correctly when loading jQuery.
If I can't figure out how to include jQuery without a browser's DOM, I could try setting up a jsdom instance in the test runner. Really I just don't want to complicate things too much since the test runner is working alright now, and I don't want it to become fragile or hard to understand.