-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vest): add support for optional tests and "isValid" (#612)
- Loading branch information
Showing
28 changed files
with
329 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# optional fields | ||
|
||
> Since 3.2.0 | ||
It is possible to mark fields in your suite as optional fields. This means that when they are skipped, the suite may still be considered as valid. | ||
All fields are by default required, unless explicitly marked as optional using the `optional` function. | ||
|
||
## Usage | ||
|
||
`optional` can take a field name as its argument, or an array of field names. | ||
|
||
```js | ||
import vest, { optional, only, test, enforce } from 'vest'; | ||
|
||
const suite = vest.create('RegisterPet', (data, currentField) => { | ||
only(currentField); // only validate this specified field | ||
|
||
optional(['pet_color', 'pet_age']); | ||
/** Equivalent to: | ||
* optional('pet_color') | ||
* optional('pet_age') | ||
**/ | ||
|
||
test('pet_name', 'Pet Name is required', () => { | ||
enforce(data.name).isNotEmpty(); | ||
}); | ||
|
||
test('pet_color', 'If provided, pet color must be a string', () => { | ||
enforce(data.color).isString(); | ||
}); | ||
|
||
test('pet_age', 'If provided, pet age must be numeric', () => { | ||
enforce(data.age).isNumeric(); | ||
}); | ||
}); | ||
|
||
suite({ name: 'Indie' }, /* -> only validate pet_name */ 'pet_name').isValid(); | ||
// ✅ Since pet_color and pet_age are optional, the suite may still be valid | ||
|
||
suite({ age: 'Five' }, /* -> only validate pet_age */ 'pet_age').isValid(); | ||
// 🚨 When erroring, optional fields still make the suite invalid | ||
``` | ||
|
||
## Difference between `optional` and `warn` | ||
|
||
While on its surface, optional might seem similar to warn, they are quite different. | ||
optional, like "only" and "skip" is set on the field level, which means that when set - all tests of an optional field are considered optional. Warn, on the other hand - is set on the test level, so the only tests affected are the tests that have the "warn" option applied within them. | ||
|
||
Another distinction is that warning tests cannot set the suite to be invalid. | ||
|
||
There may be rare occasions in which you have an optional and a warning only field, in which case, you may combine the two. |
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import vest, { test, optional } from 'vest'; | ||
|
||
describe('isValid', () => { | ||
describe('Before any test ran', () => { | ||
it('Should return false', () => { | ||
const suite = vest.create(() => { | ||
test('field_1', () => false); | ||
}); | ||
|
||
expect(suite.get().isValid()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('When there are errors in the suite', () => { | ||
let suite; | ||
|
||
beforeEach(() => { | ||
suite = vest.create(skip => { | ||
vest.skip(skip); | ||
optional('field_1'); | ||
|
||
test('field_1', () => false); | ||
test('field_2', () => false); | ||
test('sanity', () => true); | ||
}); | ||
}); | ||
|
||
it('Should return false when an optional test has errors', () => { | ||
expect(suite('field_2').isValid()).toBe(false); | ||
}); | ||
it('Should return false when a required test has errors', () => { | ||
expect(suite('field_1').isValid()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('When there are warnings in the suite', () => { | ||
let suite; | ||
|
||
beforeEach(() => { | ||
suite = vest.create(() => { | ||
test('field_1', () => { | ||
vest.warn(); | ||
return false; | ||
}); | ||
}); | ||
}); | ||
it('Should return true when a required test has warnings', () => { | ||
expect(suite().isValid()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('When a non optional field is skipped', () => { | ||
let suite; | ||
|
||
beforeEach(() => { | ||
suite = vest.create(skip => { | ||
vest.skip(skip); | ||
test('field_1', () => { | ||
return false; | ||
}); | ||
test('field_2', () => { | ||
return true; | ||
}); | ||
test('field_3', () => { | ||
return true; | ||
}); | ||
}); | ||
}); | ||
it('Should return false', () => { | ||
expect(suite('field_1').isValid()).toBe(false); | ||
}); | ||
it('Should return false', () => { | ||
expect(suite(['field_2', 'field_3']).isValid()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('When a all required fields are passing', () => { | ||
let suite; | ||
|
||
beforeEach(() => { | ||
suite = vest.create(() => { | ||
test('field_1', () => { | ||
return true; | ||
}); | ||
test('field_2', () => { | ||
return true; | ||
}); | ||
test('field_3', () => { | ||
return true; | ||
}); | ||
}); | ||
}); | ||
it('Should return true', () => { | ||
expect(suite('field_1').isValid()).toBe(true); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
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
Oops, something went wrong.