Skip to content

Commit 076c3ab

Browse files
committed
Add clearCacheFilter option
1 parent 8460539 commit 076c3ab

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = function(grunt) {
3535
captureFile: 'results.txt', // Optionally capture the reporter output to a file
3636
quiet: false, // Optionally suppress output to standard out (defaults to false)
3737
clearRequireCache: false, // Optionally clear the require cache before running tests (defaults to false)
38+
clearCacheFilter: (key) => true, // Optionally defines which files should keep in cache
3839
noFail: false // Optionally set to not fail on failed tests (will still fail on other errors)
3940
},
4041
src: ['test/**/*.js']
@@ -54,6 +55,7 @@ The following options are specific to `grunt-mocha-test` (ie. not mocha options)
5455
- `captureFile` - specify a file to capture all output to (will include any output from `console.log`)
5556
- `quiet` - `true` to not output anything to console (normally used with the `captureFile` option when console output would not be human readable)
5657
- `clearRequireCache` - `true` to clear the require cache before each test run (normally used with watch when not spawning each test run in a new `nodejs` context)
58+
- `clearCacheFilter` - `function() { return true/false }` to say which files should remain in cache. Only works with clearRequireCach set to `true`)
5759
- `noFail` - `true` to prevent the task failing on failed tests. Useful for CI setups where test reports are processed separately. Will still fail on other errors
5860

5961
The following mocha options have also been tested (others may have been added since the time of writing through changes to mocha)

tasks/lib/MochaWrapper.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ function MochaWrapper(params) {
2929

3030
var mocha = new Mocha(params.options);
3131

32+
var filterFunc = params.options.clearCacheFilter;
3233
if (params.options.clearRequireCache === true) {
3334
Object.keys(require.cache).forEach(function (key) {
34-
delete require.cache[key];
35+
if (typeof filterFunc !== 'function' || !filterFunc(key)) {
36+
delete require.cache[key];
37+
}
3538
});
3639
}
3740

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var gruntShared = require('../../helpers/grunt-shared');
2+
module.exports = function(grunt) {
3+
gruntShared(grunt, __dirname, {
4+
mochaTest: {
5+
options: {
6+
reporter: 'spec'
7+
},
8+
on: {
9+
options: {
10+
clearRequireCache: true,
11+
clearCacheFilter: (key) => /mocha-test.js/.exec(key)
12+
},
13+
src: ['teston.js']
14+
},
15+
off: {
16+
options: {
17+
clearRequireCache: true
18+
},
19+
src: ['testoff.js']
20+
}
21+
}
22+
}, ['mochaTest:on', 'mochaTest:off']);
23+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var expect = require('chai').expect;
2+
var path = require('path');
3+
4+
describe('check content of require cache does not contain tasks/mocha.js', function() {
5+
it('should pass', function() {
6+
expect(require.cache).not.to.have.property(path.resolve(__dirname, '../../../tasks/mocha-test.js'));
7+
});
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var expect = require('chai').expect;
2+
var path = require('path');
3+
4+
describe('check content of require cache contain tasks/mocha.js', function() {
5+
it('should pass', function() {
6+
expect(require.cache).to.have.property(path.resolve(__dirname, '../../../tasks/mocha-test.js'));
7+
});
8+
});

test/tasks/grunt-mocha-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ describe('grunt-mocha-test', function() {
190190
});
191191
});
192192

193+
it('should support the clearCacheFilterOption', function(done) {
194+
execScenario('clearCacheFilterOption', function(error, stdout, stderr) {
195+
expect(stdout).to.match(/mochaTest:on/);
196+
expect(stdout).to.match(/mochaTest:off/);
197+
expect(stdout).to.match(/1 passing/);
198+
expect(stdout).not.to.match(/0 passing/);
199+
expect(stdout).not.to.match(/2 passing/);
200+
expect(stdout).to.match(/Done./);
201+
expect(stderr).to.equal('');
202+
done();
203+
});
204+
});
205+
193206
it('should support the grep option', function(done) {
194207
execScenario('grepOption', function(error, stdout, stderr) {
195208
expect(stdout).to.match(/tests that match grep/);

0 commit comments

Comments
 (0)