npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-vitest-typescript-assert

v1.1.4

Published

🔥 TypeScript type assertion plugin for vitest.

Downloads

184

Readme

📌 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 '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 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