-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Simplifying configuration #7185
Comments
I completely agree we should rethink our configuration options, so thank you for collecting all this. Some comments:
I think we should favor regular expressions over globs, as they're more expressive and anything that can be expressed as a glob can be as a regexp. I'd also keep the
I'm not sure we should group configuration options in objects. I've usually found flat options easier to deal with and it aligns better with CLI options (e.g.: you could overwrite the |
The syntax is also way harder, though. I find globs more readable, and easier to write (as you can probably do
As long as we allow an array of globs and apply them in order, it's easy enough to just include an
I agree on this one, at least depending on how yargs could handle deep objects. If you can do |
Also a fan of globs for file pattern matching. As long as you can pass arrays for all matchers, the limitations vs. regexp seem like not a big deal. Another alternative (or addition) that would be great is just allowing any file pattern config to also accept |
Glob arrays + functions seems like a nice compromise of ease-of-use vs power to me. |
Passing a function could definitely work. Would it be inline, or point to some file that exports a function? Both? Edit: might be confusing that a string can be interpreted as a module, so I guess inline is the way to go |
It should be a function because otherwise it'd have to be split in two different configuration options (we can't distinguish a string being a glob or a file containing the function). |
Yes, this seem the most flexible since anyone is always free to just import their function. Another related discussion here is that a some other config options require that something that's really just a function be implemented as an npm package, e.g. I'd prefer just allowing config to accept a function for these; people can always implement it as a module themselves if they want to. But it's a lot more cruft to make a module, link it in |
It's something from when config had to be in package.json. I'm all for saying "use js config". Presets should help keep boilerplate down and if that's an issue for people. We also have to consider people passing stuff from the CLI, but I don't think that too important for stuff that rarely change (like result processors, resolver, etc). |
I suppose you could overload again, and accept a string or an fn, where a string is always treated as an npm package? Specifying a reporter by npm package name on the CLI could be useful, though interestingly that can't be done now afaict ;) |
You can do |
This is absolutely stupendous. Thank you all for your work here! It's long been a point of confusion for me. |
Resuggesting the above linked issue here: Ideally it would be very simple to configure Jest to use the same glob pattern that typescript uses to resolve paths. Initially I assumed it had that type of support, but discovered it's regex only per this so question. If this is supported users can use the same glob pattern in jest that they would use in typescript. I don't know about the rest of you, but I break out into a cold sweat any time RegEx is mentioned. |
As part of this, I'd also like to use |
Would be nice to provide a way to specify reporter options (and possibly other things) from the CLI. See #7845 Edit: Sorry, out of scope 😄 |
From OP:
But yeah, we should figure out a plan. I think as long as we really standardize on how to pass options (I like the |
On globs vs. regexp: I've been repeatedly confused by the patterns in On simplifying configurations: any thoughts on cascading/extending/inheriting hierarchical config files? Here's a StackOverflow post requesting this feature. I think ESLint does a great job at this with cascading configurations. It's been very easy to use in monorepos: have an |
@dandv That also roughly echoes what Babel is moving towards, with one root |
Some random thoughts from a quick chat with @SimenB:
|
Totally agree, configuration should be simpler and it saves us from wasting time to decide right option to use (while they're same :D) e.g. testRegex vs. testMatch in my last code review |
Is there any reason to having both setupFiles and setupFilesAfterEnv? I cannot see any downside of only having setupFilesAfterEnv? |
I made a separate issue for this: #9314 |
Curious, b/c I don't see it here (maybe I'm blind?) but does this include simplifying/clarifying the number of ways to make a mock or mock-like thing (automock, spyOn, jest.mock, __mocks__, requireMock, jest.fn)? |
Hey there! No, this is for https://jestjs.io/docs/en/configuration and https://jestjs.io/docs/en/cli, not anything else. The features you mention are more documentation issues, I think. |
I think the most confusing overlapping configuration options are the different setup options:
All of these are a bit different flavour of essentially the same thing. What makes it especially difficult is that there's no clear explanation which is used for what purposes and when they are called, are they async or not and so on. And how they differ from each others. To me it looks that all of them have came when someone has needed a new place to setup something and was too afraid to change the existing logic. |
I still want to do this at some point, but I'm not sure when I'll have the energy to take it on. Current plans for Jest and breaking changes are: V28 is right around the corner and will be filled with breaking changes., mostly minor or easy to tweak your code for. V29 will most probably only change Anyways - my current thinking before I postpone my reminder yet again: My current thinking hasn't changed much from the OP for file matching (i.e. I'd like to go for just arrays of globs), but we can probably support a function in addition (note that since we run in workers, it would have to be a string pointing to a module exporting a function). I'm thinking they'd be mutually exclusive - either provide globs or a custom function. We tested out doing just globs when I visited FB a few years ago, and it didn't cover their use cases. So a function seems reasonable. @villesau (you might know, but others reading might not)
They all serve different purposes and cannot really be interchanged (except most That said, we should probably document these better, and explain when to use which. |
This is a bit rambling, sorry about that. Feel free to edit this to clean it up and add more to it.
Jest currently has 51(!!) configuration options, many of which overlaps or intentionally overrides other options. (CLI options are out of scope for this issue, but they are obviously very much related)
They fall into a few different categories:
collectCoverageFrom
coveragePathIgnorePatterns
forceCoverageMatch
modulePathIgnorePatterns
testMatch
testPathIgnorePatterns
testRegex
transformIgnorePatterns
unmockedModulePathPatterns
watchPathIgnorePatterns
require
api (node paths, extensions)resetMocks
vsclearMocks
vsrestoreMocks
automock
moduleDirectories
vsmodulePaths
(I honestly have no idea)First of all, I'd like to simplify the file matching a lot. Both to make it easier to use, but also easier to implement and reason about.
For file matching I think
coveragePatterns: Array<Glob>
,testPatterns: Array<Glob>
,transformPatterns: Array<Glob>
and remove everything else. You can use negated patterns to exclude things instead of aignore
thing. No moreforce
to override ignores.We could also group things (using the current names, although they are subject to change):
coverage
can havecollectCoverage
,coverageDirectory
,coverageReporters
,coverageThresholds
,collectCoverageFrom
globalSetup
,setupTestFrameworkScriptFile
,setupFiles
,globalTeardown
.setup
which exported different functions. Not as declarative, but maybe better?module
can haveautomock
,resolver
,moduleDirectories
,modulePaths
,moduleNameMapper
,moduleFileExtensions
,resetModules
mocks
can haveresetMocks
,clearMocks,
restoreMocks,
timers(
automock?`)snapshots
can havesnapshotSerializers
,snapshotResolver
notify
andnotifyMode
can be combined (true
is default mode,string
sets the mode)Another thing that's somewhat confusing is the difference between
ProjectConfig
andGlobalConfig
. While the separation makes sense, it's invisible to users, and hard for them to reason about. It also doesn't work well with presets.Finally, I leave you with this awesome article https://fishshell.com/docs/current/design.html 🙂
The text was updated successfully, but these errors were encountered: