Skip to content

Generate working main() methods in test files #3582

Closed

Description

@athomas

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).

@pq

Activity

  1. added
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    on Sep 28, 2021
  2. DanTup commented on Sep 28, 2021

    @DanTup
    Member

    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 :-) )

  3. added
    in editorRelates to code editing or language features
    in lsp/analysis serverSomething to be fixed in the Dart analysis server
    on Sep 28, 2021
  4. added this to the On Deck milestone on Sep 28, 2021
  5. pq commented on Oct 1, 2021

    @pq
  6. modified the milestones: On Deck, v3.39.0 on Apr 4, 2022
  7. DanTup commented on Apr 4, 2022

    @DanTup
    Member

    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.

  8. DanTup commented on Apr 28, 2022

    @DanTup
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in editorRelates to code editing or language featuresin lsp/analysis serverSomething to be fixed in the Dart analysis serveris enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions