( actual: ArrayLikeArg<T>, expected: ArrayLikeArg<T>, msg?: string, )
| 38 | * @throws {AssertionError} If any value in `expected` is not found in `actual`. |
| 39 | */ |
| 40 | export function assertArrayIncludes<T>( |
| 41 | actual: ArrayLikeArg<T>, |
| 42 | expected: ArrayLikeArg<T>, |
| 43 | msg?: string, |
| 44 | ): void { |
| 45 | const missing: unknown[] = []; |
| 46 | const expectedLen = expected.length; |
| 47 | const actualLen = actual.length; |
| 48 | for (let i = 0; i < expectedLen; i++) { |
| 49 | const item = expected[i]; |
| 50 | let found: boolean; |
| 51 | if (isPrimitive(item)) { |
| 52 | // Fast path |
| 53 | found = Array.prototype.includes.call(actual, item); |
| 54 | } else { |
| 55 | found = false; |
| 56 | for (let j = 0; j < actualLen; j++) { |
| 57 | if (equal(item, actual[j])) { |
| 58 | found = true; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | if (!found) { |
| 64 | missing.push(item); |
| 65 | } |
| 66 | } |
| 67 | if (missing.length === 0) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | const msgSuffix = msg ? `: ${msg}` : "."; |
| 72 | msg = `Expected actual: "${format(actual)}" to include: "${ |
| 73 | format(expected) |
| 74 | }"${msgSuffix}\nmissing: ${format(missing)}`; |
| 75 | throw new AssertionError(msg); |
| 76 | } |
no test coverage detected