Skip to content

Commit

Permalink
add notContains, output comparison in contains, use .includes
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 29, 2023
1 parent 528b1c2 commit 5c29ea7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

- Added `notContains`
- `contains` and `notContains` now output the failed comparison, rather than just the failure
- No need to not use `String.prototype.includes` as it is supported by all environments we support

## v11.6.0 2023 November 24

- Updated dependencies, [base files](https://github.com/bevry/base), and [editions](https://editions.bevry.me) using [boundation](https://github.com/bevry/boundation)
Expand Down
27 changes: 26 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

// Polyfill for older node versions
if (Array.prototype.includes == null) {
// Array.prototype.includes missing on Node.js v4, first added in Node.js v6
// eslint-disable-next-line no-extend-native
Array.prototype.includes = function (searchElement) {
return this.indexOf(searchElement) !== -1
}
}
// String.prototype.includes included on Node.js v4

// Import
import {
Expand Down Expand Up @@ -322,8 +324,9 @@ export function contains(
if (testName == null)
testName = `Expected [${actual}] to contain [${expected}]`
try {
_ok(actual.indexOf(expected) !== -1, testName)
_ok(actual.includes(expected) === true, testName)
} catch (checkError: any) {
logComparison(actual, expected, checkError)
if (next) {
next(checkError)
return
Expand All @@ -334,6 +337,28 @@ export function contains(
if (next) next()
}

/** Checks to see if the actual result does not contain the expected result .*/
export function notContains(
actual: any,
expected: any,
testName = 'does not contain assertion',
next?: Errback
): void | never {
if (testName == null)
testName = `Expected [${actual}] to not contain [${expected}]`
try {
_ok(actual.includes(expected) === false, testName)
} catch (checkError: any) {
logComparison(actual, expected, checkError)
if (next) {
next(checkError)
return
} else {
throw checkError
}
}
if (next) next()
}
/** Checks to see if an error was as expected, if a failure occurs it will output detailed information */
export function errorEqual(
actualError: any,
Expand Down
7 changes: 7 additions & 0 deletions source/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ kava.suite('assert-helpers', function (suite, test) {
fail()
} catch (err) {}
})
test('notContains', function () {
helpers.notContains('ab', 'c')
try {
helpers.notContains('ab', 'a')
fail()
} catch (err) {}
})
test('errorEqual', function () {
const a = new Error('abc')
helpers.errorEqual(a, 'abc')
Expand Down

0 comments on commit 5c29ea7

Please sign in to comment.