Skip to content

Commit

Permalink
fix required test files caching, closes mochajs#736
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Sorin committed Aug 13, 2016
1 parent 2a51080 commit fec9198
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ test-browser-tdd:

test-jsapi:
@printf "==> [Test :: JS API]\n"
node test/jsapi
node test/jsapi/index.js
node test/jsapi/cache/index.js

test-unit:
@printf "==> [Test :: Unit]\n"
Expand Down
28 changes: 28 additions & 0 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ function Mocha(options) {
if (options.slow) {
this.slow(options.slow);
}

// files which were in require.cache
// by the moment of mocha instance creation
this._requireCache = this._getRequireCache();
}

/**
Expand Down Expand Up @@ -220,6 +224,8 @@ Mocha.prototype.loadFiles = function(fn) {
suite.emit('require', require(file), file, self);
suite.emit('post-require', global, file, self);
});

this._flushRequireCache();
fn && fn();
};

Expand All @@ -246,6 +252,28 @@ Mocha.prototype._growl = function(runner, reporter) {
});
};

/**
* Get require.cache files (node only)
*
* @api private
*/
Mocha.prototype._getRequireCache = function() {
return Object.keys(require.cache || {});
};

/**
* Flush test files from require.cache
*
* @api private
*/
Mocha.prototype._flushRequireCache = function() {
this._getRequireCache().forEach(function(filePath) {
if (this._requireCache.indexOf(filePath) === -1) {
delete require.cache[filePath];
}
}, this);
};

/**
* Escape string and add it to grep as a regexp.
*
Expand Down
4 changes: 4 additions & 0 deletions test/jsapi/cache/include.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function() {
global[__filename] = global[__filename] || 0;
global[__filename]++;
};
20 changes: 20 additions & 0 deletions test/jsapi/cache/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var Mocha = require('../../../')
, assert = require('assert')
, path = require('path');

var testFile = path.resolve(__dirname, 'spec.js');
var includeFile = path.resolve(__dirname, 'include.js');

var set1 = new Mocha();
set1.addFile(testFile);

var set2 = new Mocha();
set2.addFile(testFile);

set1.run(function () {
assert.strictEqual(global[includeFile], 1, 'JSAPI test #1 didn\'t run');

set2.run(function () {
assert.strictEqual(global[includeFile], 2, 'JSAPI test #2 didn\'t run');
});
});
8 changes: 8 additions & 0 deletions test/jsapi/cache/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var path = require('path');
var increment = require('./include');

describe('Mocha', function () {
it('should increment global counter', function () {
increment();
});
});

0 comments on commit fec9198

Please sign in to comment.