Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
OliverJAsh opened this issue Sep 9, 2019 · 10 comments
Open

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

OliverJAsh opened this issue Sep 9, 2019 · 10 comments
Labels
enhancement New feature or request

Comments

@OliverJAsh
Copy link

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?

@SamVerschueren
Copy link
Collaborator

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.

@OliverJAsh
Copy link
Author

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

@treybrisbane
Copy link

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?)

@ebroder
Copy link

ebroder commented Feb 14, 2020

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)

@treybrisbane
Copy link

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.

peterMuriuki added a commit to onaio/reveal-frontend that referenced this issue Apr 2, 2020
- Adds the folder test-d/ to contain the types
- we are using tsd to check the types
- Added a test:types script to package.json
- Apparently we need a root index.d.ts for tsd to work
  tsdjs/tsd#32 (comment)
peterMuriuki added a commit to onaio/reveal-frontend that referenced this issue Apr 3, 2020
- Adds the folder test-d/ to contain the types
- we are using tsd to check the types
- Added a test:types script to package.json
- Apparently we need a root index.d.ts for tsd to work
  tsdjs/tsd#32 (comment)
peterMuriuki added a commit to onaio/reveal-frontend that referenced this issue Apr 14, 2020
- Adds the folder test-d/ to contain the types
- we are using tsd to check the types
- Added a test:types script to package.json
- Apparently we need a root index.d.ts for tsd to work
  tsdjs/tsd#32 (comment)
@anilanar
Copy link

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
Copy link
Contributor

papb commented May 11, 2020

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

@UselessPickles
Copy link

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.

@molszanski
Copy link

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

@penx
Copy link

penx commented Mar 3, 2022

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);
  }}
/>

badeball added a commit to badeball/cypress-cucumber-preprocessor that referenced this issue Apr 27, 2023
Some of this was remnants of the past. Some was me not quite
understanding script vs. module. Some was me misunderstanding what was
actually going on in Tsd.

It turns out that Tsd was just incidentally working, but I was never
using it according to their intentions. You don't really test *.ts files
with Tsd [1], but I happen to place compile output along side of them,
hence "incidentally working".

Now I am merely relying on TS compiler's diagnostic output, which is
really what Tsd does anyway.

[1] tsdjs/tsd#32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants