Skip to content

Commit 9eaac47

Browse files
authored
Add expects: fail and ok (#10)
* add to gitignore .idea dir * fix README example of config * add expectFail and expectOk * update docs * bump version of lib
1 parent 0e8488f commit 9eaac47

6 files changed

Lines changed: 109 additions & 3 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,6 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
# Webstorm
133+
.idea

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Enable it inside codecept conf file:
1515
{
1616
helpers: {
1717
Playwright: {...},
18-
Expect: {
18+
ExpectHelper: {
1919
require: '@codeceptjs/expect-helper'
2020
},
2121
}
@@ -634,6 +634,43 @@ I.expectMatchesPattern(actual, pattern); // Passes
634634
I.expectMatchesPattern(actual, { name: 'Doe' }, 'Object does not match the pattern'); // Fails with custom error message
635635
```
636636

637+
### I.expectFail
638+
639+
Forces the current test to fail with an optional custom error message.
640+
641+
```js
642+
I.expectFail(customErrorMsg = 'expect fail')
643+
```
644+
645+
* `customErrorMsg`: (Optional) Custom error message to display when the assertion fails.
646+
647+
**Example:**
648+
649+
```js
650+
I.expectFail('This step should not be reached'); // Fails with custom error message
651+
```
652+
653+
### I.expectOk
654+
655+
Asserts that a value is truthy.
656+
657+
```js
658+
I.expectOk(actualValue, customErrorMsg = '')
659+
```
660+
661+
* `actualValue`: The value to check.
662+
* `customErrorMsg`: (Optional) Custom error message to display if the assertion fails.
663+
664+
**Example:**
665+
666+
```js
667+
I.expectOk(true); // Passes
668+
I.expectOk(1); // Passes
669+
I.expectOk('text'); // Passes
670+
I.expectOk(false, 'Expected value to be truthy'); // Fails with custom error message
671+
```
672+
673+
637674
---
638675

639676
This documentation provides a comprehensive overview of the `ExpectHelper` class and its methods. Each method is designed to perform specific assertions, making it easier to write and maintain tests. The examples provided demonstrate how to use each method effectively.

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ declare class ExpectHelper {
2525
expectDeepIncludeMembers(superset: any[], set: any[], customErrorMsg?: string): void;
2626
expectDeepEqualExcluding(actualValue: any, expectedValue: any, fieldsToExclude: string | string[], customErrorMsg?: string): void;
2727
expectMatchesPattern(actualValue: object, expectedPattern: object, customErrorMsg?: string): void;
28-
}
28+
expectFail(customErrorMsg?: string): void;
29+
expectOk(actualValue: object, customErrorMsg?: string): void;
30+
}

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,27 @@ class ExpectHelper {
410410
const errorMessage = customErrorMsg ? `${customErrorMsg}: Expected "${JSON.stringify(actualValue)}" to match the pattern "${JSON.stringify(expectedPattern)}"` : `Expected "${JSON.stringify(actualValue)}" to match the pattern "${JSON.stringify(expectedPattern)}"`;
411411
return assert(result, errorMessage);
412412
}
413+
414+
/**
415+
*
416+
* @param {*} [customErrorMsg]
417+
*/
418+
expectFail(customErrorMsg = 'expect fail') {
419+
// @ts-ignore
420+
output.step(`I expect fail`)
421+
return expect.fail(customErrorMsg);
422+
}
423+
424+
/**
425+
*
426+
* @param {*} actualValue
427+
* @param {*} [customErrorMsg]
428+
*/
429+
expectOk(actualValue, customErrorMsg = '') {
430+
// @ts-ignore
431+
output.step(`I expect "${JSON.stringify(actualValue)}" to be ok`)
432+
return expect(actualValue, customErrorMsg).to.be.ok;
433+
}
413434
}
414435

415436
export default ExpectHelper

index_test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,5 +667,48 @@ describe('#expectContain', () => {
667667
})
668668
})
669669

670+
describe('#expectFail', () => {
671+
it('should show error', () => {
672+
try {
673+
I.expectFail('custom failure message')
674+
} catch (e) {
675+
expect(e.message).to.contain('custom failure message')
676+
}
677+
})
678+
679+
it('should show default error', () => {
680+
try {
681+
I.expectFail()
682+
} catch (e) {
683+
expect(e.message).to.contain('expect fail')
684+
}
685+
})
686+
})
687+
688+
describe('#expectOk', () => {
689+
it('should not show error', () => {
690+
I.expectOk(true)
691+
I.expectOk(1)
692+
I.expectOk('text')
693+
I.expectOk({})
694+
})
695+
696+
it('should show error', () => {
697+
try {
698+
I.expectOk(false, 'expected value to be ok')
699+
} catch (e) {
700+
expect(e.message).to.contain('expected value to be ok')
701+
}
702+
})
703+
704+
it('should show default chai error', () => {
705+
try {
706+
I.expectOk(0)
707+
} catch (e) {
708+
expect(e.message).to.contain('expected +0 to be truthy')
709+
}
710+
})
711+
})
712+
670713

671714
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codeceptjs/expect-helper",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Expect helper for CodeceptJS",
55
"type": "module",
66
"types": "index.d.ts",

0 commit comments

Comments
 (0)