This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to unload files from
require
cache (redux) (mochajs#3726)
* Implemented `Mocha.unloadFile` and `Mocha#unloadFiles`. * Added feature tests, as well as ones for `Mocha#addFiles` and `Mocha#loadFiles`. * Beefed up some unrelated "mocha.js" tests, adding missing chainability tests.
- Loading branch information
Showing
4 changed files
with
139 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const Mocha = require('../../lib/mocha'); | ||
const utils = require('../../lib/utils'); | ||
|
||
describe('Mocha', function() { | ||
const opts = {reporter: utils.noop}; // no output | ||
const testFiles = [ | ||
__filename, | ||
path.join(__dirname, 'cli', 'config.spec.js'), | ||
path.join(__dirname, 'cli', 'run.spec.js') | ||
]; | ||
const resolvedTestFiles = testFiles.map(require.resolve); | ||
|
||
describe('#addFile', function() { | ||
it('should add the given file to the files array', function() { | ||
const mocha = new Mocha(opts); | ||
mocha.addFile(__filename); | ||
expect(mocha.files, 'to have length', 1).and('to contain', __filename); | ||
}); | ||
|
||
it('should be chainable', function() { | ||
const mocha = new Mocha(opts); | ||
expect(mocha.addFile(__filename), 'to be', mocha); | ||
}); | ||
}); | ||
|
||
describe('#loadFiles', function() { | ||
it('should load all files from the files array', function() { | ||
const mocha = new Mocha(opts); | ||
|
||
testFiles.forEach(mocha.addFile, mocha); | ||
mocha.loadFiles(); | ||
expect(require.cache, 'to have keys', resolvedTestFiles); | ||
}); | ||
|
||
it('should execute the optional callback if given', function() { | ||
const mocha = new Mocha(opts); | ||
expect(cb => { | ||
mocha.loadFiles(cb); | ||
}, 'to call the callback'); | ||
}); | ||
}); | ||
|
||
describe('.unloadFile', function() { | ||
it('should unload a specific file from cache', function() { | ||
const resolvedFilePath = require.resolve(__filename); | ||
require(__filename); | ||
expect(require.cache, 'to have key', resolvedFilePath); | ||
|
||
Mocha.unloadFile(__filename); | ||
expect(require.cache, 'not to have key', resolvedFilePath); | ||
}); | ||
}); | ||
|
||
describe('#unloadFiles', function() { | ||
it('should unload all test files from cache', function() { | ||
const mocha = new Mocha(opts); | ||
|
||
testFiles.forEach(mocha.addFile, mocha); | ||
mocha.loadFiles(); | ||
mocha.unloadFiles(); | ||
expect(require.cache, 'not to have keys', resolvedTestFiles); | ||
}); | ||
|
||
it('should be chainable', function() { | ||
const mocha = new Mocha(opts); | ||
expect(mocha.unloadFiles(), 'to be', mocha); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters