diff --git a/src/createSatisfier.spec.ts b/src/createSatisfier.spec.ts index 52ccfe5..4d86bac 100644 --- a/src/createSatisfier.spec.ts +++ b/src/createSatisfier.spec.ts @@ -40,6 +40,19 @@ test('expect [undefined]', t => { t.true(createSatisfier([undefined]).test([undefined])) }) +test('expect [undefined] should work with [null]', t => { + t.false(createSatisfier([undefined]).test([null])) +}) + +test('expect array and test against non-array', t => { + const s = createSatisfier([1]) + t.false(s.test(null)) + t.false(s.test(1)) + t.false(s.test('a')) + t.false(s.test(true)) + t.false(s.test(undefined as any)) +}) + test('array with number', t => { t.true(createSatisfier([1, 2]).test([1, 2])) }) diff --git a/src/createSatisfier.ts b/src/createSatisfier.ts index 4266f33..486d725 100644 --- a/src/createSatisfier.ts +++ b/src/createSatisfier.ts @@ -55,7 +55,7 @@ function detectDiff(actual, expected, path: string[] = []) { }) } } - else if (expected === null) { + else if (expected === null || expected === undefined) { if (expected !== actual) diff.push({ path, @@ -81,10 +81,19 @@ function detectDiff(actual, expected, path: string[] = []) { } } else if (Array.isArray(expected)) { - expected.forEach((e, i) => { - const actualValue = actual[i] - diff.push(...detectDiff(actualValue, e, path.concat([`[${i}]`]))) - }) + if (!Array.isArray(actual)) { + diff.push({ + path, + expected, + actual + }) + } + else { + expected.forEach((e, i) => { + const actualValue = actual[i] + diff.push(...detectDiff(actualValue, e, path.concat([`[${i}]`]))) + }) + } } else { // expected is object. If actual is not, then it is diff.