Code Monkey home page Code Monkey logo

vite-plugin-vitest-typescript-assert's Introduction

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

vite-plugin-vitest-typescript-assert's People

Contributors

skarab42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vite-plugin-vitest-typescript-assert's Issues

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.

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

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.