-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat(cli): Support specifying a line number when filtering tests #6411
base: main
Are you sure you want to change the base?
feat(cli): Support specifying a line number when filtering tests #6411
Conversation
It attempts to parse each filter with the format `<filename>:<line_number>`, while keeping mind that some files might have `:` in their filename. Throw an error when line range is provided. Note that there is an edge case when a file has `:` and `-` in its filename.
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
Vitest has specs that describe the test behaviour: vitest/packages/vitest/src/node/core.ts Line 1083 in 83638eb
I would prefer if we passed down locations there to keep the scope clean and avoid polluting the config. |
As far as I can tell, I went with this approach as it is more similar to how |
We don't need to collect tests for this to work, spec defines what tests to run. This is the perfect place to tell Vitest to limit what test functions to run.
I know. This doesn't really explain why it should follow the same implementation. You can keep the filtering in the test like it is, but change how the values are passed down from the main process. |
I might be missing something. What kind of information do I have access to in a spec? How would I access the line numbers of a test? I see |
Spec is a specification, it doesn't have any information about the test or a file. It tells what file to run and how to run it.
Maybe you can populate the provided context somehow or inject the config value, that's up to you, but the processing should happen there. |
Do you mean that the issue at hand is not filtering inside
Is the issue that we should provide each worker with only the values that are relevant values to its files (unlike |
Yes. The
Yes, that's the idea. |
Yeah, that makes sense. I'll take a shot at it. |
In `globTestSpecs`, the filters will be matched against the files, and if there is a ___location in the filter, it will be included with its file, in a `FileSpec: { path, testLocations? }`. I thought it would be a good idea to include the files with the ___location in the same object, instead of having them in their own attribute in the config and then matching based on the path or something. But I ended up having to modify types everywhere to get this to work. I also had to modify `groupFilesByEnv` to include the `testLocations` in its output, and also modify pools to pass the test locations to `runFiles`. Much of the code in this commit is hacked together, but I had to get something that somewhat works, sometimes, just to see if I'm on the right track.
I gave it a try. See 76bd921. Previous code will be removed of course. In I thought it would be a good idea to include the files with the ___location in the same object, instead of having them in their own attribute in the config and then matching based on the path or something. But I ended up having to modify types everywhere to get this to work. I had to modify Much of the code in this commit is hacked together, but I had to get something that somewhat works, sometimes, just to see if I'm on the right track. Would appreciate inputs on this before I commit to making more changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The general direction seems fine to me, but there are some changes that we cannot make because it would be a breaking change. Unless you want to have this merged in a year,
packages/vitest/src/node/core.ts
Outdated
@@ -374,7 +377,7 @@ export class Vitest { | |||
} | |||
} | |||
|
|||
async start(filters?: string[]) { | |||
async start(filters?: Filter[]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot change the type of this because it would be a big breaking change
packages/vitest/src/node/core.ts
Outdated
@@ -1080,18 +1083,22 @@ export class Vitest { | |||
return this.globTestSpecs().then(specs => specs.map(spec => spec.moduleId)) | |||
} | |||
|
|||
public async globTestSpecs(filters: string[] = []) { | |||
public async globTestSpecs(filters: Filter[] = []) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot change the type of this because it would be a big breaking change
packages/vitest/src/node/logger.ts
Outdated
@@ -141,7 +141,8 @@ export class Logger { | |||
return code | |||
} | |||
|
|||
printNoTestFound(filters?: string[]) { | |||
printNoTestFound(filters_?: Filter[]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot change the type of this because it would be a big breaking change
public createSpec( | ||
moduleId: string, | ||
pool: string, | ||
testLocations: number[] | undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testLocations: number[] | undefined, | |
testLocations?: number[] | undefined, |
packages/runner/src/collect.ts
Outdated
@@ -20,14 +20,15 @@ import { runSetupFiles } from './setup' | |||
const now = Date.now | |||
|
|||
export async function collectTests( | |||
paths: string[], | |||
specs: FileSpec[], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot change the type of this because it would be a big breaking change
One thing I shouldn't be doing is parsing the filters in vitest/packages/vitest/src/node/cli/cac.ts Line 265 in 76bd921
If I were to do the parsing inside How about Instead of using an incompatible type like
This has been labelled a "nice to have", but I'm not sure I'll be here in a year :) |
I guess that would work but I don't understand why you need this if you will still parse it inside, right? |
I Forgot to add the declaration of export interface FileSpec {
file: string
testLocations: number[] | undefined
} The intention of this is to pass both the filepath and the |
- Move the filter parsing inside `globTestSpecs` and revert related type changes - Loosened up the type in `packages/runner/src/collect.ts:collectTests` - Fix type in `createSpec` - Modify `groupFilesByEnv` return structure - Modified `FileSpec`. Name `filepath` is better than just `file` - Functional matching by ___location in `interpretTaskModes`
Cleaned up a bit
I hope I'm in the right direction. Thank you for you time @sheremet-va! |
Description
closes #5445.
Added a function to parse filters into
{ filename, lineNumber? }
. The function attempts to parse each filter with the format<filename>:<line_number>
. If succssful,lineNumber
is set, and we have a "___location filter", otherwise line number is not set and the filter is treated as a regular filter.Added
locationFilters
toUserConfig
and toVitestRunnerConfig
.Note that the we only store filters that have
lineNumber
, in order to use them ininterpretTaskModes
.The parsing is done in
cac.ts:start
because that's the place where we have access to both the options and filters.I think I know where to do stuff for the most part, but I need help with some stuff:
I'm not sure how to report errors in case the user included a range instead of a line number.
I'm not happy with my use of
Required
inRequired<Filter[]>
, but I'm not sure what would be a better way to do it.I think we should match filename strictly when ___location filter is provided, but I'm not sure how to. Should I be using something similar to
WorkspaceProject.filterFiles()
?As for printing an error when the user provides a wrong line number, it's a bit tough withinterpretTaskModes
being a recursive a function. What I have in mind is to keep track of all the filters and wether it was matched or not, and print out errors once we've been through all the tests.The part I'm having an issue with is where to keep track of that. That would involve major changes to howinterpretTaskModes
is written. I could move theSee 36c88e9
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yaml
unless you introduce a new test example.Tests
pnpm test:ci
.Documentation
pnpm run docs
command.Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.