From 4745125c2417d057025741d0fa0422f1ba583eb2 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Tue, 15 Feb 2022 19:48:24 +0000 Subject: [PATCH] CLI: Add support for watching `.json`, `.cjs`, and `.mjs` files And add tests for verifying which files are and aren't reacted to, including coverage for the existing ignoring of events from "node_modules". This is prep for https://github.com/qunitjs/qunit/issues/1669. --- src/cli/run.js | 6 ++- test/cli/cli-watch.js | 54 +++++++++++++++++++ .../fixtures/expected/watch-tap-outputs.js | 23 ++++++++ test/cli/helpers/execute.js | 2 +- 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/cli/run.js b/src/cli/run.js index afcafb0f9..c761f759b 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -184,13 +184,17 @@ run.watch = function watch() { const args = Array.prototype.slice.call( arguments ); const baseDir = process.cwd(); + // Include ".json" for test suites that use a data files, + // and for changes to package.json that may affect how a file is parsed (e.g. type=module). + const includeExts = [ ".js", ".json", ".cjs", ".mjs" ]; + const watcher = watch( baseDir, { persistent: true, recursive: true, delay: 0, filter: ( fullpath ) => { return !/\/node_modules\//.test( fullpath ) && - /\.js$/.test( fullpath ); + includeExts.includes( path.extname( fullpath ) ); } }, ( event, fullpath ) => { console.log( `File ${event}: ${path.relative( baseDir, fullpath )}` ); diff --git a/test/cli/cli-watch.js b/test/cli/cli-watch.js index 434473462..e2bce6292 100644 --- a/test/cli/cli-watch.js +++ b/test/cli/cli-watch.js @@ -28,6 +28,10 @@ if ( process.platform === "win32" ) { } QUnit.module( "CLI Watch", function( hooks ) { + hooks.before( function() { + rimraf.sync( fixturePath ); + } ); + hooks.beforeEach( function() { fs.mkdirSync( path.dirname( fixturePath ), { recursive: true } ); fixturify.writeSync( fixturePath, { @@ -171,6 +175,56 @@ QUnit.module( "CLI Watch", function( hooks ) { assert.equal( result.stdout, expectedWatchOutput[ "remove-file" ] ); } ); + // Skip in coverage mode since NYC adds non-default extensions + QUnit[ process.env.NYC_PROCESS_ID ? "skip" : "test" ]( "default file extensions", async assert => { + fixturify.writeSync( fixturePath, { + "tests": { + "setup.js": "QUnit.on('runEnd', function() { process.send('runEnd'); });", + "foo.js": "QUnit.test('foo', function(assert) { assert.true(true); });" + } + } ); + + const command = [ "qunit", "--watch", "watching/tests" ]; + const result = await executeIpc( + command, + execution => { + execution.once( "message", () => { + fixturify.writeSync( fixturePath, { + "x.cjs": "-", + "x.js": "-", + "x.json": "-", + "x.mjs": "-", + "x.ts": "-", + "x.txt": "-", + + "node_modules": { + "x": { + "y.js": "-" + } + }, + + "tests": { + "foo.js": "QUnit.test('foo2', function(assert) { assert.true(true); });", + "setup.js": "QUnit.on('runEnd', function() { process.send('runEnd2'); });" + } + } ); + + execution.once( "message", data => { + + // Ignore other re-runs + if ( data === "runEnd2" ) { + kill( execution ); + } + } ); + } ); + } + ); + + assert.equal( result.code, 0 ); + assert.equal( result.stderr, "" ); + assert.equal( result.stdout, expectedWatchOutput[ "file-extensions" ] ); + } ); + QUnit.test( "aborts and restarts when in middle of run", async assert => { // A proper abort finishes the currently running test and runs any remaining diff --git a/test/cli/fixtures/expected/watch-tap-outputs.js b/test/cli/fixtures/expected/watch-tap-outputs.js index 19ac14c29..8021e5229 100644 --- a/test/cli/fixtures/expected/watch-tap-outputs.js +++ b/test/cli/fixtures/expected/watch-tap-outputs.js @@ -62,6 +62,29 @@ ok 1 foo # skip 0 # todo 0 # fail 0 +Stopping QUnit...`, + + "file-extensions": `TAP version 13 +ok 1 foo +1..1 +# pass 1 +# skip 0 +# todo 0 +# fail 0 +File update: watching/x.cjs +File update: watching/x.js +File update: watching/x.json +File update: watching/x.mjs +File update: watching/tests/foo.js +File update: watching/tests/setup.js +Restarting... +TAP version 13 +ok 1 foo2 +1..1 +# pass 1 +# skip 0 +# todo 0 +# fail 0 Stopping QUnit...`, "change-file-mid-run": `TAP version 13 diff --git a/test/cli/helpers/execute.js b/test/cli/helpers/execute.js index 13784fdc3..6fbdb4145 100644 --- a/test/cli/helpers/execute.js +++ b/test/cli/helpers/execute.js @@ -110,7 +110,7 @@ module.exports.execute = async function execute( command, options = {}, hook ) { result.code = exitCode; const stderr = normalize( String( result.stderr ).trimEnd() ); if ( exitCode !== 0 ) { - reject( new Error( "Error code " + exitCode + "\n" + stderr ) ); + reject( new Error( "Error code " + exitCode + "\n" + ( stderr || result.stdout ) ) ); } else { resolve(); }