Explores side-effects, anti-patterns in the commonjs exports api, because some things just can't be assumed.
...unless you overwrite or reassign module.exports
or exports
:
-
Overwrite
module.exports
, assign attrs toexports
and/orthis
:module.exports = { name: 'module.exports' };
exports.name = 'exports'; this.name ='this';
-
Overwrite
exports
, assign attrs tomodule.exports
and/orthis
:module.exports.name = 'module.exports';
exports = { name: exports' }; this.name ='this';
In both cases, module.exports wins:
require('module-test').name === 'module.exports';
-
And unlike the AMD define() pattern:
delete module.exports; return 'something else'; // return this explicitly
returning a value explicitly from a module with a return statement does not export that value. Again, module.exports wins.
-
Adding attributes to
this
,exports
ormodule.exports
:module.exports.name = 'module.exports'; exports.name = 'exports'; this.name ='this';
In this case, last assignment wins:
require('assignment').name === 'this';
View the generated test page on rawgit.
Misko Hevery's (mhevery) jasmine-node package was used to drive these tests initially.
jasmine-node --verbose ./test/suite.spec.js
Toby Ho's (@airporty) testem package was used to drive these tests in the browser as well as on node.
The testem.json file defines a custom test page to run jasmine tests in browsers
testem
...and defines a launcher for jasmine-node:
testem -l jasmine-node
... and uses the before_tests
hook to run browserify
.
James Halliday's (@substack) node-to-browser source bundler utility, browserify:
browserify ./test/suite.spec.js > ./test/bundle-spec.js
... is used to generated the html-only version of all the javascript tests used by both testem and viewable in a standalone page on rawgit.
The custom test page can be viewed on rawgit, Ryan Grove's (@yaypie) invaluable service for remote viewing your repo's html in a browser so you don't have to run a server locally.
(Note: The testem script does not execute on rawgit, only the jasmine and test scripts in the browserified bundle.)