Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Add watching for .json, .cjs, .mjs, and .ts #1676

Merged
merged 4 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const requireQUnit = require( "./require-qunit" );
const utils = require( "./utils" );
const { findReporter } = require( "./find-reporter" );

const RESTART_DEBOUNCE_LENGTH = 200;
const DEBOUNCE_WATCH_LENGTH = 60;
const DEBOUNCE_RESTART_LENGTH = 200 - DEBOUNCE_WATCH_LENGTH;

const changedPendingPurge = [];

Expand Down Expand Up @@ -158,7 +159,7 @@ run.restart = function( args ) {
}

run.abort( () => run.apply( null, args ) );
}, RESTART_DEBOUNCE_LENGTH );
}, DEBOUNCE_RESTART_LENGTH );
};

run.abort = function( callback ) {
Expand All @@ -179,18 +180,40 @@ run.abort = function( callback ) {
}
};

run.watch = function watch() {
run.watch = function watch( _, options ) {
const watch = require( "node-watch" );
const args = Array.prototype.slice.call( arguments );
const baseDir = process.cwd();

QUnit = requireQUnit();
global.QUnit = QUnit;
options.requires.forEach( requireFromCWD );

// Include TypeScript when in use (automatically via require.extensions),
// https://github.com/qunitjs/qunit/issues/1669.
//
// Include ".json" (part of require.extensions) 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).
//
// Include ".cjs" and ".mjs", which Node.js doesn't expose via require.extensions by default.
//
// eslint-disable-next-line node/no-deprecated-api
const includeExts = Object.keys( require.extensions ).concat( [ ".cjs", ".mjs" ] );
const ignoreDirs = [ ".git", "node_modules" ];

const watcher = watch( baseDir, {
persistent: true,
recursive: true,
delay: 0,
filter: ( fullpath ) => {
return !/\/node_modules\//.test( fullpath ) &&
/\.js$/.test( fullpath );

// Bare minimum delay, we have another debounce in run.restart().
delay: DEBOUNCE_WATCH_LENGTH,
filter: ( fullpath, skip ) => {
if ( /\/node_modules\//.test( fullpath ) ||
ignoreDirs.includes( path.basename( fullpath ) )
) {
return skip;
}
return includeExts.includes( path.extname( fullpath ) );
}
}, ( event, fullpath ) => {
console.log( `File ${event}: ${path.relative( baseDir, fullpath )}` );
Expand Down
90 changes: 90 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,92 @@ 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" ] );
} );

// Skip in coverage mode since NYC adds non-default extensions
QUnit[ process.env.NYC_PROCESS_ID ? "skip" : "test" ]( "TypeScript file extension", async assert => {
fixturify.writeSync( fixturePath, {
"register.js": "require.extensions['.ts'] = function() {};",
"tests": {
"setup.js": "QUnit.on('runEnd', function() { process.send('runEnd'); });",
"foo.js": "QUnit.test('foo', function(assert) { assert.true(true); });"
}
} );

const command = [ "qunit", "--watch", "--require", "./watching/register", "watching/tests" ];
const result = await executeIpc(
command,
execution => {
execution.once( "message", () => {
fixturify.writeSync( fixturePath, {
"x.js": "-",
"x.ts": "-",
"tests": {
"foo.js": "QUnit.test('foo2', function(assert) { assert.true(true); });",
"setup.js": "QUnit.on('runEnd', function() { process.send('runEnd2'); });"
}
} );

execution.once( "message", () => {
kill( execution );
} );
} );
}
);

assert.equal( result.code, 0 );
assert.equal( result.stderr, "" );
assert.equal( result.stdout, expectedWatchOutput[ "file-extension-ts" ] );
} );

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
44 changes: 44 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,50 @@ 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...`,

"file-extension-ts": `TAP version 13
ok 1 foo
1..1
# pass 1
# skip 0
# todo 0
# fail 0
File update: watching/x.js
File update: watching/x.ts
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
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