From abdbc28a85974076ef28083c02f943eb439dd79b 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 | 65 +++++++++++++++++++ .../fixtures/expected/watch-tap-outputs.js | 26 ++++++++ test/cli/helpers/execute.js | 2 +- 4 files changed, 97 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..aa709153e 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,67 @@ QUnit.module( "CLI Watch", function( hooks ) { assert.equal( result.stdout, expectedWatchOutput[ "remove-file" ] ); } ); + QUnit.test( "consider 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.md": "-", + "x.mjs": "-", + "x.ts": "-", + "x.txt": "-", + + "node_modules": { + "x": { + "y.cjs": "-", + "y.js": "-", + "y.json": "-", + "y.md": "-", + "y.mjs": "-", + "y.ts": "-", + "y.txt": "-" + } + }, + + "y": { + "z.cjs": "-", + "z.js": "-", + "z.json": "-", + "z.md": "-", + "z.mjs": "-", + "z.ts": "-", + "z.txt": "-" + }, + + "tests": { + "foo.js": "QUnit.test('foo2', function(assert) { assert.true(true); });" + } + } ); + + execution.once( "message", () => { + 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..25d53d901 100644 --- a/test/cli/fixtures/expected/watch-tap-outputs.js +++ b/test/cli/fixtures/expected/watch-tap-outputs.js @@ -62,6 +62,32 @@ 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/y/z.cjs +File update: watching/y/z.js +File update: watching/y/z.json +File update: watching/y/z.mjs +File update: watching/tests/foo.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(); }