From 92f02489ea8fcc615d6e344ff7f8f2c2901709ba Mon Sep 17 00:00:00 2001 From: Dmitry Sorin Date: Fri, 23 Sep 2016 00:35:07 +0300 Subject: [PATCH] Fix errors, add dropRequireCacheBeforeRun() option setter --- Makefile | 3 ++- lib/mocha.js | 13 +++++++++++- .../{index.js => index.drop-require-cache.js} | 2 ++ test/jsapi/cache/index.simple.js | 20 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) rename test/jsapi/cache/{index.js => index.drop-require-cache.js} (88%) create mode 100644 test/jsapi/cache/index.simple.js diff --git a/Makefile b/Makefile index 4161c4627b..9c93f0244d 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/lib/mocha.js b/lib/mocha.js index 33f359c3b4..fa62c8ddd7 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -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(); } @@ -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 * diff --git a/test/jsapi/cache/index.js b/test/jsapi/cache/index.drop-require-cache.js similarity index 88% rename from test/jsapi/cache/index.js rename to test/jsapi/cache/index.drop-require-cache.js index eb9d8a562f..7c15c3d381 100644 --- a/test/jsapi/cache/index.js +++ b/test/jsapi/cache/index.drop-require-cache.js @@ -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 () { diff --git a/test/jsapi/cache/index.simple.js b/test/jsapi/cache/index.simple.js new file mode 100644 index 0000000000..725dac6ce6 --- /dev/null +++ b/test/jsapi/cache/index.simple.js @@ -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'); + }); +});