-
-
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
chore: fully clear require/import cache in non-parallel watch mode #5155
Closed
Kartones
wants to merge
6
commits into
mochajs:main
from
kartones-forks:clear-require-cache-in-watch-mode
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2615a18
refactor: extract handleRequires() to file
Kartones 55d042e
fix: full import/require cache blast when running non-parallel watch
Kartones 24aaa00
fix: don't use disposed mocha instance to query parallel mode
Kartones 7f26597
fix: proper import() cache busting + not do empty cache key busting
Kartones 9df2bf1
fix: specific mocha binary filters when blasting cache
Kartones 8dfc1b4
feat: always bust require cache when in watch mode
Kartones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const debug = require('debug')('mocha:cli:handle:requires'); | ||
const {requireOrImport} = require('../nodejs/esm-utils'); | ||
const PluginLoader = require('../plugin-loader'); | ||
|
||
/** | ||
* `require()` the modules as required by `--require <require>`. | ||
* | ||
* Returns array of `mochaHooks` exports, if any. | ||
* @param {string[]} requires - Modules to require | ||
* @returns {Promise<object>} Plugin implementations | ||
* @private | ||
*/ | ||
exports.handleRequires = async (requires = [], {ignoredPlugins = []} = {}) => { | ||
const pluginLoader = PluginLoader.create({ignore: ignoredPlugins}); | ||
for await (const mod of requires) { | ||
let modpath = mod; | ||
// this is relative to cwd | ||
if (fs.existsSync(mod) || fs.existsSync(`${mod}.js`)) { | ||
modpath = path.resolve(mod); | ||
debug('resolved required file %s to %s', mod, modpath); | ||
} | ||
const requiredModule = await requireOrImport(modpath); | ||
if (requiredModule && typeof requiredModule === 'object') { | ||
if (pluginLoader.load(requiredModule)) { | ||
debug('found one or more plugin implementations in %s', modpath); | ||
} | ||
} | ||
debug('loaded required module "%s"', mod); | ||
} | ||
const plugins = await pluginLoader.finalize(); | ||
if (Object.keys(plugins).length) { | ||
debug('finalized plugin implementations: %O', plugins); | ||
} | ||
return plugins; | ||
}; |
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
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
11 changes: 11 additions & 0 deletions
11
test/integration/fixtures/options/watch/dependency-with-state.fixture.js
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,11 @@ | ||
let flag = false; | ||
|
||
module.exports.getFlag = () => flag; | ||
|
||
module.exports.enableFlag = () => { | ||
flag = true; | ||
}; | ||
|
||
module.exports.disableFlag = () => { | ||
flag = false; | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/integration/fixtures/options/watch/hook-mutating-dependency.fixture.js
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,9 @@ | ||
const dependency = require('./lib/dependency-with-state'); | ||
|
||
module.exports = { | ||
mochaHooks: { | ||
beforeEach: () => { | ||
dependency.enableFlag(); | ||
} | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/integration/fixtures/options/watch/test-mutating-dependency.fixture.js
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,9 @@ | ||
const dependency = require('./lib/dependency-with-state'); | ||
|
||
it('checks and mutates dependency', () => { | ||
if (dependency.getFlag()) { | ||
throw new Error('test failed'); | ||
} | ||
// Will pass 1st run, fail on subsequent ones | ||
dependency.enableFlag(); | ||
}); |
17 changes: 17 additions & 0 deletions
17
test/integration/fixtures/options/watch/test-with-hook-mutated-dependency.fixture.js
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,17 @@ | ||
const dependency = require('./lib/dependency-with-state'); | ||
|
||
// Will fail 1st run, unless hook runs | ||
before(() => { | ||
dependency.disableFlag(); | ||
}); | ||
|
||
// Will pass 1st run, fail on subsequent ones, unless hook runs | ||
afterEach(() => { | ||
dependency.disableFlag(); | ||
}); | ||
|
||
it('hook should have mutated dependency', () => { | ||
if (!dependency.getFlag()) { | ||
throw new Error('test failed'); | ||
} | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Also doing some tests, to avoid extra invalidations, this should be
node_modules/mocha/
. Confirmed that that single filtering works with Mocha tests, standard mocha dependency/package, and a custom repository & branch path (at least under PNPM).