πŸ”₯ TypeScript type assertion plugin for vitest

Overview

TypeScript type assertion plugin for vitest.

vite-plugin-vitest-typescript-assert : TypeScript assertion (test) 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 or give feedback if you encounter any issues. Thank you.

Installation

Install the dependencies.

pnpm add -D vitest typescript vite-plugin-vitest-typescript-assert
# yarn and npm also works

Setup the plugin in vitest.config.ts file.

import { defineConfig } from 'vite';
import { vitestTypescriptAssertPlugin } from 'vite-plugin-vitest-typescript-assert';

export default defineConfig({
  plugins: [vitestTypescriptAssertPlugin()],
});

Assertions APIs

This plugin was more than inspired by the tsd project, much of the assertion logic comes from their code.

That's why two APIs are available:

// Exactly the same behaviour as tsd (plus any bugs I might have introduced πŸ™ˆ)
import * as tsd from 'vite-plugin-vitest-typescript-assert/tsd';
// 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.

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

expectType<ExpectedType>(value);
expectNotType<ExpectedType>(value);
expectAssignable<ExpectedType>(value);
expectNotAssignable<ExpectedType>(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.

expectType<string>().assignableTo<string>();
expectType<string>().assignableTo('nyan');
expectType('nyan').assignableTo<string>();
expectType('nyan').assignableTo('nyan');
expectType<ExpectedType>().assignableTo(value);
expectType<ExpectedType>().identicalTo(value);
expectType<ExpectedType>().subtypeOf(value);
expectType<ExpectedType>().equalTo(value);
expectType<ExpectedType>().toBeDeprecated();
expectType<ExpectedType>().toThrowError();
expectType<ExpectedType>().toThrowError(code);
expectType<ExpectedType>().toThrowError(message);

expectType<ExpectedType>().not.assignableTo(value);
expectType<ExpectedType>().not.identicalTo(value);
expectType<ExpectedType>().not.subtypeOf(value);
expectType<ExpectedType>().not.equalTo(value);
expectType<ExpectedType>().not.toBeDeprecated();

Options

vitestTypescriptAssertPlugin(options?: PluginOptions);

By default the plugin applies the following configuration.

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.
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

You might also like...

Nuxt3 template with Windicss + Pinia + Vitest + Playwright

Nuxt3 template with Windicss + Pinia + Vitest + Playwright

Nuxt 3 Template Nuxt 3 template repository Look at the nuxt 3 documentation to learn more. Used Technologies Usage Setup Make sure to install the depe

Dec 14, 2022

A testing focused Remix Stack, that integrates E2E & Unit testing with Playwright, Vitest, MSW and Testing Library. Driven by Prisma ORM. Deploys to Fly.io

A testing focused Remix Stack, that integrates E2E & Unit testing with Playwright, Vitest, MSW and Testing Library. Driven by Prisma ORM. Deploys to Fly.io

Live Demo Β· Twitter A testing focused Remix Stack, that integrates E2E & Unit testing with Playwright, Vitest, MSW and Testing Library. Driven by Pris

Oct 31, 2022

Nuxt.js 3 x Histoire x Vitest x VitePress x Turbo (pnpm)

Turborepo nuxt starter This is a monorepo with Nuxt, Histoire, Vitest & VitePress as a starter for any project that can be easily extended. You can al

Dec 19, 2022

Type predicate functions for checking if a value is of a specific type or asserting that it is.

As-Is Description As-Is contains two modules. Is - Type predicates for checking values are of certain types. As - Asserting values are of a certain ty

Feb 10, 2022

🧬 A type builder for pagination with prisma and type-graphql.

🧬 Prisma TypeGraphql Pagination Prisma TypeGraphql Pagination builds prisma pagination types for type-graphql. import { ... } from 'type-graphql'

Apr 21, 2022

100% type-safe query builder for node-postgres :: Generated types, call any function, tree-shakable, implicit type casts, and more

⚠️ This library is currently in alpha. Contributors wanted! tusken Postgres client from a galaxy far, far away. your database is the source-of-truth f

Dec 29, 2022

Simple, lightweight at-runtime type checking functions, with full TypeScript support

pheno Simple, lightweight at-runtime type checking functions, with full TypeScript support Features Full TypeScript integration: TypeScript understand

Sep 5, 2022

A Brainf*ck interpreter built in the TypeScript type system.

Brainf*ck Interpreter in the TypeScript type system Just another thing that no one asked for. Why? I love messing with the TypeScript type system. Som

Dec 17, 2022

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

Dec 7, 2022
Comments
  • Reported errors don't agree with TypeScript IDE / CLI

    Reported errors don't agree with TypeScript IDE / CLI

    Describe the bug

    CleanShot 2022-09-05 at 17 10 33@2x

    In the above you can see that the reported error is not reported in the IDE and I know it to be correct (e.g. this would pass in Jest before etc.).

    Reproduction

    Run tests on this branch https://github.com/jasonkuhrt/alge/tree/tests/try-vite-ts-plugin

    System Info

    Node 18, vitest 0.23
    

    Used Package Manager

    pnpm

    Validations

    • [X] Follow our Code of Conduct
    • [X] Read the Contributing Guide.
    • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
    • [X] Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
    • [X] The provided reproduction is a minimal reproducible of the bug.
    bug 
    opened by jasonkuhrt 7
  • ESM support

    ESM support

    When using this plugin in a project specifying "type": "module" in its package.json, no tests can be run due to the following error:

    import { isMatch } from "micromatch";
             ^^^^^^^
    SyntaxError: Named export 'isMatch' not found. The requested module 'micromatch' is a CommonJS module, which may not support all module.exports as named exports.
    CommonJS modules can always be imported via the default export, for example using:
    
    import pkg from 'micromatch';
    const { isMatch } = pkg;
    

    You can find a repro of the issue in this repository.

    released 
    opened by HiDeoo 2
  • `esModuleInterop` support

    `esModuleInterop` support

    When using this plugin in a project not specifying in its tsconfig.json the esModuleInterop option or setting it to false, no tests can be run due to the following error:

    FAIL  index.test.ts [ index.test.ts ]
    TypeError: Module '"chai"' has no exported member 'default'.
    

    You can find a repro of the issue in this repository.

    released 
    opened by HiDeoo 2
Releases(v1.1.4)
Hemsida fΓΆr personer i Sverige som kan och vill erbjuda boende till mΓ€nniskor pΓ₯ flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo fΓΆr kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
🐬 A simplified implementation of TypeScript's type system written in TypeScript's type system

?? HypeScript Introduction This is a simplified implementation of TypeScript's type system that's written in TypeScript's type annotations. This means

Ronen Amiel 1.8k Dec 20, 2022
Combine type and value imports using Typescript 4.5 type modifier syntax

type-import-codemod Combines your type and value imports together into a single statement, using Typescript 4.5's type modifier syntax. Before: import

Ian VanSchooten 4 Sep 29, 2022
A type programming language which compiles to and interops with type-level TypeScript

Prakaar Prakaar (hindi for "type") is a type programming language which compiles to and interops with type-level TypeScript. Prakaar itself is also a

Devansh Jethmalani 17 Sep 21, 2022
A transpiler from golang's type to typescript's type for collaboration between frontend & backend.

go2type go2type.vercel.app (backup site) A typescript transpiler that convert golang's type to typescript's type. Help front-end developer to work fas

Oanakiaja 8 Sep 26, 2022
Vitest for coc.nvim

coc-vitest Vitest for coc.nvim Install vim-plug: Plug 'yaegassy/coc-vitest', {'do': 'yarn install --frozen-lockfile'} CocInstall: Not supported at thi

yaegassy 8 Sep 4, 2022
Remix + Cloudflare Workers + Wrangler2 + Tailwind + ESLint + Prettier + Vitest + Playwright

Welcome to Remix! Remix Docs Development You will be running two processes during development: The Miniflare server (miniflare is a local environment

null 48 Dec 19, 2022
Custom Vitest matchers to test the state of the DOM, forked from jest-dom.

vitest-dom Custom Vitest matchers to test the state of the DOM This library is a fork of @testing-library/jest-dom. It shares that library's implement

Chance Strickland 14 Dec 16, 2022
πŸš€ A boilerplate with generic configurations to a Nextjs project with bun, vitest, cicd and etc

?? Next.JS Template with Linter ?? Tools: NextJS Typescript ESLint (Code Pattern) Prettier (Formatter) Husky (Pre-commit) Vitest (Unit/Integration Tes

Rodrigo Victor 8 Dec 18, 2022