Skip to content

fix: begin running tests immediately instead of waiting for watcher #5409

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
40 changes: 29 additions & 11 deletions lib/cli/watch-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const chokidar = require('chokidar');
const Context = require('../context');
const collectFiles = require('./collect-files');
const glob = require('glob');
const fs = require('node:fs');

/**
* Exports the `watchRun` function that runs mocha in "watch" mode.
Expand Down Expand Up @@ -215,23 +216,40 @@ const createWatcher = (
beforeRun
});

watcher.on('ready', async () => {
if (!globalFixtureContext) {
debug('triggering global setup');
globalFixtureContext = await mocha.runGlobalSetup();
}
rerunner.run();
// eslint-disable-next-line no-restricted-globals
const startTime = new Date();
let ready = false;
watcher.on('ready', () => {
ready = true;
});

watcher.on('all', (event, filePath) => {
if (event === 'add') {
tracker.regenerate();
watcher.on('all', async function handleEvent(event, filePath, stat) {
if (exiting) return;
if (event === 'unlink' || (stat ? stat.mtime > startTime : ready)) {
if (event === 'add') {
tracker.regenerate();
}
// we don't want to accidentally trigger a run before globalFixtureContext
// has been created. If it hasn't then it's okay to do nothing here because
// the code that creates globalFixtureContext will run the tests afterward
if (tracker.has(filePath) && globalFixtureContext) {
rerunner.scheduleRun();
}
return;
}
if (tracker.has(filePath)) {
rerunner.scheduleRun();
if (!ready && !stat) {
fs.stat(filePath, (err, stat) => {
if (stat) handleEvent(event, filePath, stat);
});
}
});

debug('triggering global setup');
mocha.runGlobalSetup().then(context => {
globalFixtureContext = context;
rerunner.run();
});

hideCursor();
process.on('exit', () => {
showCursor();
Expand Down
Loading