This repository has been archived on 2025-10-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
lilbetter.js/test/try.test.js
Gustavo "Guz" L. de Mello ae87a2da7b feat!: 💥 invert order of array
2024-01-18 18:50:50 -03:00

36 lines
1.1 KiB
JavaScript

/* eslint-disable import/no-relative-parent-imports */
// eslint-disable-next-line n/no-unpublished-import
import { describe, it } from 'vitest';
import { tryA, tryS } from '../src/index.js';
describe.concurrent('Return values', () => {
it('JSON parsing [Sync, Success]', ({ expect }) => {
const [json, error] = tryS(JSON.parse)('{ "hello": "world" }');
expect(error).toBe(null);
expect(json).toEqual({ hello: 'world' });
});
it('JSON parsing [Sync, Error]', ({ expect }) => {
const [json, error] = tryS(JSON.parse)('{ "hello: "world" }');
expect(error?.name).toEqual('SyntaxError');
expect(error).toBeInstanceOf(Error);
expect(json).toBe(undefined);
});
it('Fetch function [Async, Success]', async ({ expect }) => {
const [res, error] = await tryA(fetch)('https://example.com');
expect(error).toBe(null);
expect(res?.status).toBe(200);
});
it('Fetch function [Async, Error]', async ({ expect }) => {
const [res, error] = await tryA(fetch)('htps://example.com');
expect(error?.name).toEqual('TypeError');
expect(error).toBeInstanceOf(Error);
expect(res).toBe(undefined);
});
});