Skip to content
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

[feature idea] unit tests #91

Closed
Almenon opened this issue Aug 31, 2018 · 8 comments
Closed

[feature idea] unit tests #91

Almenon opened this issue Aug 31, 2018 · 8 comments
Assignees

Comments

@Almenon
Copy link

Almenon commented Aug 31, 2018

It would be nice if some of the samples had unit tests or there was a sample unit test project. It's good for people looking through the samples to see how to unit test it because putting off unit tests just leads to tech debt in the future.

@lannonbr
Copy link
Contributor

lannonbr commented Sep 6, 2018

I would be interested in making a PR for this given I've been writing a vscode extension and setting up some unit tests for it. Not sure what to test for an example but I am interested in helping out

@gkalpak
Copy link

gkalpak commented Sep 10, 2018

It would also be useful to distinguish between unit tests and end-to-end/integration tests.

E2E tests are useful because they run on an actual VSCode instance, but they are too slow to be run often during development (e.g. on save).

The unit tests are very fast and suitable for immediate feedback during development (e.g. on save), but require mocking; especially the vscode parts, which is tricky until you figure it out the first time.

FWIW, I am using mock-require for my setup. Here is my vscode mock and here my overall test setup (which is a little involved, because I wanted to use jasmine 😁).

@Almenon
Copy link
Author

Almenon commented Sep 18, 2018

I just updated my integration tests for the new webview API, the same format could be applied to the webview sample.

https://github.com/Almenon/AREPL-vscode/blob/master/test/suite/previewContainer.test.ts

The only catch is I had to make a mock context like so:

    const arepl = vscode.extensions.getExtension("almenon.arepl")!;

    const mockContext: any = {
        asAbsolutePath: (file: string)=>{
            return __dirname + "/" + file
        },
        extensionPath: arepl.extensionPath,
    }

Aside from that it's just calling the function that updates the html and verifying that the expected output is in the html, pretty simple.

@Almenon
Copy link
Author

Almenon commented Dec 15, 2018

A person just asked about this in the vscode slack channel.

Anon1
guys what do you do about unit testing `vscode` ?
We’re currently writing stub methods for everything we use
that’s getting boring, real quick

Some responses:

Anon2
doing the same over here. it’s not too bad but maybe i’m not using as much of the API as you are. i’m also using sinon for the stubs.

Anon3
It depends on what code you're trying to write tests for. You're right in that if you want to do unit testing you'll end up writing stubs for vscode api's.
In my experience it's a case by case evaluation on which approach (unit vs integration) will provide you the most benefits.

Anon4
I had noticed the same thing. Most extensions do not have tests. In my re-write of my extension I am making it a point to have unit tests. I’m using jest and have mocked the vscode api. I only mock what I need when I need it so that I don’t get ahead of myself. https://github.com/kenhowardpdx/vscode-gist/

In summary it seems like most people are in favor of mocking the actual VSCode API. This lets your tests run faster at the cost of the time it takes to make the mock.

@connor4312
Copy link
Member

Duplicates microsoft/vscode#94746

@Almenon
Copy link
Author

Almenon commented Jul 16, 2021

This issue is a subset of microsoft/vscode#94746, but not a duplicate. microsoft/vscode#94746 is asking for a mock library and general guidance. Can you reopen please?

@connor4312
Copy link
Member

There is guidance given to mocking in the discussion on that issue, for example my comment here: microsoft/vscode#94746 (comment)

@Almenon
Copy link
Author

Almenon commented Jul 17, 2021

I appreciate the comment, but it's not the same as having multiple unit test examples to reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants