Skip to content

Commit

Permalink
Fix errors, add dropRequireCacheBeforeRun() option setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Sorin committed Sep 22, 2016
1 parent 8f706e2 commit 92f0248
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ test-browser-tdd:
test-jsapi:
@printf "==> [Test :: JS API]\n"
node test/jsapi/index.js
node test/jsapi/cache/index.js
node test/jsapi/cache/index.simple.js
node test/jsapi/cache/index.drop-require-cache.js

test-unit:
@printf "==> [Test :: Unit]\n"
Expand Down
13 changes: 12 additions & 1 deletion lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Mocha.prototype.loadFiles = function(fn) {
suite.emit('post-require', global, file, self);
});

if (!process.browser) {
if (!process.browser && this.options.dropRequireCache) {
this._flushRequireCache();
}

Expand Down Expand Up @@ -345,6 +345,17 @@ Mocha.prototype.checkLeaks = function() {
return this;
};

/**
* Delete required files from require.cache before runner starts processing tests
*
* @return {Mocha}
* @api public
*/
Mocha.prototype.dropRequireCacheBeforeRun = function() {
this.options.dropRequireCache = true;
return this;
};

/**
* Display long stack-trace on failing
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ var testFile = path.resolve(__dirname, 'spec.js');
var includeFile = path.resolve(__dirname, 'include.js');

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

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

set1.run(function () {
Expand Down
20 changes: 20 additions & 0 deletions test/jsapi/cache/index.simple.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], 1, 'JSAPI test has probably been executed');
});
});

0 comments on commit 92f0248

Please sign in to comment.