An open API service indexing awesome lists of open source software.

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

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 or exports:__

1. Overwrite module.exports, assign attrs to exports and/or this:

module.exports = { name: 'module.exports' };

exports.name = 'exports';
this.name ='this';

2. Overwrite exports, assign attrs to module.exports and/or this:

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 or module.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 the before_tests hook to run browserify.

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.)