Skip to content

Commit

Permalink
Start with the test cases
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
mzhubail committed Oct 24, 2024
1 parent ad5a937 commit 67d0b1b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.',
Expand Down
10 changes: 10 additions & 0 deletions test/cli/fixtures/list/no-task-location.config.ts
Original file line number Diff line number Diff line change
@@ -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,
},
})

87 changes: 87 additions & 0 deletions test/cli/test/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'), '<root>')
}
Expand Down

0 comments on commit 67d0b1b

Please sign in to comment.