microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.74k stars 29.1k forks source link

Improve selfhost testing experience #189680

Closed connor4312 closed 1 year ago

connor4312 commented 1 year ago
gjsjohnmurray commented 1 year ago

Personally I am hoping greater use by the team of the native testing UI will lead to the coverage API getting some attention. In particular, until its results get surfaced in the UI there's not a lot of incentive for us extension developers to invest in it. See #123713

ulugbekna commented 1 year ago

I’m a big fan of snapshot testing that Jest supports and think we could benefit greatly from adopting it.

Snapshot testing

Snapshot tests (aka "expect tests) are tests that capture the string representation of a given object and compare to the expected string. The important aspect of these tests is that those snapshots

https://github.com/microsoft/vscode/assets/16353531/f5550aed-b47c-4769-85ab-442524f33715

image

Snapshots also allow for more visual tests because one doesn't need to write those by hand:

        test('returns the positions of all functions in the source code', async () => {
            const source = outdent`
                function add(a, b) {
                    return a + b;
                }

                function subtract(a, b) {
                    return a - b;
                }
                `;
            expect(await jsSourceWithFunPos(source)).toMatchInlineSnapshot(`
            "<start-0>function add(a, b) {
                return a + b;
            }<end-0>

            <start-1>function subtract(a, b) {
                return a - b;
            }<end-1>"
        `);
        });

Some things that could be improved with Jest & Jest extension:

  1. Updating the snapshot requires a re-run of tests (possibly reuses the cached test runs, but still not instantaneous update as is the case with the OCaml language test runner), while we could store the expected outputs and update the file as required (this's the problem with Jest rather than the extension)
  2. It uses diagnostics to show the diff between test mismatches instead of a diff viewzone
image
  1. Each diff viewzone could have a button to accept the proposed snapshot

I like this blog post describing how snapshot tests can be nice - https://blog.janestreet.com/the-joy-of-expect-tests/

Jane Street is big on snapshot testing and have their own framework. Their way of creating snapshots is a bit different because they don't compare the snapshot against an object but they capture stdout from a test as a snapshot.


Happy to jump on a call to discuss this more and, in general, help with implementation some of the things, if time permits.

connor4312 commented 1 year ago

Followup items from discussion:

connor4312 commented 1 year ago

I finished the big themes I wanted to implement for this issues with the test CLI and its adoption in core. Copying the notice I sent for posterity


This week I've been working on a new way to run extension tests. It includes a configuration-driven command-line runner, as well as an extension that runs extensions tests in the VS Code UI.

:point_right: For people who own built-in extensions: You can now use this for built-in extensions in VS Code! To onboard your extension: (example)

  1. Add your extension to the extensions array in .vscode-test.js
  2. Update test-integration.sh and test-integration.bat to run using the runner
  3. If the tests don't run in the web, you can remove your old test .ts runner script. The runner does not yet support web tests (but it will soon) so this is still needed.
  4. You can now run your extension tests in two ways: a. Via yarn test-extension -l <label> on the command line. Do this to validate your config is set up correctly! b. With the ms-vscode.extension-test-runner extension installed, you can run and debug tests using the VS Code UI, as in the image below

:point_right: For people who write extensions: pretty similar to the above! You can use the above as guidelines as well as the documentation and an example.