test-mole

0.2.0 • Public • Published

test-mole

Test inside javascript closures by sneaking a mole

NPM

Build status dependencies devdependencies

Properly written JavaScript is hard to test because most of the code is hidden inside closures. For example, we cannot directly test the inner function isNumber in this example:

var add = (function () {
  function isNumber(x) {
    return typeof x === 'number';
  }
 
  return function add(a, b) {
    if (isNumber(a) &&
      isNumber(b))
      return a + b;
    return 'arguments should be numbers';
  };
}());

We can maybe move isNumber to another library, but often our logic is too specific. We are left with testing isNumber indirectly via add or by playing various tricks to relax the access rules. We could make inner functions used by a constructor function by inspecting the constructor's source. The solution is ugly and brittle.

Inject test mole

Instead of exporting inner functions to make them callable from unit tests, I am proposing the opposite. Inject test mole into your code that will have full access to the private code, but will do nothing in production mode.

Example:

// test/add.js
var add = (function () {
  function isNumber(x) {
    return typeof x === 'number';
  }
 
  testMole.it('returns true for numbers', function () {
    lazyAss(isNumber(1), '1');
    lazyAss(isNumber(-10), '-10');
  });
 
  testMole.it('returns false for strings', function () {
    lazyAss(!isNumber('foo'), 'foo');
    lazyAss(!isNumber('2'), '2');
  });
 
  return function add(a, b) {
    if (isNumber(a) &&
      isNumber(b))
      return a + b;
    return 'arguments should be numbers';
  };
}());

I am using lazy-ass assertions.

$ node add.js
5
$ mocha add.js
5
2 passing (2ms)

When running in production, testMole will not find describe or it functions, so it will default its own methods to be noop. Your code will pay only for source code download and compilation once. When running with the unit test environment, the testMole.it will point at window.it provided by BDD framework.

Install

Node: npm install test-mole then require('test-mole')

Browser: bower install test-mole then <script src="bower_components/test-mole/test-mole.js"></script>

Test mole attaches itself to the window or global under names testMole and tm.

API

Test mole mimics BDD testing framework with the following methods: describe, it and expect.

Small print

Author: Gleb Bahmutov © 2014

License: MIT - do anything with the code, but don't blame me if it does not work.

Spread the word: tweet, star on github, etc.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2014 Gleb Bahmutov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i test-mole

Weekly Downloads

0

Version

0.2.0

License

MIT

Last publish

Collaborators

  • bahmutov