Automattic / expect.js

Minimalistic BDD-style assertions for Node.JS and the browser.
2.11k stars 209 forks source link

Disable on production #49

Open onedayitwillmake opened 11 years ago

onedayitwillmake commented 11 years ago

Is there a way to disable expect.js?

onedayitwillmake commented 11 years ago

Currently I'm using this to disable expect.js on production. It's working well for my needs, in case any one wants to use it.

/**
 * Makes all expect.js calls a noop
 * This is used on production
 */
disableExpectjs: function(){

  window.expect = function(){ return window.expect; }
  var flags = {
    not: ['to', 'be', 'have', 'include', 'only', 'equal']
    , to: ['be', 'have', 'include', 'only', 'not']
    , only: ['have']
    , have: ['own', 'property']
    , be: ['an', 'ok', 'empty', 'a']
  };

  for(var key in flags ) {
    if( !flags.hasOwnProperty( key ) ) continue;
    window.expect[key] = window.expect;
    for(var i = 0; i < flags[key].length;i++) {
      window.expect[ key] [ flags[key][i] ] = window.expect;
    }
  }
},
skeggse commented 10 years ago

To my knowledge, Expect.js is designed to be used in unit tests. Generally, those unit tests are not included in the actual code, but in separate files that vary with project structure, and if that is the case then you should just not include the tests in your production packaged code. This doesn't really seem like a good feature for Expect.js.