Skip to content

Commit 88b3035

Browse files
Merge pull request #9 from qavajs/satisfy
added `to satisfy` validation to verify user-defined expectation provided as predicate
2 parents b89ee22 + 9f24ec9 commit 88b3035

File tree

6 files changed

+1147
-1300
lines changed

6 files changed

+1147
-1300
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1010
:pencil: - chore
1111
:microscope: - experimental
1212

13+
## [2.2.0]
14+
- :rocket: added `to satisfy` validation to verify user-defined expectation provided as predicate
15+
```Gherkin
16+
Then I expect '$value' to satisfy '$either(1, 2)'
17+
```
18+
where `$either` is a function
19+
```typescript
20+
function either(...expected) {
21+
return function (actual) {
22+
return expected.includes(actual)
23+
}
24+
}
25+
```
26+
1327
## [2.1.1]
1428
- :beetle: replaced .as to Cypress.log in page object logging
1529

lib/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defineParameterType({
3434

3535
defineParameterType({
3636
name: 'validation',
37-
regexp: /((?:is |do |does |to )?(not |to not )?(?:to )?(?:be )?(equal|strictly equal|deeply equal|have member|match|contain|above|below|greater than|less than|have type)(?:s|es)?)/,
37+
regexp: /((?:is |do |does |to )?(not |to not )?(?:to )?(?:be )?(softly )?(equal|strictly equal|deeply equal|have member|match|contain|above|below|greater than|less than|have type|have property|match schema|include members|satisfy|case insensitive equal)(?:s|es| to)?)/,
3838
useForSnippets: false,
3939
transformer: getValidation
4040
});

lib/valueExpect.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ chai.Assertion.addMethod('notStrictEqual', function (ER) {
1010
);
1111
});
1212

13+
chai.Assertion.addMethod('caseInsensitiveEqual', function (ER) {
14+
const obj = this._obj;
15+
16+
this.assert(
17+
obj.toLowerCase() === ER.toLowerCase(),
18+
'expected #{this} to equal #{exp}',
19+
'expected #{this} to not equal #{exp}',
20+
ER,
21+
obj
22+
);
23+
});
24+
25+
chai.Assertion.addMethod('satisfy', function (predicate) {
26+
const actual = this._obj;
27+
28+
this.assert(
29+
predicate(actual),
30+
'expected #{this} to satisfy #{exp}',
31+
'expected #{this} to not satisfy #{exp}',
32+
predicate.toString(),
33+
actual
34+
);
35+
});
36+
1337
export const validations = {
1438
EQUAL: 'equal',
1539
DEEPLY_EQUAL: 'deeply equal',
@@ -23,7 +47,9 @@ export const validations = {
2347
LESS: 'less than',
2448
HAVE_TYPE: 'have type',
2549
INCLUDE_MEMBERS: 'include member',
26-
HAVE_PROPERTY: 'have property'
50+
HAVE_PROPERTY: 'have property',
51+
CASE_INSENSITIVE_EQUAL: 'case insensitive equal',
52+
SATISFY: 'satisfy'
2753
};
2854

2955
const isClause = '(?:is |do |does |to )?';
@@ -50,6 +76,8 @@ const validationFns = {
5076
[validations.HAVE_TYPE]: (expectClause, ER) => expectClause.a(ER),
5177
[validations.INCLUDE_MEMBERS]: (expectClause, ER) => expectClause.include.members(ER),
5278
[validations.HAVE_PROPERTY]: (expectClause, ER) => expectClause.have.property(ER),
79+
[validations.CASE_INSENSITIVE_EQUAL]: (expectClause, ER) => expectClause.caseInsensitiveEqual(ER),
80+
[validations.SATISFY]: (expectClause, ER) => expectClause.satisfy(ER),
5381
};
5482

5583
/**

0 commit comments

Comments
 (0)