Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mikicho committed Mar 16, 2024
1 parent 58419e1 commit 3cecd1d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
23 changes: 12 additions & 11 deletions packages/testwatch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ process.stdin.setRawMode?.(true);

class REPL {
#controller = new AbortController();

#hooks = {
shouldRunTestSuite: [],
onTestRunComplete: [],
}
};

#filesFilter = process.argv[2] || '';

Expand All @@ -67,10 +68,10 @@ class REPL {
this.#clear();
this.#controller.abort();
this.#controller = new AbortController();
if (this.#hooks.shouldRunTestSuite.some(fn => !fn())) {
if (this.#hooks.shouldRunTestSuite.some((fn) => !fn())) {
this.#clear();
this.#emitter.emit('drained');
this.#hooks.onTestRunComplete.forEach(fn => fn())
this.#hooks.onTestRunComplete.forEach((fn) => fn());
return;
}

Expand All @@ -80,7 +81,7 @@ class REPL {
if (!files.length) {
process.stdout.write(chalk.red(`\nNo files found for pattern ${filter}\n`));
this.#emitter.emit('drained');
this.#hooks.onTestRunComplete.forEach(fn => fn())
this.#hooks.onTestRunComplete.forEach((fn) => fn());
return;
}

Expand All @@ -105,7 +106,7 @@ class REPL {
setImmediate(() => {
this.#emitter.emit('drained');
drained = true;
this.#hooks.onTestRunComplete.forEach(fn => fn())
this.#hooks.onTestRunComplete.forEach((fn) => fn());
});
}
}
Expand Down Expand Up @@ -256,27 +257,27 @@ ${Object.entries(this.#currentCommands)

registerPlugin(plugin) {
// TODO: should we be compatible (as much as possible) to Jest API for easy migration?
const usageInfo = plugin.getUsageInfo()
const usageInfo = plugin.getUsageInfo();

// TODO: enable override t,p overridable
this.#currentCommands = Object.freeze({
[usageInfo.key]: {
fn: plugin.run.bind(plugin),
description: usageInfo.description,
},
...this.#currentCommands
})
...this.#currentCommands,
});

plugin.apply({
shouldRunTestSuite: (fn) => this.#hooks.shouldRunTestSuite.push(fn),
onTestRunComplete: (fn) => this.#hooks.onTestRunComplete.push(fn),
})
});
}
}

const repl = new REPL()
const repl = new REPL();
// TODO: only for testing/development. remove this after we have config.
repl.registerPlugin(new WatchSuspendPlugin())
repl.registerPlugin(new WatchSuspendPlugin());

repl.run()
.then(() => process.exit(0))
Expand Down
20 changes: 10 additions & 10 deletions packages/testwatch/tests/fixtures/suspend-plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const chalk = require('chalk');

module.exports = class WatchSuspendPlugin {
Expand All @@ -6,28 +8,26 @@ module.exports = class WatchSuspendPlugin {
}

apply(hooks) {
hooks.shouldRunTestSuite(() => {
return !this.suspend
})
hooks.shouldRunTestSuite(() => !this.suspend);
hooks.onTestRunComplete(() => {
if (this.suspend) {
console.info(chalk.bold(`\nTest is suspended.`))
console.info(chalk.bold('\nTest is suspended.'));

Check warning on line 14 in packages/testwatch/tests/fixtures/suspend-plugin.js

View workflow job for this annotation

GitHub Actions / tests (v20)

Unexpected console statement

Check warning on line 14 in packages/testwatch/tests/fixtures/suspend-plugin.js

View workflow job for this annotation

GitHub Actions / tests (v18)

Unexpected console statement

Check warning on line 14 in packages/testwatch/tests/fixtures/suspend-plugin.js

View workflow job for this annotation

GitHub Actions / tests (v21)

Unexpected console statement
}
})
});
}

// eslint-disable-next-line class-methods-use-this
getUsageInfo() {
return {
key: 's',
description: 'suspend watch mode',
}
};
}

run() {
this.suspend = !this.suspend
this.suspend = !this.suspend;
if (this.suspend) {
console.info(chalk.bold('\nTest is suspended.'))
console.info(chalk.bold('\nTest is suspended.'));

Check warning on line 30 in packages/testwatch/tests/fixtures/suspend-plugin.js

View workflow job for this annotation

GitHub Actions / tests (v20)

Unexpected console statement

Check warning on line 30 in packages/testwatch/tests/fixtures/suspend-plugin.js

View workflow job for this annotation

GitHub Actions / tests (v18)

Unexpected console statement

Check warning on line 30 in packages/testwatch/tests/fixtures/suspend-plugin.js

View workflow job for this annotation

GitHub Actions / tests (v21)

Unexpected console statement
}
}
}

};
9 changes: 6 additions & 3 deletions packages/testwatch/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ REPL Usage
const mainMenuWithFilters = mainMenu.replace('REPL Usage', `REPL Usage
› Press c to clear the filters.`);
const mainMenuWithPlugin = mainMenu.replace('REPL Usage', 'REPL Usage\n'
+ ' › Press s to suspend watch mode')
+ ' › Press s to suspend watch mode');
const compactMenu = '\nREPL Usage: Press w to show more.';
const filterTestsPrompt = `
Filter Test
Expand Down Expand Up @@ -87,7 +87,8 @@ async function spawnInteractive(commandSequence = 'q', args = []) {
pending.stdout += data;
const s = pending.stdout;
// TODO: can we check only the last line of each possible option?
if (s.includes(mainMenu) || s.includes(mainMenuWithFilters) || s.includes(compactMenu) || s.includes(mainMenuWithPlugin)) {
if (s.includes(mainMenu) || s.includes(mainMenuWithFilters)
|| s.includes(compactMenu) || s.includes(mainMenuWithPlugin)) {
pending.resolve();
}
});
Expand Down Expand Up @@ -299,13 +300,15 @@ describe('testwatch', { concurrency: true, skip: !isSupported ? 'unsupported nod
});
});

describe('Plugins', () => {
describe('Plugins', () => {
it('should suspend the watch mode', async () => {
const { outputs, stderr } = await spawnInteractive(['s', '\r', 'q']);
assert.strictEqual(stderr, '');
assert.deepStrictEqual(outputs, [
'',
'',
`${tests}\n${mainMenuWithPlugin}\nTest is suspended.\n`,
'',
`${compactMenu}\nTest is suspended.\n\n`,
]);
});
Expand Down

0 comments on commit 3cecd1d

Please sign in to comment.