Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unloading files feature from require cache #3388

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ Mocha.prototype.ui = function(name) {
return this;
};

/**
* Unload test file.
*
* @api public
*/
Mocha.prototype.unloadFile = function(filePath) {
delete require.cache[require.resolve(filePath)];
};

/**
* Unload all test files.
*
* @api public
*/
Mocha.prototype.unloadFiles = function() {
this.files.forEach(this.unloadFile);
};

/**
* Load registered files.
*
Expand Down
43 changes: 43 additions & 0 deletions test/node-unit/mocha.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var Mocha = require('../../lib/mocha');
var Path = require('path');

describe('.unloadFile()', function() {
it('should load and unload file to/from cache', function() {
var mocha = new Mocha({});
var filePath = __filename;

mocha.addFile(filePath);
mocha.loadFiles();

expect(require.cache, 'to have property', require.resolve(filePath));
mocha.unloadFile(filePath);
expect(require.cache, 'not to have property', require.resolve(filePath));
});
});

describe('.unloadFiles()', function() {
it('should unload all test files from cache', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this test would be more meaningful if we added multiple files, then made the assertion they were all removed by looping through mocha.files.

var mocha = new Mocha({});
var testFiles = [
__filename,
Path.join(__dirname, 'fs.spec.js'),
Path.join(__dirname, 'color.spec.js')
];

testFiles.forEach(function(file) {
mocha.addFile(file);
});

mocha.loadFiles();
testFiles.forEach(function(file) {
expect(require.cache, 'to have property', require.resolve(file));
});

mocha.unloadFiles();
testFiles.forEach(function(file) {
expect(require.cache, 'not to have property', require.resolve(file));
});
});
});