tsdjs/tsd

How to write tests for source code in .ts files?

OliverJAsh opened this issue ยท 10 comments

The docs provide an example of writing a test against a declaration file, but what if you want to instead write a test against a source .ts file? I tried doing this but I couldn't get it to work. Is this not yet supported?

So what you want is actually write a test which verifies that a certain function call only accepts an array of strings and nothing else?

That's not yet supported right now. The main focus is type definitions but I don't see a reason why this wouldn't be supported.

Correct @SamVerschueren. I have .ts files which I want to write type tests for. I could of course generate .d.ts files (using the declaration compiler option) and then test them, but that adds extra work and we would lose some nice dev UX for our tests (such as "go to definition").

I too came here looking for this functionality. ๐Ÿ™‚
Is this on the roadmap at all? I'm not aware of any other libraries for testing types in .ts files that offer the kinds of assertions tsd does, so it's been really great if this was possible.

(As a sidenote, it actually wasn't clear to me that this library is only for .d.ts files. Perhaps we could make that clearer in the readme?)

For what it's worth, I was able to work around this by creating an empty index.d.ts file to pacify tsd and then creating test files that imported the actual code I wanted to test. Seems to be working for me, although +1 to wanting native support. In my case, I'm doing some complicated stuff with mapped types in the actual code, and I want to be able to verify that they're behaving in the way I expect (c.f. MongoProjection.ts and tests for that file)

Something that may also help here is more flexibility around specifying which .test-d.ts files should be run. Perhaps just do what Ava does and allow for a glob pattern?

Definitely a separate issue though.

I develop libraries that are mostly about introducing advanced type safety features by making use of compiler inference. I'm using dtslint and got fed up with weird file structure requirements and worarounds, insufficient type equality checks etc. I found tsd hoping I can replace dtslint.

papb commented

I suggest using expect-type to write tests for .ts files. Simple and great.

I'm also trying to use tsd to test non-trivial generics/types in the source code of a project written in TypeScript. It would be nice if tsd directly supported an option to NOT require a .d.ts file, and NOT assume that it should try to load the file specified by package.json's "types" field (because that refers to a generated compiler output file for my project that either doesn't exist or is outdated at the time I attempt to run tsd).

But for now, I have come up with a hackish workaround.

WORKAROUND!!!

In my project, I added a dummy nested "project" directory named tsd_project. This directory contains a completely empty dummy.d.ts file and a package.json file that contains only this:

{
    "types": "dummy.d.ts",
    "tsd": {
        "directory": "../src"
    }
}

Then my script in my real package.json for running tsd references that fake project directory:

{
   "scripts": {
       "tsd": "tsd tsd_project"
   }
}

This satisfies tsd's current requirement to work in terms of a .d.ts file as specified by package.json, while looking for test files (.test-d.ts) in my project's src directory, so I can place test files adjacent to corresponding source files. Within my test files, I import directly from my source files with relative paths.

@UselessPickles, thank you! This worked for me!!

penx commented

I'd love to be able to use tsd to test polymorphic types in React components, with code written in TypeScript and without too much hacking.

Here's a quick use case of the kind of thing I'd like to be able to write:

import {MouseEvent as ReactMouseEvent} from 'react';
import {expectType} from 'tsd';
import {Button} from './Button';

<Button
  onClick={e => {
    expectType<ReactMouseEvent<HTMLButtonElement, MouseEvent>>(e);
  }}
/>

<Button
  as="a"
  onClick={e => {
    expectType<ReactMouseEvent<HTMLAnchorElement, MouseEvent>>(e);
  }}
/>