UNPKG

type-fns

Version:

A set of types, type checks, and type guards for simpler, safer, and easier to read code.

49 lines 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test_fns_1 = require("test-fns"); const withAssure_1 = require("./withAssure"); describe('asAssure', () => { it('should be able to assure for a specific check', () => { const isAString = (input) => typeof input === 'string'; const assure = (0, withAssure_1.asAssure)(isAString); const shouldBeString = assure('hi'); const error = (0, test_fns_1.getError)(() => { const shouldThrowBeforeHere = assure(7); }); expect(error.message).toContain(`input does not satisfy type.check 'isAString'`); }); it('should be able to assure for a generic check', () => { const isNotNull = (input) => input !== null; const assure = (0, withAssure_1.asAssure)(isNotNull); const shouldBeString = assure('hi'); const shouldBeNumber = assure(7); const error = (0, test_fns_1.getError)(() => { const shouldThrowBeforeHere = assure(null); }); expect(error.message).toContain(`input does not satisfy type.check 'isNotNull'`); }); }); describe('withAssure', () => { it('should be able to add assure to a specific check', () => { const isAString = (0, withAssure_1.withAssure)((input) => typeof input === 'string', { name: 'isAString' }); const shouldBeString = isAString.assure('hi'); const error = (0, test_fns_1.getError)(() => { const shouldThrowBeforeHere = isAString.assure(7); }); expect(error.message).toContain(`input does not satisfy type.check 'isAString'`); }); it.skip('should be able to assure for a generic check', () => { const isNotNullOrig = (input) => input !== null; const isNotNull = (0, withAssure_1.withAssure)(isNotNullOrig); // @ts-expect-error // todo: fix this once typescript enables passing through generic types. (today for generic, ReturnType<AssureMethod> works but ReturnType<{ assure: AssureMethod }> does not, leading this to fail) const shouldBeString = isNotNull.assure('hi'); // @ts-expect-error // todo: fix this once typescript enables passing through generic types. (today for generic, ReturnType<AssureMethod> works but ReturnType<{ assure: AssureMethod }> does not, leading this to fail) const shouldBeNumber = isNotNull.assure(7); const error = (0, test_fns_1.getError)(() => { // @ts-expect-error // todo: fix this once typescript enables passing through generic types. (today for generic, ReturnType<AssureMethod> works but ReturnType<{ assure: AssureMethod }> does not, leading this to fail) const shouldThrowBeforeHere = isNotNull.assure(null); }); expect(error.message).toContain(`input does not satisfy type.check 'isNotNull'`); }); }); //# sourceMappingURL=withAssure.test.js.map