-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
# Accessing intermediate suite results | ||
|
||
In some cases, you want to conditionally run tests based on the results of other tests. For example, preventing validating a field over the server when you already know the field is invalid. | ||
|
||
For this you can use `vest.draft()`. The `traft` function returns the intermediate [result object](./result) of the currently running suite. | ||
|
||
**Limitations when using vest.draft()** | ||
|
||
- It is only possible to access intermediate test results for sync tests, and it is recommended to put all the async tests at the bottom of your suite so they have access to the result of all the sync tests. | ||
- It is only possible to access intermediate test results for sync tests, and it is recommended to put all the async tests at the bottom of your suite so they have access to the result of all the syenc tests. | ||
- You may not call draft from outside a running suite. Doing that will result in a thrown error. | ||
- Each `draft()` call returns a copy of your suite result, and its result gets outdated between test calls. Do not try to save `draft` result in a variable for later use, instead, call `draft` whenever you need to use it. | ||
|
||
In the following example, we're preventing the async validation from running over the username field in case it already has errors. | ||
|
||
```js | ||
import vest, { test, enforce } from 'vest'; | ||
|
||
vest('NewUserForm', () => { | ||
test('username', 'Must be a string between 2 and 10 chars', () => { | ||
enforce(data.username).isString().longerThan(1).shorterThan(11); | ||
}); | ||
|
||
if (!vest.draft().hasErrors('username')) { | ||
// if `username` did not pass the previous test, the following test won't run | ||
test('username', 'username already exists', () => doesTheUserExist(username)); | ||
} | ||
test('username', 'Must be a string between 2 and 10 chars', () => { | ||
enforce(data.username).isString().longerThan(1).shorterThan(11); | ||
}); | ||
|
||
if (!vest.draft().hasErrors('username')) { | ||
// if `username` did not pass the previous test, the following test won't run | ||
test('username', 'username already exists', () => | ||
doesTheUserExist(username) | ||
); | ||
} | ||
}); | ||
``` |