You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generate working main() methods in test files #3582
Is your feature request related to a problem? Please describe.
When you create a new test file for use with package:test, you need a main method without arguments (e.g. void main()). However, the main template in vscode will insert void main(List<String> arguments) {.... The analyzer can't find an issue with that because it's a valid main method. However, running the tests fails because package:test barfs at the main method.
Describe the solution you'd like
A context aware template that inserts the main method with arguments in regular dart files but inserts a main method without arguments in _test.dart files. (Or alternatively, a second template that also comes up when you type main and inserts something appropriate for tests, perhaps even contains a bit more common test skeleton code like an actual test method or setUp/tearDown calls).
This is related to #2059 (and came up in #3347 and others). The proper fix is to move the snippets from the client to the server. This would allow them to adjust to many things like language versions (eg. conditionally include ?), account for lints, not show stful etc. in places it isn't valid to create a class, and also have different mains based on whether a file is a test (although I've found that "a test file" has conflicting definitions depending on who you ask :-) )
If you're using the latest VS Code extensions (just published) and on Flutter master, you can now user the dart.enableServerSnippets setting to switch to server-provided snippets. These will become enabled by default in a future release, but any feedback in the meantime is appreciated.
Server-provided snippets are more dynamic/context aware and handle this issue, plus some others:
main methods in test files won't have args
Null operators like ? will only be inserted when null safety is enabled
Snippets won't show up in contexts they're not valid (for example stful will no longer show up when you're not at the top level)
Right now this is only in VS Code, but with some work in the IntelliJ extension (+ adding to the original analysis server protocol) it should be possible to use the same shared snippets there.
In the upcoming release of Dart-Code, server snippets will be enabled by default if your SDK supports them (which if you're on stable won't be until the next SDK releases, but if you're on master/dev, it probably already does). The server-provided snippets will conditionally include List<String> args depending on whether the file is a test file.
Activity
DanTup commentedon Sep 28, 2021
This is related to #2059 (and came up in #3347 and others). The proper fix is to move the snippets from the client to the server. This would allow them to adjust to many things like language versions (eg. conditionally include
?), account for lints, not showstfuletc. in places it isn't valid to create a class, and also have differentmains based on whether a file is a test (although I've found that "a test file" has conflicting definitions depending on who you ask :-) )pq commentedon Oct 1, 2021
/fyi @bwilkerson and @jwren
DanTup commentedon Apr 4, 2022
If you're using the latest VS Code extensions (just published) and on Flutter
master, you can now user thedart.enableServerSnippetssetting to switch to server-provided snippets. These will become enabled by default in a future release, but any feedback in the meantime is appreciated.Server-provided snippets are more dynamic/context aware and handle this issue, plus some others:
mainmethods in test files won't have args?will only be inserted when null safety is enabledstfulwill no longer show up when you're not at the top level)Right now this is only in VS Code, but with some work in the IntelliJ extension (+ adding to the original analysis server protocol) it should be possible to use the same shared snippets there.
DanTup commentedon Apr 28, 2022
In the upcoming release of Dart-Code, server snippets will be enabled by default if your SDK supports them (which if you're on stable won't be until the next SDK releases, but if you're on master/dev, it probably already does). The server-provided snippets will conditionally include
List<String> argsdepending on whether the file is a test file.