From 3cecd1d470a692c9cb36fc5dc00ea8abd7eaf6ba Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sat, 16 Mar 2024 15:16:46 +0200 Subject: [PATCH] fix --- packages/testwatch/index.js | 23 ++++++++++--------- .../tests/fixtures/suspend-plugin.js | 20 ++++++++-------- packages/testwatch/tests/index.test.js | 9 +++++--- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/testwatch/index.js b/packages/testwatch/index.js index d0aaded..b5a3e5e 100755 --- a/packages/testwatch/index.js +++ b/packages/testwatch/index.js @@ -41,10 +41,11 @@ process.stdin.setRawMode?.(true); class REPL { #controller = new AbortController(); + #hooks = { shouldRunTestSuite: [], onTestRunComplete: [], - } + }; #filesFilter = process.argv[2] || ''; @@ -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; } @@ -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; } @@ -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()); }); } } @@ -256,7 +257,7 @@ ${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({ @@ -264,19 +265,19 @@ ${Object.entries(this.#currentCommands) 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)) diff --git a/packages/testwatch/tests/fixtures/suspend-plugin.js b/packages/testwatch/tests/fixtures/suspend-plugin.js index e4d44c5..75f7d3b 100644 --- a/packages/testwatch/tests/fixtures/suspend-plugin.js +++ b/packages/testwatch/tests/fixtures/suspend-plugin.js @@ -1,3 +1,5 @@ +'use strict'; + const chalk = require('chalk'); module.exports = class WatchSuspendPlugin { @@ -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.')); } - }) + }); } + // 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.')); } } -} - +}; diff --git a/packages/testwatch/tests/index.test.js b/packages/testwatch/tests/index.test.js index 5016fee..c43bd47 100644 --- a/packages/testwatch/tests/index.test.js +++ b/packages/testwatch/tests/index.test.js @@ -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 @@ -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(); } }); @@ -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`, ]); });