Understanding Vest's state
Vest is a framework designed to perform validations on user inputs. To provide good user experience, we validate fields upon user interaction. Vest's state mechanism helps us accomplish this.
The state mechanism helps validate only the current field and not the rest of the form, making it suitable for validating user inputs one by one.
When you skip fields in your validation suite, Vest will merge their results from the previous suite run with the current result object. This helps maintain state between suite runs.
What Vest's state does​
-
Skipped field merge: Vest merges skipped fields' previous results with the current result object.
-
Lagging async
donecallback blocking: When an async test doesn't finish from the previous suite run, Vest blocks thedone()callbacks for that field from running for the previous suite result.
Drawbacks when using stateful validations
When validations are stateful, the benefit is that we don't have to keep track of which fields have already been validated and their previous results. However, the drawback of this approach is that when we run the same form in multiple unrelated contexts, the previous validation state still holds the previous result.
Here are some examples and solutions:
Single Page Application - suite result retention​
Suppose your form is a part of a single-page-app with client-side routing. In that case, if the user submits the form successfully, navigates outside the page, and later navigates back to the form, the form will have a successful validation state because the previous result is stored in the suite state.
Solution: Resetting suite state with .reset()​
In some cases, such as form reset, we want to discard previous validation results. This can be done with vest.reset().
.reset() is a property on your validation suite. Calling it will remove the suite's state.
Usage:​
import { create } from 'vest';
const suite = create(() => {
// Your tests go here
});
suite.reset(); // validation result is removed from Vest's state.