-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interface "exports" not working in browser #2198
Comments
Some digging into the code and experimenting has yielded the following insights:
;(function(module){"use strict" // "use strict" optional
;(function(module, exports){
// SAME TEST CODE HERE
if (typeof mocha !== "undefined") { mocha.suite.emit("require", module.exports) }
}(module, module.exports))
}(typeof module !== "undefined" ? module : {exports:{}})) It's a bit of boilerplate, but largely out of the way of the actual test code, and it saves me from having to handle similar boilerplate in the page as I'm adding or removing tests from the in-page list and from having to depend on additional tools and/or extra steps to prepare the tests and/or page. However, there are some possible alternatives that don't require changing the test scripts themselves:
In any case, I don't think there's a good way to make it happen just by modifications to Mocha without risk of odd behavior in edge cases. Instead, I'd like to recommend just documenting how to use the Until then, hopefully this comment itself can help anyone else looking for a solution to run |
On further study, I think this may theoretically actually be doable -- with AMD modules. Which isn't necessarily as bad as it sounds. For Node/non-browser running, if I understand correctly (haven't tested extensively yet, but I'll be back to let you know if I do run into any problems with it) they'll need to be like this: var define = typeof define !== "undefined" ? define : function(factory){ factory(require, exports, module) }
define(function(require, exports, module) {
// SAME TEST MODULE CODE HERE AS IN NORMAL EXPORT-STYLE TEST (except possible using only exports rather than module.exports?)
}) That's even less boilerplate than my earlier solution. It's not a full implementation of an AMD loader in Node.js (although you can find those out there), but it should be sufficient for the typical exports-based Mocha test, I would think. And then in the browser, we'd want mocha's "exports" suite setup to provide a similarly rudimentary AMD loader implementation that sends the test modules to the exports interface. It would need some criteria (preferably configurable) to tell which AMD modules are tests (in case they're pulling in other AMD modules), perhaps something like On the other hand, whether it's a good or bad idea may depend on whether or not you want to increase mutual compatibility with another major JavaScript testing framework out there, since I recall noticing examples for that one that opened with In any case, it's another option to consider. And, I suppose, the exports-interface browser AMD loader wouldn't necessarily need to be supplied by Mocha even; I could probably write up a small project for it. (The downside to that is it's pulled in separately rather than pulled in or not based on whether the "exports" interface is in use. I don't know what would happen if, say, when using another Mocha interface the AMD loader tried to pass a test module to |
I was able to set up the AMD-modules idea as an AMD loader plugin that works quite well for me so far: https://github.com/ScottFreeCode/mocha-exports-amd |
Getting error due to
module
not being defined, no tests detected because of that error in the JS file.Windows 8.1, IE 11 & Firefox 45.0.1, Mocha 2.4.5
Works in Mocha commandline. Also works in browser if I use BDD interface instead of exports interface.
./index.html:
./test/X.js:
The text was updated successfully, but these errors were encountered: