-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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: Add a mode to watch with the ability to update an expected literal #4442
Comments
There's some interesting feedback against this on twitter:
|
I'm not sold on these being arguments alone against having a feature like this, there are a bazillion use cases where you need to make small changes to existing expectations - literals like numbers/strings aren't exactly a good match for using snapshots on a larger scale. I get where they're coming from though 👍 Making it more thoughtful is a great idea, so I'd say that you should probably need to confirm each transition in the run: FAIL src/_tests/_wrong.test.tsx
● is an incorrect test
Expected value to equal:
5
Received:
4
Updated test code:
it("is an incorrect test", () => {
-- expect(4).toEqual(5)
++ expect(4).toEqual(4)
})
in file src/_tests/_wrong.test.tsx:2:13
Do you want to update? [Yn] Which is a better feeling overall for me. Doing so would mean having to re-run in only one process (to allow test running to be blocked) but I think that's a fine trade-off. You're not going to be using this all the time. One of my end goals will be to allow the jest-editor-support to be able to state "here's something fixable" so that you get this kind of popover (but for changing the string |
Mainly so that I can keep track, but VS Code is going to introduce an API allowing extensions to describe changes like this, so editor integration will be pretty simple too - microsoft/vscode#34664 |
Got an hour to have a think about this, I think it can be done by allowing matchers to define fixits. So, the return value from any matcher, can go from: export type ExpectationResult = {
pass: boolean,
message: () => string,
}; to export type ExpectationResult = {
pass: boolean,
message: () => string,
fixit?: ExpectationFixit,
};
export type ExpectationFixit = {
message: string,
type: 'number', // This will get built out as more types are supported
from: {
start?: Callsite,
end?: Callsite,
value: string,
},
to: {
value: string,
},
}; So for example: toEqual(received: any, expected: any) {
// [...]
let fixit: ?ExpectationFixit = null;
if (areBothNumbers(expected, received)) {
fixit = ExpectationFixit = {
from: {
value: expected,
},
message: 'Update number to',
to: {
value: received,
},
type: 'number',
};
}
// Passing the the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
// or create a different error message
return {actual: received, expected, message, name: 'toEqual', pass, fixit};
}, Which gets passed up to the error eventually thrown by the matcher. This then gets picked up by something which has to do source mapping somewhere and that can be used to set up the example callsites. Then a watch plugin can listen out for errors with fixits, or jest-editor-support can ensure it gets passed through JSON and present a UI |
Love the proposal! I've got two quick questions @orta
|
Yeah, maybe it would need a different keyboard shortcut TBH, I would rather they be different personally. I can see a few common use cases where you want to update either separately. Maybe "f" if it's free? |
|
|
One way to get away from keyboard shortcuts could be adding a command mode. ⇧+⌘+p
and it would look similar to filter mode.
Jest itself—or a plugin, could do typeahead and filter the commands in a list below the command prompt similar to what https://github.com/jest-community/jest-watch-typeahead does for watch mode |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
With
yarn jest -- --watch
What is the potential behavior?
What if there was an extra watch mode:
Where after pressing
u
it re-runs with whatever lasts triggered the test run ( so maybe the watchman announcement of a file being saved ) and outputs something like:Which updates the file
src/_tests/_wrong.test.tsx
. Messaging to the watcher should have the same feeling as a snapshot update.Implementation
This could be an incrementally built out feature:
4
->5
"four"
->"five"
{ four: true }
->{ five: true }
As number literals have the least amount of potential variants ( vs the multiple types of string literals ) that makes sense as a nice place to start.
I had initially wondered if this could be built out as its own plugin, but once I came to the conclusion about the user experience side of it - it really needs work inside the watcher, though it may be feasible to have the work of AST transformation happen inside a new package ( which could make to easy for dev tools to use it too)
Jest could pass the AST of the
it
block that failed to the new package, and then it's the job of the package to read through the AST and make the transformation. I don't know if there's an easy way to go from an AST literal back to the LOC/character index (As I've not done too much there) but if prettier/codemods can do it, so can this package.The text was updated successfully, but these errors were encountered: