From 20ab9efe72e1fbf93e3d5fafae011f8813f61181 Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Sat, 13 Jan 2018 18:56:35 -0800 Subject: [PATCH] fix: object test agains null or undefined (#35) --- src/createSatisfier.spec.ts | 4 ---- src/createSatisfier.test.spec.ts | 13 ++++++++++++- src/createSatisfier.ts | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/createSatisfier.spec.ts b/src/createSatisfier.spec.ts index 4d86bac..b025010 100644 --- a/src/createSatisfier.spec.ts +++ b/src/createSatisfier.spec.ts @@ -32,10 +32,6 @@ test('actual should be a complete struct', t => { t.true(s.test({ a: 1, b: 'b' })) }) -test('expect null', t => { - t.true(createSatisfier(null).test(null)) -}) - test('expect [undefined]', t => { t.true(createSatisfier([undefined]).test([undefined])) }) diff --git a/src/createSatisfier.test.spec.ts b/src/createSatisfier.test.spec.ts index ef070ea..f7b5da5 100644 --- a/src/createSatisfier.test.spec.ts +++ b/src/createSatisfier.test.spec.ts @@ -2,7 +2,7 @@ import { test } from 'ava' import { createSatisfier } from './index' -test('empty expecter passes everything', t => { +test('empty expecter passes everything but not null or undefined', t => { t.true(createSatisfier({}).test({})) t.true(createSatisfier({}).test({ a: 1 })) t.true(createSatisfier({}).test({ a: true })) @@ -10,8 +10,19 @@ test('empty expecter passes everything', t => { t.true(createSatisfier({}).test({ a: [1, true, 'a'] })) t.true(createSatisfier({}).test({ a: { b: 'a' } })) t.true(createSatisfier({}).test([{}, { a: 1 }])) + t.false(createSatisfier({}).test(null)) + t.false(createSatisfier({}).test(undefined as any)) }) +test('expect null to pass only null', t => { + t.true(createSatisfier(null).test(null)) + t.false(createSatisfier(null).test(undefined as any)) + t.false(createSatisfier(null).test(0)) + t.false(createSatisfier(null).test(false)) + t.false(createSatisfier(null).test('')) +}) + + test('mismatch value fails', t => { t.false(createSatisfier({ a: 1 }).test({ a: 2 })) t.false(createSatisfier({ a: true }).test({ a: false })) diff --git a/src/createSatisfier.ts b/src/createSatisfier.ts index 486d725..31bf6ee 100644 --- a/src/createSatisfier.ts +++ b/src/createSatisfier.ts @@ -98,7 +98,7 @@ function detectDiff(actual, expected, path: string[] = []) { else { // expected is object. If actual is not, then it is diff. const actualType = typeof actual - if (actualType === 'boolean' || actualType === 'string' || actualType === 'number') + if (actualType === 'boolean' || actualType === 'string' || actualType === 'number' || actual === undefined || actual === null) diff.push({ path, expected,