From 67d0b1bde7a68edd7eb8a9d1e871b54f4206e490 Mon Sep 17 00:00:00 2001 From: mzhubail Date: Wed, 23 Oct 2024 16:46:39 +0300 Subject: [PATCH] Start with the test cases - Changed resolve code in `globTestSpecs`. Rationale is to better match what's happening in `filterFiles` of `node/workspace.ts`. - Attempted to test in case `includeTaskLocation` is disabled. But it seems to me that the code for throwing the error is not reached when the test case is executed. idk what to do about this. - Providing ranges is not tested yet. Code needs to be changed. --- packages/vitest/src/node/cli/cli-api.ts | 2 +- packages/vitest/src/node/core.ts | 5 +- .../fixtures/list/no-task-location.config.ts | 10 +++ test/cli/test/list.test.ts | 87 +++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 test/cli/fixtures/list/no-task-location.config.ts diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 21a4d946f855..ca42a904fd30 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -306,7 +306,7 @@ export function parseFilter(f: string) { } } else if (lineNumber.includes('-')) { - throw new Error('Range line numbers are not allowed') + throw new Error(`Range line numbers are not allowed: ${f}`) } else { return { filename: f } diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 642816fb959b..874adb59af16 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -1095,9 +1095,10 @@ export class Vitest { } public async globTestSpecs(filters: string[] = []) { + const dir = this.config.dir || this.config.root const parsedFilters = filters.map(f => parseFilter(f)) const testLocations = groupFilters(parsedFilters.map( - f => ({ ...f, filename: resolve(f.filename) }), + f => ({ ...f, filename: resolve(dir, f.filename) }), )) // Key is file and val sepcifies whether we have matched this file with testLocation @@ -1130,7 +1131,7 @@ export class Vitest { Object.entries(testLocations).forEach(([filepath, loc]) => { if (loc.length !== 0 && !testLocHasMatch[filepath]) { - const rel = relative(this.config.dir || this.config.root, filepath) + const rel = relative(dir, filepath) this.logger.printError(new Error(`Couldn\'t find file "${rel}".\n` + 'Note when specifying the test location you have to specify the full test filename.', diff --git a/test/cli/fixtures/list/no-task-location.config.ts b/test/cli/fixtures/list/no-task-location.config.ts new file mode 100644 index 000000000000..6ef28d4e2315 --- /dev/null +++ b/test/cli/fixtures/list/no-task-location.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + include: ['basic.test.ts', 'math.test.ts'], + name: 'no task location', + includeTaskLocation: false, + }, +}) + diff --git a/test/cli/test/list.test.ts b/test/cli/test/list.test.ts index ad3c8a03267b..8559f356908b 100644 --- a/test/cli/test/list.test.ts +++ b/test/cli/test/list.test.ts @@ -231,6 +231,93 @@ test('ignores watch flag', async () => { `) }) +test('file not found mentions strict matching for location filters', async () => { + const { stdout, stderr } = await runVitestCli( + 'list', + '-r=./fixtures/list', + 'a/file/that/doesnt/exit:10', + ) + + expect(stderr).toMatchSnapshot() + expect(stdout).toEqual('') +}) + +test('location filter finds test at correct line number', async () => { + const { stdout, stderr } = await runVitestCli( + 'list', + '-r=./fixtures/list', + '--config=custom.config.ts', + 'basic.test.ts:5', + ) + + expect(stdout).toMatchInlineSnapshot(` + "[custom] basic.test.ts > basic suite > inner suite > some test + " + `) + expect(stderr).toEqual('') +}) + +test('location filter reports not found test', async () => { + const { stdout, stderr } = await runVitestCli( + 'list', + '-r=./fixtures/list', + '--config=custom.config.ts', + 'basic.test.ts:99', + ) + + expect(stdout).toEqual('') + expect(stderr).toMatchInlineSnapshot(` + "Error: No test found in basic.test.ts in line 99 + " + `) +}) + +test('location filter reports multiple not found tests', async () => { + const { stdout, stderr } = await runVitestCli( + 'list', + '-r=./fixtures/list', + '--config=custom.config.ts', + 'basic.test.ts:5', + 'basic.test.ts:12', + 'basic.test.ts:99', + ) + + expect(stdout).toEqual('') + expect(stderr).toMatchInlineSnapshot(` + "Error: No test found in basic.test.ts in lines 12, 99 + " + `) +}) + +// Will do after feedback +// test('throw error when range number is provided', async () => { +// const { stdout, stderr } = await runVitestCli( +// 'list', +// '-r=./fixtures/list', +// 'a/file/that/doesnt/exit:10-15' +// ) +// +// // Just to see output +// expect(stdout + '\n' + stderr).toEqual('') +// +// // expect(stderr).toMatchSnapshot() +// // expect(stdout).toEqual('') +// }) +// + +// DOESN'T WORK +// test('erorr if location filter provided without enabling includeTaskLocation', async () => { +// const { stdout, stderr } = await runVitestCli( +// 'list', +// '-r=./fixtures/list', +// '--config=no-task-location.config.ts', +// 'basic.test.ts:5' +// ) +// +// // Just to see output +// expect(stdout + '\n' + stderr).toEqual('') +// }) + function relative(stdout: string) { return stdout.replace(new RegExp(slash(process.cwd()), 'gi'), '') }