From 8d3ff56eb2d15f7f0cfde257f761fd4bcfe0aae5 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:53:42 +0300 Subject: [PATCH 01/16] feat(cli): add the files only option to the list command (unfuncitonal) --- packages/vitest/src/node/cli/cli-api.ts | 4 ++++ packages/vitest/src/node/cli/cli-config.ts | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index c9e3576fabe2..f0b4fc57fbc0 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -26,6 +26,10 @@ export interface CliOptions extends UserConfig { * Output collected tests as JSON or to a file */ json?: string | boolean + /** + * Output collected test files only + */ + filesOnly?: boolean } /** diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index b9bf3295262e..07dda2060b0c 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -795,6 +795,7 @@ export const cliOptionsConfig: VitestCLIOptions = { outputJson: null, json: null, provide: null, + filesOnly: null, } export const benchCliOptionsConfig: Pick< @@ -813,10 +814,13 @@ export const benchCliOptionsConfig: Pick< export const collectCliOptionsConfig: Pick< VitestCLIOptions, - 'json' + 'json' | 'filesOnly' > = { json: { description: 'Print collected tests as JSON or write to a file (Default: false)', argument: '[true/path]', }, + filesOnly: { + description: 'Print only test files with out the test cases', + } } From a6f9e3c22b6bfb0b06c9bbff258bce8a1099f925 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:20:27 +0300 Subject: [PATCH 02/16] feat(cli): add implementation for listing files only --- packages/vitest/src/node/cli/cli-api.ts | 48 ++++++++++++++++++++----- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index f0b4fc57fbc0..684e61a72abe 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -1,16 +1,16 @@ /* eslint-disable no-console */ +import type { File, Suite, Task } from '@vitest/runner' import { mkdirSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'pathe' import type { UserConfig as ViteUserConfig } from 'vite' -import type { File, Suite, Task } from '@vitest/runner' import { CoverageProviderMap } from '../../integrations/coverage' import type { environments } from '../../integrations/env' -import { createVitest } from '../create' -import { registerConsoleShortcuts } from '../stdin' +import { getNames, getTests } from '../../utils' import type { Vitest, VitestOptions } from '../core' +import { createVitest } from '../create' import { FilesNotFoundError, GitNotFoundError } from '../errors' -import { getNames, getTests } from '../../utils' +import { registerConsoleShortcuts } from '../stdin' import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config' export interface CliOptions extends UserConfig { @@ -185,18 +185,18 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions return processJsonOutput(files, options) } - return formatCollectedAsString(files).forEach(test => console.log(test)) + return formatCollectedAsString(files, options).forEach(test => console.log(test)) } function processJsonOutput(files: File[], options: CliOptions) { if (typeof options.json === 'boolean') { - return console.log(JSON.stringify(formatCollectedAsJSON(files), null, 2)) + return console.log(JSON.stringify(formatCollectedAsJSON(files, options), null, 2)) } if (typeof options.json === 'string') { const jsonPath = resolve(options.root || process.cwd(), options.json) mkdirSync(dirname(jsonPath), { recursive: true }) - writeFileSync(jsonPath, JSON.stringify(formatCollectedAsJSON(files), null, 2)) + writeFileSync(jsonPath, JSON.stringify(formatCollectedAsJSON(files, options), null, 2)) } } @@ -209,7 +209,25 @@ function forEachSuite(tasks: Task[], callback: (suite: Suite) => void) { }) } -export function formatCollectedAsJSON(files: File[]) { +export function formatCollectedAsJSON(files: File[], options: CliOptions) { + + if(options.filesOnly){ + return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { + const result: any = { + name: file.name, + file: file.filepath, + } + if (file.projectName) { + result.projectName = file.projectName + } + if (file.location) { + result.location = file.location + } + return result + }) + .flat() + } + return files.map((file) => { const tests = getTests(file).filter(test => test.mode === 'run' || test.mode === 'only') return tests.map((test) => { @@ -228,7 +246,19 @@ export function formatCollectedAsJSON(files: File[]) { }).flat() } -export function formatCollectedAsString(files: File[]) { +export function formatCollectedAsString(files: File[], options: CliOptions) { + + if(options.filesOnly){ + return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { + const name = file.name; + if (file.projectName) { + return `[${file.projectName}] ${name}` + } + return name + }) + .flat() + } + return files.map((file) => { const tests = getTests(file).filter(test => test.mode === 'run' || test.mode === 'only') return tests.map((test) => { From 51c0577c3bc607fe54ce11b184b6529a41730946 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Sun, 25 Aug 2024 11:41:36 +0300 Subject: [PATCH 03/16] feat(cli): add testing for list --filesOnly --- packages/vitest/src/node/cli/cli-api.ts | 7 +-- test/cli/test/list.test.ts | 73 ++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 684e61a72abe..4c043001f4ac 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -211,7 +211,7 @@ function forEachSuite(tasks: Task[], callback: (suite: Suite) => void) { export function formatCollectedAsJSON(files: File[], options: CliOptions) { - if(options.filesOnly){ + if(typeof options.filesOnly !== undefined && options.filesOnly){ return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { const result: any = { name: file.name, @@ -220,9 +220,6 @@ export function formatCollectedAsJSON(files: File[], options: CliOptions) { if (file.projectName) { result.projectName = file.projectName } - if (file.location) { - result.location = file.location - } return result }) .flat() @@ -248,7 +245,7 @@ export function formatCollectedAsJSON(files: File[], options: CliOptions) { export function formatCollectedAsString(files: File[], options: CliOptions) { - if(options.filesOnly){ + if(typeof options.filesOnly !== undefined && options.filesOnly){ return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { const name = file.name; if (file.projectName) { diff --git a/test/cli/test/list.test.ts b/test/cli/test/list.test.ts index 6fc3597cb2c2..7fc8b113c3de 100644 --- a/test/cli/test/list.test.ts +++ b/test/cli/test/list.test.ts @@ -6,7 +6,7 @@ test.each([ ['--pool=threads'], ['--pool=forks'], ['--pool=vmForks'], - ['--browser.enabled'], + // ['--browser.enabled'], ])('correctly outputs all tests with args: "%s"', async (...args) => { const { stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list', ...args) expect(stdout).toMatchSnapshot() @@ -58,6 +58,24 @@ test('correctly outputs json', async () => { expect(exitCode).toBe(0) }) +test('correctly outputs files only json', async () => { + const { stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list', '--json', '--filesOnly') + expect(relative(stdout)).toMatchInlineSnapshot(` + "[ + { + "name": "basic.test.ts", + "file": "/fixtures/list/basic.test.ts" + }, + { + "name": "math.test.ts", + "file": "/fixtures/list/math.test.ts" + } + ] + " + `) + expect(exitCode).toBe(0) +}) + test('correctly saves json', async () => { const { stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list', '--json=./list.json') onTestFinished(() => { @@ -96,6 +114,28 @@ test('correctly saves json', async () => { expect(exitCode).toBe(0) }) +test('correctly saves files only json', async () => { + const { stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list', '--json=./list.json', '--filesOnly') + onTestFinished(() => { + rmSync('./fixtures/list/list.json') + }) + const json = readFileSync('./fixtures/list/list.json', 'utf-8') + expect(stdout).toBe('') + expect(relative(json)).toMatchInlineSnapshot(` + "[ + { + "name": "basic.test.ts", + "file": "/fixtures/list/basic.test.ts" + }, + { + "name": "math.test.ts", + "file": "/fixtures/list/math.test.ts" + } + ]" + `) + expect(exitCode).toBe(0) +}) + test('correctly filters by file', async () => { const { stdout, exitCode } = await runVitestCli('list', 'math.test.ts', '-r=./fixtures/list') expect(stdout).toMatchInlineSnapshot(` @@ -106,6 +146,15 @@ test('correctly filters by file', async () => { expect(exitCode).toBe(0) }) +test('correctly filters by file when using --filesOnly', async () => { + const { stdout, exitCode } = await runVitestCli('list', 'math.test.ts', '-r=./fixtures/list', '--filesOnly') + expect(stdout).toMatchInlineSnapshot(` + "math.test.ts + " + `) + expect(exitCode).toBe(0) +}) + test('correctly prints project name in basic report', async () => { const { stdout } = await runVitestCli('list', 'math.test.ts', '-r=./fixtures/list', '--config=./custom.config.ts') expect(stdout).toMatchInlineSnapshot(` @@ -115,6 +164,14 @@ test('correctly prints project name in basic report', async () => { `) }) +test('correctly prints project name in basic report when using --filesOnly', async () => { + const { stdout } = await runVitestCli('list', 'math.test.ts', '-r=./fixtures/list', '--config=./custom.config.ts', '--filesOnly') + expect(stdout).toMatchInlineSnapshot(` + "[custom] math.test.ts + " + `) +}) + test('correctly prints project name and locations in json report', async () => { const { stdout } = await runVitestCli('list', 'math.test.ts', '-r=./fixtures/list', '--json', '--config=./custom.config.ts') expect(relative(stdout)).toMatchInlineSnapshot(` @@ -142,6 +199,20 @@ test('correctly prints project name and locations in json report', async () => { `) }) +test('correctly prints project name in json report when using --filesOnly', async () => { + const { stdout } = await runVitestCli('list', 'math.test.ts', '-r=./fixtures/list', '--json', '--config=./custom.config.ts', '--filesOnly') + expect(relative(stdout)).toMatchInlineSnapshot(` + "[ + { + "name": "math.test.ts", + "file": "/fixtures/list/math.test.ts", + "projectName": "custom" + } + ] + " + `) +}) + test('correctly filters by test name', async () => { const { stdout } = await runVitestCli('list', '-t=inner', '-r=./fixtures/list') expect(stdout).toMatchInlineSnapshot(` From 8f6e1bed384c38d3bf052d923332a9eb0b273749 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Sun, 25 Aug 2024 11:42:14 +0300 Subject: [PATCH 04/16] feat(cli): add documentation for list --filesOnly --- docs/guide/cli.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 9041fc80e828..ba46c7b1270f 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -86,6 +86,17 @@ vitest list filename.spec.ts -t="some-test" --json=./file.json If `--json` flag doesn't receive a value, it will output the JSON into stdout. +You also can pass down `--filesOnly` flag to print the test files only: + +```bash +vitest list --filesOnly +``` + +```txt +tests/test1.test.ts +tests/test2.test.ts +``` + ## Options ::: tip From 55eb3cd1cc9b3845cb092dd5ff1711abecc0ca4c Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:07:08 +0300 Subject: [PATCH 05/16] feat(cli): lint the files --- packages/vitest/src/node/cli/cli-api.ts | 26 ++++++++++------------ packages/vitest/src/node/cli/cli-config.ts | 2 +- test/cli/test/list.test.ts | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 4c043001f4ac..d27a2321abf8 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ -import type { File, Suite, Task } from '@vitest/runner' import { mkdirSync, writeFileSync } from 'node:fs' +import type { File, Suite, Task } from '@vitest/runner' import { dirname, resolve } from 'pathe' import type { UserConfig as ViteUserConfig } from 'vite' import { CoverageProviderMap } from '../../integrations/coverage' @@ -210,8 +210,7 @@ function forEachSuite(tasks: Task[], callback: (suite: Suite) => void) { } export function formatCollectedAsJSON(files: File[], options: CliOptions) { - - if(typeof options.filesOnly !== undefined && options.filesOnly){ + if (typeof options.filesOnly !== 'undefined' && options.filesOnly) { return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { const result: any = { name: file.name, @@ -221,8 +220,8 @@ export function formatCollectedAsJSON(files: File[], options: CliOptions) { result.projectName = file.projectName } return result - }) - .flat() + }) + .flat() } return files.map((file) => { @@ -244,16 +243,15 @@ export function formatCollectedAsJSON(files: File[], options: CliOptions) { } export function formatCollectedAsString(files: File[], options: CliOptions) { - - if(typeof options.filesOnly !== undefined && options.filesOnly){ + if (typeof options.filesOnly !== 'undefined' && options.filesOnly) { return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { - const name = file.name; - if (file.projectName) { - return `[${file.projectName}] ${name}` - } - return name - }) - .flat() + const name = file.name + if (file.projectName) { + return `[${file.projectName}] ${name}` + } + return name + }) + .flat() } return files.map((file) => { diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index 07dda2060b0c..05b83a555a17 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -822,5 +822,5 @@ export const collectCliOptionsConfig: Pick< }, filesOnly: { description: 'Print only test files with out the test cases', - } + }, } diff --git a/test/cli/test/list.test.ts b/test/cli/test/list.test.ts index 7fc8b113c3de..6f41c9c881a1 100644 --- a/test/cli/test/list.test.ts +++ b/test/cli/test/list.test.ts @@ -6,7 +6,7 @@ test.each([ ['--pool=threads'], ['--pool=forks'], ['--pool=vmForks'], - // ['--browser.enabled'], + ['--browser.enabled'], ])('correctly outputs all tests with args: "%s"', async (...args) => { const { stdout, exitCode } = await runVitestCli('list', '-r=./fixtures/list', ...args) expect(stdout).toMatchSnapshot() From d1738e1ee512567210e9a5d94948b2eb6c652786 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:48:53 +0300 Subject: [PATCH 06/16] chore: add changegs in license --- packages/vitest/LICENSE.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/vitest/LICENSE.md b/packages/vitest/LICENSE.md index d54def96b44e..9f826da7d2d2 100644 --- a/packages/vitest/LICENSE.md +++ b/packages/vitest/LICENSE.md @@ -1235,6 +1235,35 @@ Repository: sindresorhus/p-locate --------------------------------------- +## package-manager-detector +License: MIT +By: Anthony Fu +Repository: git+https://github.com/antfu-collective/package-manager-detector.git + +> MIT License +> +> Copyright (c) 2020-PRESENT Anthony Fu +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in all +> copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +> SOFTWARE. + +--------------------------------------- + ## picomatch License: MIT By: Jon Schlinkert From 85547a9ca3143df4abea27a7b673bb38c0f7f313 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:59:52 +0300 Subject: [PATCH 07/16] Revert "feat(cli): add implementation for listing files only" This reverts commit a6f9e3c22b6bfb0b06c9bbff258bce8a1099f925. --- packages/vitest/src/node/cli/cli-api.ts | 42 ++++++------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index d27a2321abf8..d643377b0597 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -4,13 +4,14 @@ import { mkdirSync, writeFileSync } from 'node:fs' import type { File, Suite, Task } from '@vitest/runner' import { dirname, resolve } from 'pathe' import type { UserConfig as ViteUserConfig } from 'vite' +import type { File, Suite, Task } from '@vitest/runner' import { CoverageProviderMap } from '../../integrations/coverage' import type { environments } from '../../integrations/env' -import { getNames, getTests } from '../../utils' -import type { Vitest, VitestOptions } from '../core' import { createVitest } from '../create' -import { FilesNotFoundError, GitNotFoundError } from '../errors' import { registerConsoleShortcuts } from '../stdin' +import type { Vitest, VitestOptions } from '../core' +import { FilesNotFoundError, GitNotFoundError } from '../errors' +import { getNames, getTests } from '../../utils' import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config' export interface CliOptions extends UserConfig { @@ -185,18 +186,18 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions return processJsonOutput(files, options) } - return formatCollectedAsString(files, options).forEach(test => console.log(test)) + return formatCollectedAsString(files).forEach(test => console.log(test)) } function processJsonOutput(files: File[], options: CliOptions) { if (typeof options.json === 'boolean') { - return console.log(JSON.stringify(formatCollectedAsJSON(files, options), null, 2)) + return console.log(JSON.stringify(formatCollectedAsJSON(files), null, 2)) } if (typeof options.json === 'string') { const jsonPath = resolve(options.root || process.cwd(), options.json) mkdirSync(dirname(jsonPath), { recursive: true }) - writeFileSync(jsonPath, JSON.stringify(formatCollectedAsJSON(files, options), null, 2)) + writeFileSync(jsonPath, JSON.stringify(formatCollectedAsJSON(files), null, 2)) } } @@ -209,21 +210,7 @@ function forEachSuite(tasks: Task[], callback: (suite: Suite) => void) { }) } -export function formatCollectedAsJSON(files: File[], options: CliOptions) { - if (typeof options.filesOnly !== 'undefined' && options.filesOnly) { - return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { - const result: any = { - name: file.name, - file: file.filepath, - } - if (file.projectName) { - result.projectName = file.projectName - } - return result - }) - .flat() - } - +export function formatCollectedAsJSON(files: File[]) { return files.map((file) => { const tests = getTests(file).filter(test => test.mode === 'run' || test.mode === 'only') return tests.map((test) => { @@ -242,18 +229,7 @@ export function formatCollectedAsJSON(files: File[], options: CliOptions) { }).flat() } -export function formatCollectedAsString(files: File[], options: CliOptions) { - if (typeof options.filesOnly !== 'undefined' && options.filesOnly) { - return files.filter(test => test.mode === 'run' || test.mode === 'only').map((file) => { - const name = file.name - if (file.projectName) { - return `[${file.projectName}] ${name}` - } - return name - }) - .flat() - } - +export function formatCollectedAsString(files: File[]) { return files.map((file) => { const tests = getTests(file).filter(test => test.mode === 'run' || test.mode === 'only') return tests.map((test) => { From 2ee41c4ce1d3610466227a0284f15b218dd95bda Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:11:04 +0300 Subject: [PATCH 08/16] feat(cli): Change the approach of collecting files --- packages/vitest/src/node/cli/cac.ts | 25 ++++++++----- packages/vitest/src/node/cli/cli-api.ts | 48 ++++++++++++++++++++++++- packages/vitest/src/node/core.ts | 9 +++++ test/cli/test/list.test.ts | 5 --- 4 files changed, 72 insertions(+), 15 deletions(-) diff --git a/packages/vitest/src/node/cli/cac.ts b/packages/vitest/src/node/cli/cac.ts index e2fcc55c2030..e0babaf7d3e8 100644 --- a/packages/vitest/src/node/cli/cac.ts +++ b/packages/vitest/src/node/cli/cac.ts @@ -300,24 +300,31 @@ async function collect(mode: VitestRunMode, cliFilters: string[], options: CliOp catch {} try { - const { prepareVitest, processCollected } = await import('./cli-api') + const { prepareVitest, processCollected, outputFileList } = await import('./cli-api') const ctx = await prepareVitest(mode, { ...normalizeCliOptions(options), watch: false, run: true, }) + if(!options.filesOnly){ + const { tests, errors } = await ctx.collect(cliFilters.map(normalize)); + + if (errors.length) { + console.error('\nThere were unhandled errors during test collection') + errors.forEach(e => console.error(e)) + console.error('\n\n') + await ctx.close() + return + } - const { tests, errors } = await ctx.collect(cliFilters.map(normalize)) + processCollected(ctx, tests, options) + }else{ - if (errors.length) { - console.error('\nThere were unhandled errors during test collection') - errors.forEach(e => console.error(e)) - console.error('\n\n') - await ctx.close() - return + const files = await ctx.listFiles(cliFilters.map(normalize)); + outputFileList(files, options) + } - processCollected(ctx, tests, options) await ctx.close() } catch (e) { diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index d643377b0597..e2b587f374d6 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -1,7 +1,6 @@ /* eslint-disable no-console */ import { mkdirSync, writeFileSync } from 'node:fs' -import type { File, Suite, Task } from '@vitest/runner' import { dirname, resolve } from 'pathe' import type { UserConfig as ViteUserConfig } from 'vite' import type { File, Suite, Task } from '@vitest/runner' @@ -13,6 +12,7 @@ import type { Vitest, VitestOptions } from '../core' import { FilesNotFoundError, GitNotFoundError } from '../errors' import { getNames, getTests } from '../../utils' import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config' +import { WorkspaceSpec } from '../pool' export interface CliOptions extends UserConfig { /** @@ -189,6 +189,52 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions return formatCollectedAsString(files).forEach(test => console.log(test)) } +export function outputFileList(files: WorkspaceSpec[], options: CliOptions){ + + // const paths = files.map((file) => file.moduleId) + + if (typeof options.json !== 'undefined') { + return outputJsonFileList(files, options) + } + + return formatFilesAsString(files) +} + +function outputJsonFileList(files: WorkspaceSpec[], options: CliOptions){ + if (typeof options.json === 'boolean') { + return console.log(JSON.stringify(formatFilesAsJSON(files), null, 2)) + } + if (typeof options.json === 'string') { + const jsonPath = resolve(options.root || process.cwd(), options.json) + mkdirSync(dirname(jsonPath), { recursive: true }) + writeFileSync(jsonPath, JSON.stringify(formatFilesAsJSON(files), null, 2)) + } +} + +function formatFilesAsJSON(files: WorkspaceSpec[]){ + return files.map((file) => { + const result: any = { + file: file.moduleId, + } + + if (file.project.name) { + result.projectName = file.project.name + } + return result + }) +} + +function formatFilesAsString(files: WorkspaceSpec[]) { + return files.map((file) => { + let name = file.moduleId.substring(String(file[0].runner.root).length+1) + if(file.project.name){ + name = `[${file.project.name}] ${name}` + } + console.log(name) + return name + }) +} + function processJsonOutput(files: File[], options: CliOptions) { if (typeof options.json === 'boolean') { return console.log(JSON.stringify(formatCollectedAsJSON(files), null, 2)) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index ce12df6e1357..2ad164e581a7 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -374,6 +374,15 @@ export class Vitest { } } + async listFiles(filters?: string[]){ + + const files = await this.filterTestsBySource( + await this.globTestFiles(filters), + ) + + return files + } + async start(filters?: string[]) { this._onClose = [] diff --git a/test/cli/test/list.test.ts b/test/cli/test/list.test.ts index 6f41c9c881a1..afcaeaaafeb3 100644 --- a/test/cli/test/list.test.ts +++ b/test/cli/test/list.test.ts @@ -63,11 +63,9 @@ test('correctly outputs files only json', async () => { expect(relative(stdout)).toMatchInlineSnapshot(` "[ { - "name": "basic.test.ts", "file": "/fixtures/list/basic.test.ts" }, { - "name": "math.test.ts", "file": "/fixtures/list/math.test.ts" } ] @@ -124,11 +122,9 @@ test('correctly saves files only json', async () => { expect(relative(json)).toMatchInlineSnapshot(` "[ { - "name": "basic.test.ts", "file": "/fixtures/list/basic.test.ts" }, { - "name": "math.test.ts", "file": "/fixtures/list/math.test.ts" } ]" @@ -204,7 +200,6 @@ test('correctly prints project name in json report when using --filesOnly', asyn expect(relative(stdout)).toMatchInlineSnapshot(` "[ { - "name": "math.test.ts", "file": "/fixtures/list/math.test.ts", "projectName": "custom" } From 27ebf1f4b7adc268e9357ecdc86643094cc45565 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:29:15 +0300 Subject: [PATCH 09/16] feat(cli): linting --- packages/vitest/src/node/cli/cac.ts | 11 +++++----- packages/vitest/src/node/cli/cli-api.ts | 27 ++++++++++++------------- packages/vitest/src/node/core.ts | 5 ++--- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/packages/vitest/src/node/cli/cac.ts b/packages/vitest/src/node/cli/cac.ts index e0babaf7d3e8..4a025df52766 100644 --- a/packages/vitest/src/node/cli/cac.ts +++ b/packages/vitest/src/node/cli/cac.ts @@ -306,8 +306,8 @@ async function collect(mode: VitestRunMode, cliFilters: string[], options: CliOp watch: false, run: true, }) - if(!options.filesOnly){ - const { tests, errors } = await ctx.collect(cliFilters.map(normalize)); + if (!options.filesOnly) { + const { tests, errors } = await ctx.collect(cliFilters.map(normalize)) if (errors.length) { console.error('\nThere were unhandled errors during test collection') @@ -318,11 +318,10 @@ async function collect(mode: VitestRunMode, cliFilters: string[], options: CliOp } processCollected(ctx, tests, options) - }else{ - - const files = await ctx.listFiles(cliFilters.map(normalize)); + } + else { + const files = await ctx.listFiles(cliFilters.map(normalize)) outputFileList(files, options) - } await ctx.close() diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index e2b587f374d6..f731346f8221 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -12,7 +12,7 @@ import type { Vitest, VitestOptions } from '../core' import { FilesNotFoundError, GitNotFoundError } from '../errors' import { getNames, getTests } from '../../utils' import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config' -import { WorkspaceSpec } from '../pool' +import type { WorkspaceSpec } from '../pool' export interface CliOptions extends UserConfig { /** @@ -189,8 +189,7 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions return formatCollectedAsString(files).forEach(test => console.log(test)) } -export function outputFileList(files: WorkspaceSpec[], options: CliOptions){ - +export function outputFileList(files: WorkspaceSpec[], options: CliOptions) { // const paths = files.map((file) => file.moduleId) if (typeof options.json !== 'undefined') { @@ -200,7 +199,7 @@ export function outputFileList(files: WorkspaceSpec[], options: CliOptions){ return formatFilesAsString(files) } -function outputJsonFileList(files: WorkspaceSpec[], options: CliOptions){ +function outputJsonFileList(files: WorkspaceSpec[], options: CliOptions) { if (typeof options.json === 'boolean') { return console.log(JSON.stringify(formatFilesAsJSON(files), null, 2)) } @@ -211,23 +210,23 @@ function outputJsonFileList(files: WorkspaceSpec[], options: CliOptions){ } } -function formatFilesAsJSON(files: WorkspaceSpec[]){ +function formatFilesAsJSON(files: WorkspaceSpec[]) { return files.map((file) => { - const result: any = { - file: file.moduleId, - } + const result: any = { + file: file.moduleId, + } - if (file.project.name) { - result.projectName = file.project.name - } - return result + if (file.project.name) { + result.projectName = file.project.name + } + return result }) } function formatFilesAsString(files: WorkspaceSpec[]) { return files.map((file) => { - let name = file.moduleId.substring(String(file[0].runner.root).length+1) - if(file.project.name){ + let name = file.moduleId.substring(String(file[0].runner.root).length + 1) + if (file.project.name) { name = `[${file.project.name}] ${name}` } console.log(name) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 2ad164e581a7..70ab5dad2d35 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -374,12 +374,11 @@ export class Vitest { } } - async listFiles(filters?: string[]){ - + async listFiles(filters?: string[]) { const files = await this.filterTestsBySource( await this.globTestFiles(filters), ) - + return files } From 2615de9e0997b39c5e0da85e71a4155c30402ff4 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Tue, 27 Aug 2024 09:07:53 +0300 Subject: [PATCH 10/16] move printing outside the format function --- packages/vitest/src/node/cli/cli-api.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index f731346f8221..bb9500273687 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -196,7 +196,7 @@ export function outputFileList(files: WorkspaceSpec[], options: CliOptions) { return outputJsonFileList(files, options) } - return formatFilesAsString(files) + return formatFilesAsString(files).map(file => console.log(file)) } function outputJsonFileList(files: WorkspaceSpec[], options: CliOptions) { @@ -229,7 +229,6 @@ function formatFilesAsString(files: WorkspaceSpec[]) { if (file.project.name) { name = `[${file.project.name}] ${name}` } - console.log(name) return name }) } From 16c8606a548be0ff2710f2d317d40fa7c084a320 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:39:19 +0300 Subject: [PATCH 11/16] Update packages/vitest/src/node/cli/cli-api.ts Co-authored-by: Vladimir --- packages/vitest/src/node/cli/cli-api.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index bb9500273687..d56405a71504 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -190,7 +190,6 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions } export function outputFileList(files: WorkspaceSpec[], options: CliOptions) { - // const paths = files.map((file) => file.moduleId) if (typeof options.json !== 'undefined') { return outputJsonFileList(files, options) From 12bfad146199b092445b3b6ce2e910697e23b4de Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Tue, 27 Aug 2024 13:10:23 +0300 Subject: [PATCH 12/16] lintingg --- packages/vitest/src/node/cli/cli-api.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index d56405a71504..dfc46eccdeeb 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -190,7 +190,6 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions } export function outputFileList(files: WorkspaceSpec[], options: CliOptions) { - if (typeof options.json !== 'undefined') { return outputJsonFileList(files, options) } From beb6bd96e155d20b2eaa4456c95d285ee1c2041e Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 27 Aug 2024 12:22:06 +0200 Subject: [PATCH 13/16] Update packages/vitest/src/node/cli/cli-api.ts --- packages/vitest/src/node/cli/cli-api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index dfc46eccdeeb..38992e1240db 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -223,7 +223,7 @@ function formatFilesAsJSON(files: WorkspaceSpec[]) { function formatFilesAsString(files: WorkspaceSpec[]) { return files.map((file) => { - let name = file.moduleId.substring(String(file[0].runner.root).length + 1) + let name = resolve(options.root || process.cwd(), file.moduleId) if (file.project.name) { name = `[${file.project.name}] ${name}` } From 00445ab39a7ecd8fab73ff37ec80b07691d2033e Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Tue, 27 Aug 2024 13:33:59 +0300 Subject: [PATCH 14/16] Update packages/vitest/src/node/cli/cli-api.ts --- packages/vitest/src/node/cli/cli-api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 38992e1240db..0c5693e36320 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -221,7 +221,7 @@ function formatFilesAsJSON(files: WorkspaceSpec[]) { }) } -function formatFilesAsString(files: WorkspaceSpec[]) { +function formatFilesAsString(files: WorkspaceSpec[], options: CliOptions) { return files.map((file) => { let name = resolve(options.root || process.cwd(), file.moduleId) if (file.project.name) { From 2f5a5d3247866ad0a6a828ef86bf8ebe850db875 Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Tue, 27 Aug 2024 13:34:06 +0300 Subject: [PATCH 15/16] Update packages/vitest/src/node/cli/cli-api.ts --- packages/vitest/src/node/cli/cli-api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 0c5693e36320..5e12ffc989d3 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -194,7 +194,7 @@ export function outputFileList(files: WorkspaceSpec[], options: CliOptions) { return outputJsonFileList(files, options) } - return formatFilesAsString(files).map(file => console.log(file)) + return formatFilesAsString(files, options).map(file => console.log(file)) } function outputJsonFileList(files: WorkspaceSpec[], options: CliOptions) { From 99afa71357dc47ddf7051cc2854a0d17876a0d0d Mon Sep 17 00:00:00 2001 From: Mahmood Alhawaj <119938418+Ma-hawaj@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:37:04 +0300 Subject: [PATCH 16/16] fixing the printed file path --- packages/vitest/src/node/cli/cli-api.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 5e12ffc989d3..486bf385d047 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { mkdirSync, writeFileSync } from 'node:fs' -import { dirname, resolve } from 'pathe' +import { dirname, relative, resolve } from 'pathe' import type { UserConfig as ViteUserConfig } from 'vite' import type { File, Suite, Task } from '@vitest/runner' import { CoverageProviderMap } from '../../integrations/coverage' @@ -223,7 +223,7 @@ function formatFilesAsJSON(files: WorkspaceSpec[]) { function formatFilesAsString(files: WorkspaceSpec[], options: CliOptions) { return files.map((file) => { - let name = resolve(options.root || process.cwd(), file.moduleId) + let name = relative(options.root || process.cwd(), file.moduleId) if (file.project.name) { name = `[${file.project.name}] ${name}` }