type-to-fast-check
v0.0.3
Published
Automatically convert TypeScript types to fast-check arbitraries!
Maintainers
Readme
[!NOTE]
This package is in a beta state and I'm excited for you to try it out!
I'd love your feedback! Share any suggestions, bug reports, feature requests, or general thoughts by filing an issue.
Install
$ npm i --save-dev type-to-fast-check fast-checkUsage
import * as fc from 'fast-check'
import typeToArb from 'type-to-fast-check'
type User = {
firstName: string
lastName: string
}
const userArb = typeToArb<User>()
fc.assert(
fc.property(userArb, user => {
// ...
}),
)The following code:
const userArb = typeToArb<User>()Compiles to:
const userArb = fc.record({
firstName: fc.string(),
lastName: fc.string(),
})You can pass any type to typeToArb!
Configure
Add the transformer plugin to your
tsconfig.json:{ "compilerOptions": { "plugins": [{ "transform": "type-to-fast-check/transformer" }] } }Set up
ts-patchto apply the configured plugin:Install:
$ npm i --save-dev ts-patchRun for the first time:
$ ts-patch installConfigure to run after
npm install:package.json
{ "scripts": { "prepare": "ts-patch install -s" } }
Configure your test runner to run
tscif it doesn't by default:vitest(usesesbuildand doesn't runtsc)Install Rollup plugin:
npm i --save-dev @rollup/plugin-typescriptAdd the plugin to your
vitest.config.ts:vitest.config.ts
import typescript from '@rollup/plugin-typescript' import { defineConfig } from 'vitest/config' export default defineConfig({ // ... plugins: [typescript()], })
Send me PRs for configuring other test runners!
Roadmap
See the issue tracker! Share any suggestions, bug reports, feature requests, or general thoughts by filing an issue.
Contributing
Stars are always welcome!
For bugs and feature requests, please create an issue.
