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 abdbc28
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/cli/run.js
Original file line number Diff line number Diff line change
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
65 changes: 65 additions & 0 deletions test/cli/cli-watch.js
Original file line number Diff line number Diff line change
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,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
Expand Down
26 changes: 26 additions & 0 deletions test/cli/fixtures/expected/watch-tap-outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/cli/helpers/execute.js
Original file line number Diff line number Diff line change
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 abdbc28

Please sign in to comment.