Skip to main content
Version: 4.x

Failing with a custom message

Sometimes your validation logic might result in different failure reasons that are unknown before you run the test. In this case the message argument is not as useful, and instead you should omit it. Vest allows you to provide the message within the test body itself by doing one of the following:

Enforce message

A custom message can ban passed to enforce via the message modifier. Multiple messages can be passed as well in case you have multiple failure conditions.

The message must be specified before the rule it refers to, because once the rule fails, enforce throws immediately.

test('username', () => {
enforce(data.username)
.message('Username is required')
.isNotBlank()
.message('Username must be at least 3 characters')
.longerThan(2);
});

Throwing a string

If the message param is omitted, and the test throws a string value, the string will be used as the test's message:

test('price', () => {
if (price < 0) {
throw 'Price must be positive';
}
});
enforce.extend({
isPositive: value => {
return {
pass: value > 0,
message: () => 'value must be positive',
};
},
});

test('price', () => {
enforce(price).isPositive(); // will fail with the message: "value must be positive"
});

Rejecting with a string

Async tests can reject their promise with the string as well:

test('price', () => {
return apiCall().then(() => {
throw 'Price must be positive';
});
});

test('price', () => {
return Promise.reject('Price must be positive');
});