Skip to content
Discussion options

You must be logged in to vote

This is working https://stackblitz.com/edit/vitest-dev-vitest-bjrcixdf?file=test%2Frepro.test.ts

import { expect, test } from 'vitest';

test('foo(3)', () => {
  const result = foo(3);
  expect.assert(result.ok);
  expect(result.value).toBe(7);
});

test('foo(∞)', () => {
  const result = foo(Infinity);
  expect.assert(!result.ok);
  expect(result.error.message).toMatch(/infinite/);
});

type FooResult = { ok: true; value: number } | { ok: false; error: Error };

function foo(n: number): FooResult {
  return Number.isFinite(n)
    ? { ok: true, value: 2 * n + 1 }
    : { ok: false, error: new Error('n is infinite') };
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@jamesarosen
Comment options

Answer selected by jamesarosen

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants