Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skarab42/vite-plugin-vitest-typescript-assert
🔥 TypeScript type assertion plugin for vitest
https://github.com/skarab42/vite-plugin-vitest-typescript-assert
assert assertion expect test type-check type-checking typescript vite vitest
Last synced: 3 months ago
JSON representation
🔥 TypeScript type assertion plugin for vitest
- Host: GitHub
- URL: https://github.com/skarab42/vite-plugin-vitest-typescript-assert
- Owner: skarab42
- License: mit
- Created: 2022-07-13T15:15:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-07T02:03:00.000Z (over 1 year ago)
- Last Synced: 2024-09-24T07:45:57.259Z (4 months ago)
- Topics: assert, assertion, expect, test, type-check, type-checking, typescript, vite, vitest
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/vite-plugin-vitest-typescript-assert
- Size: 473 KB
- Stars: 79
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
TypeScript type assertion plugin for vitest.
> 📌 This plugin is in **alpha** version, and will probably stay that way for a long time, it lacks tests (a bit ironic) and real documentation! **But I can't stop myself from publishing it**. Moreover this plugin is a succession of tricks to integrate it to vitest (it would be incredible that vitest offer an official plugin system 💜). But it **seems to work** for my use cases and maybe **for you too?** I'm thinking of continuing to evolve it if I need it or if others start using it. Feel free to [contribute](contributing) or [give feedback](https://github.com/skarab42/vite-plugin-vitest-typescript-assert/issues) if you encounter any issues. **Thank you**.
# Installation
Install the dependencies.
```bash
pnpm add -D vitest typescript vite-plugin-vitest-typescript-assert
# yarn and npm also works
```Setup the plugin in `vitest.config.ts` file.
```ts
import { defineConfig } from 'vitest/config';
import { vitestTypescriptAssertPlugin } from 'vite-plugin-vitest-typescript-assert';export default defineConfig({
plugins: [vitestTypescriptAssertPlugin()],
});
```# Assertions APIs
This plugin was more than inspired by the [tsd](https://github.com/SamVerschueren/tsd) project, much of the assertion logic comes from their code.
That's why two APIs are available:
```ts
// Exactly the same behaviour as tsd (plus any bugs I might have introduced 🙈)
import * as tsd from 'vite-plugin-vitest-typescript-assert/tsd';
``````ts
// A more descriptive/flexible API with some additions.
import * as tssert from 'vite-plugin-vitest-typescript-assert/tssert';
```Named imports, alias imports and named exports are supported in both API.
```ts
import { expectType } from 'vite-plugin-vitest-typescript-assert/tsd';
import { expectType as assertType } from 'vite-plugin-vitest-typescript-assert/tsd';export { expectType as assertType } from 'vite-plugin-vitest-typescript-assert/tsd';
```## tsd [docs](https://github.com/SamVerschueren/tsd#assertions)
```ts
expectType(value);
expectNotType(value);
expectAssignable(value);
expectNotAssignable(value);
expectError(value);
expectDeprecated(expression);
expectNotDeprecated(expression);
printType(expression);
```## tssert
`tssert` supports 4 signatures for each method. You can either specify the type in the generic or the value in the first argument, but never both at the same time. But don't worry, if this happens the plugin will send you an error.
```ts
expectType().assignableTo();
expectType().assignableTo('nyan');
expectType('nyan').assignableTo();
expectType('nyan').assignableTo('nyan');
``````ts
expectType().assignableTo(value);
expectType().identicalTo(value);
expectType().subtypeOf(value);
expectType().equalTo(value);
expectType().toBeDeprecated();
expectType().toThrowError();
expectType().toThrowError(code);
expectType().toThrowError(message);expectType().not.assignableTo(value);
expectType().not.identicalTo(value);
expectType().not.subtypeOf(value);
expectType().not.equalTo(value);
expectType().not.toBeDeprecated();
```# Options
```ts
vitestTypescriptAssertPlugin(options?: PluginOptions);
```By default the plugin applies the following configuration.
```ts
const defaultOptions: PluginOptions = {
report: ['type-error', 'type-assertion'],
include: ['**/*.test.ts'],
exclude: [],
typescript: {
configName: 'tsconfig.json',
searchPath: process.cwd(),
compilerOptions: {},
},
};
```- `type-error`: Report the errors raised by the TS compiler.
- `type-assertion`: Report the errors raised by the assertion library.```ts
export interface PluginOptions {
report?: 'type-error' | 'type-assertion';
include?: string | string[];
exclude?: string | string[];
typescript?: TypeScriptConfigOptions;
}interface TypeScriptConfigOptions {
configName?: string;
searchPath?: string;
compilerOptions?: ts.CompilerOptions;
}
```# Contributing 💜
See [CONTRIBUTING.md](https://github.com/skarab42/vite-plugin-vitest-typescript-assert/blob/main/CONTRIBUTING.md)