Success messages
By default, Vest tests focus on failures (errors and warnings). However, sometimes you want to provide positive feedback when a test passes—for example, a "Password is strong" message or a checkmark next to a field.
To mark a test with a success severity, call success() from the body of your test. Unlike warn(), the success message will only be surfaced if the test passes.
If you need to set success severity after an await in an async test, use useSuccess() and call the setter it returns.
success() vs useSuccess() in async tests​
- You may only use
success()anduseSuccess()from within the body of atestfunction. success()should be called in the synchronous portion of your test.useSuccess()is the async-safe alternative when success severity needs to be set later.
// ✔
test('password', () => {
success();
enforce(password).matches(/[A-Z]/);
});
// ✔ Use useSuccess() when you need to set success after await
test('username', async () => {
const setSuccess = useSuccess();
await checkAvailability(username);
setSuccess();
});
Behavior and Priority​
- Opt-in only: Passing tests do not have a "success" message by default. You must explicitly call
success(). - Passed tests only: If a test marked with
success()fails, the success message is discarded, and the test is treated as a regular failure (or warning). - No impact on validity:
successseverity never makes a suite or field invalid. It is purely informational. - Message Priority:
getMessage()prioritizes messages in this order:error>warn>success.
Using successes in the result object​
You can access success messages using these methods:
result.hasSuccesses(); // Returns whether any success messages are present in the suite.
result.hasSuccesses('password'); // Returns whether any success messages are present in the 'password' field.
result.getSuccesses(); // Returns an object with all fields that have successes.
result.getSuccesses('password'); // Returns an array of success messages for the password field.
Read next about: