Last active
October 16, 2022 09:01
-
-
Save janhesters/01c3940a0e11a65b9ecda055a1b0ffab to your computer and use it in GitHub Desktop.
Vitest + RITEway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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