nikku / karma-browserify

A fast Browserify integration for Karma that handles large projects with ease
MIT License
321 stars 49 forks source link

Polluted Tests #142

Closed skortchmark9 closed 9 years ago

skortchmark9 commented 9 years ago

Tests are being polluted between runs: lib.js

var private = 'foo';
module.exports = {
   setPrivateVar: function(var) {private = var;},
   getPrivateVar: function() {return private;}
};

file1.js

var lib = require('./lib.js');
lib.setPrivateVar('bar');

file1.test.js

var a = require('./file1.js');

lib.test.js

var lib = require('./lib.js');
lib.getPrivateVar() == 'bar'; //should be 'foo'

File 1 is getting tested before lib - and lib test is failing as a result. Maybe there could be some way of clearing a module between runs?

bendrucker commented 9 years ago

Please post the config you're using

skortchmark9 commented 9 years ago
// Karma configuration
// Generated on Tue Jul 07 2015 20:47:56 GMT-0400 (EDT)

module.exports = function(config) {
  config.set({

  // base path that will be used to resolve all patterns (eg. files, exclude)
  basePath: '',

  // frameworks to use
  // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
  frameworks: ['browserify', 'jasmine'],

  // list of files / patterns to load in the browser
  files: [
    'js/spec/*Spec.js'
  ],

  // list of files to exclude
  exclude: [
  ],

  // preprocess matching files before serving them to the browser
  // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
   preprocessors: {
    'js/spec/*Spec.js': [ 'browserify' ]
  },

  browserify: {
    debug: true
  },

  // test results reporter to use
  // possible values: 'dots', 'progress'
  // available reporters: https://npmjs.org/browse/keyword/karma-reporter
  reporters: ['spec'],

  // web server port
  port: 9876,

  // enable / disable colors in the output (reporters and logs)
  colors: true,

  // level of logging
  // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
  logLevel: config.LOG_INFO,

  // enable / disable watching file and executing tests whenever any file changes
  autoWatch: false,

  // start these browsers
  // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
  browsers: ['PhantomJS'],

  // Continuous Integration mode
  // if true, Karma captures browsers, runs the tests and exits
  singleRun: true
  })
}
bendrucker commented 9 years ago

Ah, I misunderstood. If you're looking for tests to run in a particular order, you need to orchestrate them in code. karma-browserify can't flush the require cache for you nor should it.

skortchmark9 commented 9 years ago

Can you explain why not? I don't want the tests to run in a specific order, just independently.

I don't really know how the require cache works, but couldn't we work around it by offering a way to separate different test files into independent bundles? It seems like that would be a more true-life way of testing anyway, since you generally load one browserified script for one page , not the whole site's worth.

nikku commented 9 years ago

If you would like to have independent tests, you need to write them in an independent manner.

This includes properly cleaning up the tests side effects in a afterEach or after hook.

skortchmark9 commented 9 years ago

But both of the tests in the example I posted are written in an independent manner - neither test is even doing anything besides requiring the code to be tested.

Dealing with the pollution in an after hook means that in order to even test the code you need to write it so that it can be reset to its initial state, which is a huge burden on developers. It's clearly a problem.

bendrucker commented 9 years ago

Semantics aren't the issue here. I'm not aware of any test runner that manipulates the module cache in order to accomplish the effect you want. We can't support it.