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

Proposal: add a contextValue to TestMessage #190277

Closed
connor4312 opened this issue Aug 11, 2023 · 2 comments · Fixed by #195706
Closed

Proposal: add a contextValue to TestMessage #190277

connor4312 opened this issue Aug 11, 2023 · 2 comments · Fixed by #195706
Assignees
Labels
api-finalization insiders-released Patch has been released in VS Code Insiders
Milestone

Comments

@connor4312
Copy link
Member

connor4312 commented Aug 11, 2023

Motivation

There are sometimes actions users may want to take on test results. For example, as @ulugbekna references #189680 (comment), applying the "actual" value to test expectations is a common case. We do it using diagnostics and the proposed testObserver API in the selfhost test provider, but this is not a great experience. @orta's Jest extension adds a context menu to rerun the test and apply the snapshot.

Proposal

Simply enough, to add a contextValue on the TestMessage class (along with some extra menus where actions can be show)

export class TestMessage2 extends TestMessage {

	/**
	 * Context value of the test item. This can be used to contribute message-
	 * specific actions to the test peek view. The value set here can be found
	 * in the `testMessage` property of the following `menus` contribution points:
	 *
	 * - `testing/message/context` - context menu for the message in the results tree
	 * - `testing/message/content` - a prominent button overlaying editor content where
	 *    the message is displayed.
	 *
	 * For example:
	 *
	 * ```json
	 * "contributes": {
	 *   "menus": {
	 *     "testing/message/content": [
	 *       {
	 *         "command": "extension.deleteCommentThread",
	 *         "when": "testMessage == canApplyRichDiff"
	 *       }
	 *     ]
	 *   }
	 * }
	 * ```
	 *
	 * The command will be called with an object containing:
	 * - `test`: the {@link TestItem} the message is associated with, *if* it
	 *    is still present in the {@link TestController.items} collection.
	 * - `message`: the {@link TestMessage} instance.
	 */
	contextValue?: string;

	// ...
}

Limitations & Alternatives

The main limitation arises if the information on the TestMessage and TestItem is insufficient to accomplish the necessary tasks. For snapshot testing, it should be sufficient to apply the actualOutput, but if it's not, then the runner is stuck re-running the test.

A fix for this would be if we ensure that the TestMessage instance passed to the executed command is the same as the one initially created in the test result, allow the consumer to use a WeakMap approach. However, this requires retaining test messages in the extension host, which we don't currently do and may be burdensome.

one clever workaround for this would be having the TestMessage instance be a "shell" class that gets/sets properties on an inner class. We could wipe out this inner instance when we're done with the result in the extension host, and re-hydrate it when the command is executed. Then we would only have the base memory usage from the shell class and avoid holding potentially huge results indefinitely.

An alternative would be to allow the TestMessage to have a list of Commands. This is more flexible, namely in that custom arguments can be supplied to the command, but we'd have to re-invent how to determine where actions are shown in the UI.

@connor4312 connor4312 added this to the August 2023 milestone Aug 11, 2023
@connor4312 connor4312 self-assigned this Aug 11, 2023
connor4312 added a commit that referenced this issue Aug 11, 2023
- Implements the proposal in #190277 by adding a `contextValue` to
  TestMessages added to test runs.
- Make the `FloatingClickMenu` reusable outside the editor, and uses
  it to implement a `testing/message/content` contribution point.

With this extensions can do things like:

![](https://memes.peet.io/img/23-08-68e2f9db-abc4-4717-9da6-698b002c481c.png)
connor4312 added a commit that referenced this issue Aug 12, 2023
- Implements the proposal in #190277 by adding a `contextValue` to
  TestMessages added to test runs.
- Make the `FloatingClickMenu` reusable outside the editor, and uses
  it to implement a `testing/message/content` contribution point.

With this extensions can do things like:

![](https://memes.peet.io/img/23-08-68e2f9db-abc4-4717-9da6-698b002c481c.png)
@connectdotz
Copy link

@connor4312, a bit of late feedback regarding the snapshots: our use case will fall into the "re-run test" category. It would be unlikely for us to update snapshots directly based on the test output for the following reasons:

  1. Avoid reinventing the wheel. Jest already knows how to update snapshots; we should not replicate the same logic, which is not as trivial as it might seem.
  2. Avoid snapshot version compatibility issues. Even if we were to decide to reinvent the wheel, the concerns of accuracy, version maintenance, and synchronization with Jest present a nightmare scenario we would like to avoid if possible.

For us, updating snapshots is really just a test run with a special flag. Therefore, it seems natural for the action to sit with the rest of the run/debug menu items. The TestMessage contextMenu will not add any additional benefit to our use case.

Just to clarify, we don't object to this feature, especially when it helps other extensions and use cases. This is just some context for reference.

@connor4312
Copy link
Member Author

Sounds good. However, on "Jest already knows how to update snapshots; we should not replicate the same logic" -- you could use this action to trigger Jest's snapshot update, you don't necessarily need to do the update by hand.

connor4312 added a commit that referenced this issue Oct 16, 2023
@vscodenpa vscodenpa added unreleased Patch has not yet been released in VS Code Insiders insiders-released Patch has been released in VS Code Insiders and removed unreleased Patch has not yet been released in VS Code Insiders labels Oct 16, 2023
Alex0007 pushed a commit to Alex0007/vscode that referenced this issue Oct 26, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-finalization insiders-released Patch has been released in VS Code Insiders
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants