Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying a line number when filtering tests #5445

Closed
4 tasks done
sheremet-va opened this issue Mar 28, 2024 · 3 comments · Fixed by #6411 or #7048
Closed
4 tasks done

Support specifying a line number when filtering tests #5445

sheremet-va opened this issue Mar 28, 2024 · 3 comments · Fixed by #6411 or #7048
Labels
p2-nice-to-have Not breaking anything but nice to have (priority) pr welcome

Comments

@sheremet-va
Copy link
Member

Clear and concise description of the problem

Right now it's quite cumbersome to filter out tests when using a Vitest CLI. You need to know the name and the file:

vitest test/basic.test.ts -t "some-name"

It fails if there are multiple tests that have the same pattern, so you are forced to use the full name manually:

vitest test/basic.test.ts -t "test full some-name"

Suggested solution

Vitest 1.4.0 introduced includeTaskLocation option that adds a location property to every test. With this information, we can filter tests by the line number:

vitest test/basic.test.ts:20

Alternative

No response

Additional context

I am not sure what should happen when the line doesn't exist or there is no test at that line. Currently, we only know the location of a test, and we don't know when the body of a test ends, so it's tricky to implement a range support.

I think we should:

  • Only run a test if the line is exactly equal to a location line
  • If there is no test at that location, throw an error
  • If range is used, throw an error (test/basic.test.ts:20-30)

Validations

@LostKobrakai
Copy link

I am not sure what should happen when the line doesn't exist or there is no test at that line. Currently, we only know the location of a test, and we don't know when the body of a test ends, so it's tricky to implement a range support.

In elixir it'll run the test, which starts the closest above the supplied line. That's less brittle in the context of line changes (e.g. removing an include at the top).

@mzhubail
Copy link
Contributor

mzhubail commented Aug 25, 2024

I'm interested in picking this up.

I've looked around in the codebase. What I have in mind is to add the filtering in packages/vitest/src/node/workspace.ts; and to use getTests() from packages/runner/src/utils/tasks.ts to access the tests in each file and its position.

It might take a bit of time till I bring about a PR. Just wanted to chime in and see if I'm on the right track, and if anyone has an notes or any pointers. 👀

Nevermind, I was way off 🥲

@sheremet-va
Copy link
Member Author

The implementation in #6411 only supports tests, it would be nice to also support filtering suites:

describe('suite', () => {
  test('nested 1')
  test('nested 2')
})
vitest example.test.ts:1

@sheremet-va sheremet-va reopened this Dec 6, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Dec 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-nice-to-have Not breaking anything but nice to have (priority) pr welcome
Projects
Archived in project
3 participants