-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it has to be, but why doesn't the unloadFiles()
implementation just call unloadFile()
in files.forEach()
?
@plroebuck Fixed. |
test/node-unit/mocha.spec.js
Outdated
mocha.addFile(filePath); | ||
mocha.loadFiles(); | ||
|
||
expect( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can write something like this, which IMO is more appropriate:
expect(require.cache, 'to have property', require.resolve(filePath));
then later
expect(require.cache, 'not to have property', require.resolve(filePath));
}); | ||
|
||
describe('.unloadFiles()', function() { | ||
it('should unload all test files from cache', function() { |
There was a problem hiding this comment.
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
.
Hi, thanks for the PR. The intent here seems to be to create a public API; methods shouldn't be marked private, and they should have complete JSDoc docstrings and be marked public. Moreover, Mocha must consume this API somehow, or it simply adds maintenance burden with little benefit. The only place currently where it could and should be used is in What's the use case for this, anyhow? |
Unfortunately, I don't think that I would be able to use this feature in existing codebase, but what it does is: it improves public API and allows to reuse existing Mocha instance (In terms of when you need to change Mocha dataset) |
@boneskull wrote:
While I definitely understand this sentiment, @WithoutCaps: It would seem appropriate make the documentation for each of /**
* Unloads registered files.
* This will remove them from Node's `require` cache, allowing them to be safely reloaded.
*
* @public
* @see {@link Mocha#loadFiles}
* @see {@link Mocha#run}
*/
Mocha.prototype.unloadFiles = function() { |
Tangentially related, I find no usage of the |
See PR #3534. |
Description of the Change
Added a feature which allows unloading test files from
require
cache.This feature allows reusing data/objects since there is no need to create a new Mocha process.
(Feature is orientated to those that are accessing Mocha as a module, not CLI)
Alternate Designs
Why should this be in core?
loadFiles
function, so I would think that there should be a function that does the opposite.Benefits
Better reusability, fewer headaches for programmers
Possible Drawbacks
None as far as I know
Applicable issues
This is just a small code enhancement, has no impact on current production code.