https://github.com/dfkaye/module-api-tests
explores interesting side-effects and anti-patterns in the exports api
https://github.com/dfkaye/module-api-tests
Last synced: 3 months ago
JSON representation
explores interesting side-effects and anti-patterns in the exports api
- Host: GitHub
- URL: https://github.com/dfkaye/module-api-tests
- Owner: dfkaye
- Created: 2013-10-14T17:36:28.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-10-09T19:44:37.000Z (over 10 years ago)
- Last Synced: 2025-01-02T03:46:38.986Z (5 months ago)
- Language: JavaScript
- Size: 199 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
module-api-tests
================Explores side-effects, anti-patterns in the commonjs exports api, because some
things just can't be assumed.module.exports === exports === this ...
---------------------------------------__...unless you overwrite or reassign
module.exports
orexports
:__1. Overwrite
module.exports
, assign attrs toexports
and/orthis
:module.exports = { name: 'module.exports' };
exports.name = 'exports';
this.name ='this';2. 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';
3. 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__.4. 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';
tests
-----View the generated test page on
rawgit.
jasmine-node
------------Misko Hevery's (mhevery) [jasmine-node](https://github.com/mhevery/jasmine-node)
package was used to drive these tests initially.jasmine-node --verbose ./test/suite.spec.js
testem
------Toby Ho's (@airporty) [testem](https://github.com/airportyh/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 thebefore_tests
hook to runbrowserify
.browserify
----------James Halliday's (@substack) node-to-browser source bundler utility,
[browserify](https://github.com/substack/node-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.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.)