UNPKG

as-procedure

Version:

easily create procedures within a pit-of-success

49 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const getError_1 = require("@ehmpathy/error-fns/dist/getError"); const test_fns_1 = require("test-fns"); const withExpectOutkey_1 = require("./withExpectOutkey"); describe('withExpectOutkey', () => { (0, test_fns_1.given)('a procedure which looks like a typical api caller', () => { const getMiracleBySlug = async (input) => { if (input.slug === 'nulled') return { miracle: null, success: true }; return { miracle: { slug: 'slug', name: 'name' }, success: true }; }; (0, test_fns_1.when)('wrapped withExpectOutkey', () => { const getMiracleBySlugWrapped = (0, withExpectOutkey_1.withExpectOutkey)(getMiracleBySlug); (0, test_fns_1.then)('should typecheck that the key is a valid key of the output', async () => { // "anything" should not be a valid key await (0, test_fns_1.getError)(() => getMiracleBySlugWrapped({ slug: 'junk-removal', }, {}).expect( // @ts-expect-error: Argument of type '"anything"' is not assignable to parameter of type '"miracle" | "success"'.ts(2345) 'anything', 'isPresent')); // but "miracle" should be a valid key const { miracle } = await getMiracleBySlugWrapped({ slug: 'junk-removal', }, {}).expect('miracle', 'isPresent'); expect(miracle).toBeDefined(); }); (0, test_fns_1.then)('should be possible to get cause message and stack', async () => { const error = await (0, test_fns_1.getError)(() => getMiracleBySlugWrapped({ slug: 'nulled', }, {}).expect('miracle', 'isPresent')); if (error instanceof getError_1.NoErrorThrownError) throw error; const words = { errorMessage: error.message, errorTrace: error.stack, causeMessage: error.cause instanceof Error ? error.cause.message : undefined, causeTrace: error.cause instanceof Error ? error.cause.stack : undefined, }; expect(words.causeTrace).toBeDefined(); }); }); }); }); //# sourceMappingURL=withExpectOutkey.test.js.map