Skip to content

Unit testing extensions is quite difficult #94746

Closed as not planned
Closed as not planned

Description

@eamodio

Refs: #93604

Maybe we could provide a mock library for certain vs code types, and guidance (docs, examples, blogs, etc) on how it can be done effectively.

/cc Johannes Rieken (@jrieken)

Activity

  1. jrieken commented on Apr 9, 2020

    @jrieken
    Contributor

    Adding Pine (@octref) since you have been making good work here

  2. bfitzpat commented on Apr 9, 2020

    @bfitzpat

    Our QE team at Red Hat has been doing great work with the https://github.com/redhat-developer/vscode-extension-tester if anybody wants to take a look at it.

  3. octref commented on Apr 9, 2020

    @octref
    Contributor

    Brian Fitzpatrick (@bfitzpat) Thanks for mentioning. I'll take a look. Do you have specific issues you ran into? What specifics you were trying to test that you cannot test using our integration test?

    Eric Amodio (@eamodio) There's nothing actionable. Please be more precise. What's the issue they run into?

  4. added
    info-needed
    on Apr 9, 2020
  5. eamodio commented on Apr 9, 2020

    @eamodio
    ContributorAuthor

    Pine (@octref) the request here is about pure unit-tests -- not integration or UI driven. Basically providing anything we can do to make it easier as well as guidance for best practices.

    Almenon (@Almenon) Can you provide any more specific details here?

  6. octref commented on Apr 10, 2020

    @octref
    Contributor

    Prescribing one pattern excludes others. I don't think it's good to include "how you can write your extension so it's unit-testable".
    If there's anything specific to vscode extension that makes unit testing hard I can address it. But if it's just "split your code into parts where some requires vscode API the others can be unit-testable", I don't see much value advising everyone doing that.

  7. Almenon commented on Apr 11, 2020

    @Almenon

    This originates from #82471. I never got around to responding and the issue got locked, sorry. Rearchitecturing my code to decouple it from vscode would be difficult, or at least take a while. I tried mocking but that was nightmarish. Getting the mocks to behave like vscode in some cases basically required rewriting vscode logic.

    Maybe if I tried a blend of all three approaches - decoupling what can easily be decoupled for easy unit testing, mocking that which is lightly coupled, and integration testing that which is heavily coupled, maybe that would work effectively. But I would strongly prefer plain old unit testing if it was possible.

    Is there any technical solution that might solve #6586 to let vscode be imported during unit tests? Maybe there's some way it might be achieved (or maybe it fundamentally goes against the core architecture, idk)

    If there's no possible way it can be realistically done then the only action item I can think of would be adding documentation / code samples. See microsoft/vscode-extension-samples#91

  8. tamuratak commented on Apr 12, 2020

    @tamuratak
    Contributor

    To my experience with LaTeX Workshop, the following steps are an easy way to write unit tests.

    1. Export objects for which you want to write unit tests as a return value of activate function.
    2. In test codes, you can obtain the exported objects with vscode.extensions.getExtension.
    3. Write unit tests for the objects.
    4. If you do not want to export the objects in the release, you can use an environment variable to enable it only on tests.

    I do not think this is an appropriate way. However, it is super easy.

  9. Almenon commented on Apr 12, 2020

    @Almenon

    Takashi Tamura (@tamuratak) thanks, but I believe those are integration tests - they run with a live vscode instance and it looks like you run them through vscode-test so you can't choose a individual test to run. I'm talking about unit tests - the kind where you could run individual tests through the test explorer sidepanel.

    Also just curious but why are you exporting objects that way? In arepl I just import the objects for my integration tests directly.

  10. Almenon commented on Apr 12, 2020

    @Almenon

    While I was writing my above comment I realized that there are some more possibilities here. If there's a way to run vscode in headless mode the tests could be run faster and without the UI popping up. Unfortunately electron doesn't support headless mode yet 😢 (even though chrome supports it which is kinda weird).

    Another thing that would be nice is if you could run tests individually through the test explorer.

  11. iliazeus commented on Apr 14, 2020

    @iliazeus
    Contributor

    Eric Amodio (@eamodio)

    To make unit testing easier, it would be nice to have certain classes and enums from the extension API to be available without VSCode itself.

    Like, for example, Ranges and Locations are nice to use internally, not only when interfacing with VSCode, but we can't unit test that code in a generic nodejs environment.

    This has already been done for Uris with the vscode-uri package.

  12. added
    feature-request
    and removed
    info-needed
    on Apr 16, 2020
  13. 6 remaining items

  14. dcermak commented on Apr 23, 2020

    @dcermak

    What might help (at least it would help me) would be a "test/mock mode" for vscode or some other library that you'd inject via dependency injection. This would allow you to setup expected user input and give you convenience functions to check that certain things happened.
    For example:

    it("inputBox rejects a number", async () => {
      const someObj = new Something();
      vscodeTest.setUserInput(InputBox, "123");
      await someObj.askForName();
      vscodeTest.assertError(InputBox, "Invalid name: it contains only numbers");
    });

    The API that I've written down is of course terrible, but something along those lines would be certainly helpful although it overlaps a bit with vscode-extension-tester.

  15. Lennon925 commented on May 28, 2020

    @Lennon925

    Refs: #93604

    Maybe we could provide a mock library for certain vs code types, and guidance (docs, examples, blogs, etc) on how it can be done effectively.

    /cc Johannes Rieken (@jrieken)

    Hi, Any way to get extentionContext in unit test?

  16. DonJayamanne commented on Jul 3, 2020

    @DonJayamanne
    Contributor

    Today in the Python extension we mock most of the VS Code API.

    We (Python Extension) find that to be very simple, however it could be a lot simpler if we were using SinonJs.
    I have also used sinonjs (and mocked VS Code) in another extension, see here https://github.com/DonJayamanne/gitHistoryVSCode/tree/master/test/extension.

    As for full blow UI tests, the Python extension did go down this route. Basically we used the VS code Smoke Tests infrastructure and built on top of that to run full blown UI tests. However we stopped using that simply because we didn't see much value in it (i.e. it didn't get used much).

    If anyone is interested in the UI tests we had, it can be found in this PR (where it was remove).
    https://github.com/microsoft/vscode-python/pull/10215/files
    We used puppeteer + BDD (cucumberjs) for our tests.

  17. manunamz commented on Jun 28, 2022

    @manunamz
    Contributor

    In case others still want examples for testing:

    I found the mocks here useful.

  18. michaeljohnbennett commented on Aug 5, 2022

    @michaeljohnbennett

    I agree with everyone above, we have a reasonably mature set of tests but sometimes the amount of sinon and rewire in there scares me. You get to thinking are you testing the mocking frameworks more than the actual VSCode API.

    I have to say for a well used application development framework the documentation and testing components are woefully inadequate and the examples that are official are so naive to be embarassing TBH. Even when I look at repos like the docker extension the level of testing they have done is so cursory it's quite shocking.

    I'd love to see or have somewhere where the community could actually make this better as we all seem to have hacked around it in some ways (The RedHat stuff is almost the same our fake context/memento objects) and others have done the same with quickpicks. Why not make some kind of repo/package that has all the best practices inside it?

  19. added
    *out-of-scope
    on Dec 4, 2023
  20. vscodenpa commented on Dec 4, 2023

    @vscodenpa

    We closed this issue because we don't plan to address it in the foreseeable future. If you disagree and feel that this issue is crucial: we are happy to listen and to reconsider.

    If you wonder what we are up to, please see our roadmap and issue reporting guidelines.

    Thanks for your understanding, and happy coding!

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

Metadata

Metadata

Labels

*out-of-scopeextensions-developmentfeature-request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions