Added unloading files feature from require cache#3388
Added unloading files feature from require cache#3388gytis-ivaskevicius wants to merge 8 commits intomochajs:masterfrom gytis-ivaskevicius:master
Conversation
plroebuck
left a comment
There was a problem hiding this comment.
Not that it has to be, but why doesn't the unloadFiles() implementation just call unloadFile() in files.forEach()?
|
@plroebuck Fixed. |
| mocha.addFile(filePath); | ||
| mocha.loadFiles(); | ||
|
|
||
| expect( |
There was a problem hiding this comment.
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.
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
requirecache.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?
loadFilesfunction, 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.