Skip to content

Instantly share code, notes, and snippets.

@janhesters
Last active October 16, 2022 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janhesters/01c3940a0e11a65b9ecda055a1b0ffab to your computer and use it in GitHub Desktop.
Save janhesters/01c3940a0e11a65b9ecda055a1b0ffab to your computer and use it in GitHub Desktop.
Vitest + RITEway
import { describe } from 'vitest';
import { assert } from './riteway';
describe('assert.only', () => {
assert({
given: 'this is a failing test',
should: 'be skipped because another test is here with .only',
actual: true,
expected: false,
});
assert.only({
given: 'this is a passing test with .only',
should: 'be the only test that runs in this describe block',
actual: true,
expected: true,
});
});
import { faker } from '@faker-js/faker';
import { describe, vi, vitest } from 'vitest';
import { assert } from './riteway';
const asyncInc = (n: number) => Promise.resolve(n + 1);
const wait = (ms: number) =>
new Promise<number>(resolve => setTimeout(() => resolve(ms), ms));
describe('assert', () => {
assert({
given: 'true',
should: 'be true',
actual: true,
expected: true,
});
assert.skip({
given: 'assert is called with .skip',
should: 'skip the test',
actual: true,
expected: false,
});
assert.todo({
given: 'assert is called with .todo',
should: 'skip the test and mark it as todo',
actual: true,
expected: false,
});
});
describe('async assert', async () => {
const result = await asyncInc(41);
assert({
given: 'the await is done before the assert',
should: 'execute the code correctly',
actual: result,
expected: 42,
});
assert({
given: 'the await is done inline within the assert',
should: 'execute the code correctly',
actual: await asyncInc(41),
expected: 42,
});
vi.useFakeTimers();
const time = faker.datatype.number() + 5000;
// eslint-disable-next-line testing-library/await-async-utils
const waitPromise = wait(time);
vitest.advanceTimersByTime(time);
assert({
given: 'doing other vitest things like messing with the timers',
should: 'still assert the correct thing',
actual: await waitPromise,
expected: time,
});
vi.useRealTimers();
});
import { expect, test } from 'vitest';
interface Assertion<T> {
readonly given: string;
readonly should: string;
readonly actual: T;
readonly expected: T;
}
export function assert<T>(assertion: Assertion<T>) {
test(`${assertion.given}: ${assertion.should}`, () => {
expect(assertion.actual).toEqual(assertion.expected);
});
}
assert.todo = function todo<T>(assertion: Assertion<T>) {
test.todo(`${assertion.given}: ${assertion.should}`, () => {
expect(assertion.actual).toEqual(assertion.expected);
});
};
assert.only = function only<T>(assertion: Assertion<T>) {
test.only(`${assertion.given}: ${assertion.should}`, () => {
expect(assertion.actual).toEqual(assertion.expected);
});
};
assert.skip = function skip<T>(assertion: Assertion<T>) {
test.skip(`${assertion.given}: ${assertion.should}`, () => {
expect(assertion.actual).toEqual(assertion.expected);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment