This code sets up a file watcher that automatically re-runs JavaScript tests whenever changes are detected in specified files, allowing for continuous testing during development.
npm run import -- "test runner"var chokidar = require("chokidar");
var importer = require('../Core');
var testCells = importer.import("test cells");
// TODO: code analysis to combine blocks into modules?
var rateLimiter, done = true;
function testWatcher(files, tests) {
files = typeof files === 'string' ? [files] : files;
console.log('watching ' + files + ' - ' + path.resolve('.'))
var watcher = chokidar.watch(files, {
interval: 1000,
atomic: 1000,
awaitWriteFinish: true
});
watcher.on("change", function(event, path) {
if(!done) {
return;
}
console.log('running all tests');
done = false;
return testCells(tests).then(() => (done = true))
});
testCells(tests);
var stdin = process.openStdin();
stdin.addListener("data", function(d) {
stdin.close();
});
}
module.exports = testWatcher;
const chokidar = require('chokidar');
const path = require('path');
const importer = require('../Core');
const { testCells } = importer;
const rateLimiter = null; // initialized but not used
let done = true;
/**
* Watches for file changes and runs tests accordingly.
* @param {Array|string} files - files or a single file to watch
* @param {Array} tests - tests to run
*/
async function testWatcher(files, tests) {
files = Array.isArray(files)? files : [files];
console.log(`Watching ${files.join(', ')} - ${path.resolve('.')}`);
const watcher = chokidar.watch(files, {
interval: 1000,
atomic: true, // deprecated, use awaitWriteFinish instead
awaitWriteFinish: true
});
watcher.on('change', async (event, path) => {
if (!done) {
return;
}
console.log('Running all tests');
done = false;
try {
await testCells(tests);
} catch (error) {
console.error('Error running tests:', error);
} finally {
done = true;
}
});
try {
await testCells(tests);
} catch (error) {
console.error('Error running tests:', error);
}
}
// Use a stream to close the stdin stream
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', () => {
rl.close();
});
module.exports = testWatcher;This code sets up a file watcher that automatically re-runs tests whenever changes are detected in specified files.
Here's a breakdown:
Dependencies:
chokidar: A library for watching files and directories for changes.importer: A custom module for importing code (likely related to the test runner).testCells: A function imported from importer that executes JavaScript tests.Initialization:
rateLimiter and done variables are declared, but not used in the provided code.testWatcher function is defined, taking files (paths to watch) and tests (test code) as arguments.File Watching:
chokidar.watch is used to monitor the specified files for changes.interval: Check for changes every 1000 milliseconds (1 second).atomic: Wait for up to 1000 milliseconds after a change before processing it.awaitWriteFinish: Ensure that file writes are complete before triggering a change event.Change Handling:
watcher.on("change", ...) event listener is triggered whenever a change is detected.done is false (indicating tests are already running), the event is ignored.done is set to false, testCells is called to run the tests, and done is set back to true when the tests complete.Manual Trigger:
process.stdin to allow manual triggering of tests by pressing Enter.In essence, this code provides a way to continuously monitor files for changes and automatically re-run tests whenever modifications are detected.