From 596823529cd9a86e5eeada0523825fa215e49efe Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Thu, 15 Jun 2023 18:05:09 +0300 Subject: [PATCH] feat: change default file filter --- packages/testwatch/index.js | 4 ++-- .../tests/fixtures/{j.test.js => j.spec.js} | 0 packages/testwatch/tests/fixtures/not-test.js | 6 ++++++ packages/testwatch/tests/index.test.js | 20 ++++++++++++++----- 4 files changed, 23 insertions(+), 7 deletions(-) rename packages/testwatch/tests/fixtures/{j.test.js => j.spec.js} (100%) create mode 100644 packages/testwatch/tests/fixtures/not-test.js diff --git a/packages/testwatch/index.js b/packages/testwatch/index.js index ac6f328..4f67683 100755 --- a/packages/testwatch/index.js +++ b/packages/testwatch/index.js @@ -39,7 +39,7 @@ process.stdin.setRawMode?.(true); class REPL { #controller = new AbortController(); - #filesFilter = ''; + #filesFilter = process.argv[2] || ''; #testsFilter = ''; @@ -59,7 +59,7 @@ class REPL { async #runTests() { this.#controller.abort(); this.#controller = new AbortController(); - const filter = this.#filesFilter ? `**/${this.#filesFilter}*.*` : '**/*.test.js'; + const filter = this.#filesFilter ? `**/${this.#filesFilter}*.*` : '**/?(*.)+(spec|test).[jt]s'; const files = await glob(filter, { ignore: 'node_modules/**' }); if (!files.length) { diff --git a/packages/testwatch/tests/fixtures/j.test.js b/packages/testwatch/tests/fixtures/j.spec.js similarity index 100% rename from packages/testwatch/tests/fixtures/j.test.js rename to packages/testwatch/tests/fixtures/j.spec.js diff --git a/packages/testwatch/tests/fixtures/not-test.js b/packages/testwatch/tests/fixtures/not-test.js new file mode 100644 index 0000000..4d65644 --- /dev/null +++ b/packages/testwatch/tests/fixtures/not-test.js @@ -0,0 +1,6 @@ +const { test } = require('node:test'); +const assert = require('node:assert'); + +test('should not run', () => { + assert.strictEqual(1 + 2, 3); +}); diff --git a/packages/testwatch/tests/index.test.js b/packages/testwatch/tests/index.test.js index 130ea9f..310fac8 100644 --- a/packages/testwatch/tests/index.test.js +++ b/packages/testwatch/tests/index.test.js @@ -35,11 +35,11 @@ Filter Test pattern › `; const filterFilesPrompt = filterTestsPrompt.replace('test', 'file').replace('Test', 'File'); -async function spawnInteractive(commandSequence = 'q') { +async function spawnInteractive(commandSequence = 'q', args = []) { let stderr = ''; let stdout = ''; - const child = spawn(process.execPath, ['../../index.js'], { - env: { }, cwd: path.resolve(__dirname, 'fixtures'), + const child = spawn(process.execPath, ['../../index.js', ...args], { + env: {}, cwd: path.resolve(__dirname, 'fixtures'), }); child.stdin.setEncoding('utf8'); let writing = false; @@ -62,7 +62,7 @@ async function spawnInteractive(commandSequence = 'q') { child.stdout.setEncoding('utf8'); child.stdout.on('data', (data) => { stdout += data; - if (stdout.includes(mainMenu)) { + if (stdout.includes(mainMenu) || stdout.includes(mainMenuWithFilters)) { writeInput(); } }); @@ -103,7 +103,7 @@ describe('testwatch', { concurrency: true, skip: !isSupported ? 'unsupported nod }); it('should exit on sigkill', async () => { const child = spawn(process.execPath, ['../../index.js'], { - env: { }, cwd: path.resolve(__dirname, 'fixtures'), + env: {}, cwd: path.resolve(__dirname, 'fixtures'), }); let stderr = ''; let stdout = ''; @@ -156,6 +156,16 @@ describe('testwatch', { concurrency: true, skip: !isSupported ? 'unsupported nod }); describe('files filter', () => { + it('should set first argument as file filter', async () => { + const { outputs, stderr } = await spawnInteractive('q', ['ind']); + const activeFilters = '\nActive Filters: file name **/ind*.*\n'; + assert.strictEqual(stderr, ''); + assert.deepStrictEqual(outputs, [ + '', + `${testsRun[1]}\n${activeFilters}${mainMenuWithFilters}\n`, + ]); + }); + it('should filter files on "p"', async () => { const { outputs, stderr } = await spawnInteractive(['p', 'index', '\r', 'w', 'q'].join('')); const activeFilters = '\nActive Filters: file name **/index*.*\n';