Skip to content

Commit

Permalink
CLI: Add support for watching .json, .cjs, and .mjs files
Browse files Browse the repository at this point in the history
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 #1669.
  • Loading branch information
Krinkle committed Feb 15, 2022
1 parent a8fdac2 commit 4745125
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/cli/run.js
Expand Up @@ -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 )}` );
Expand Down
54 changes: 54 additions & 0 deletions test/cli/cli-watch.js
Expand Up @@ -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, {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/cli/fixtures/expected/watch-tap-outputs.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/cli/helpers/execute.js
Expand Up @@ -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();
}
Expand Down

0 comments on commit 4745125

Please sign in to comment.