Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Global types are breaking type tets #358

Closed
Ver0s opened this issue Sep 25, 2023 · 4 comments
Closed

Global types are breaking type tets #358

Ver0s opened this issue Sep 25, 2023 · 4 comments

Comments

@Ver0s
Copy link

Ver0s commented Sep 25, 2023

Hi, first of all I wanted to say thank you for the hard work on the library! 馃槂

At our project we wanted to introduce type testing using tsd-lite along with tsd-runner-jest. We did a basic setup according to the docs to make a sort of MVP. The setup is mirrored in this repro repo.

The problem that we encountered is that types from the global scope seem to break the type tests. In this repro we're creating a type test but also importing a function to test its signature. This function is using a global type.

After running npx jest --config jest.config.tsd.js you can see that type ThisIsNull cannot be found, but it is used in the id function. The test would also break if we used ThisIsNull directly in the type tests since it would not
be recognized unless it's explicitly imported (which would make it non-global).

Steps to reproduce:

  1. Clone the repo and install dependencies - git clone followed by npm i
  2. Run the tests - npx jest --config jest.config.tsd.js
  3. Check the result

Actual result:

Expected result:
ThisIsNull type should be known to the runner when importing the id function, error shouldn't be thrown

The question

So we wanted to ask whether we can do something about it right now, maybe we missed something? And if we can - would it require us to get rid of global types or do some hacks to get around problems surrounding them? Or maybe that problem is unsolvable for the time being?

Please let us know, thanks for any help in advance!

CC @erheron

@mrazauskas
Copy link
Owner

Thanks for reaching out!

Checked your reproduction. Feels like something is wrong on tsd-lite side, because tsc compiles the same file with no issues.

Hm.. Interesting what is going on here. I will try to find time to think about it and to dig around.


Actually! A better solution could be simply to wait for a brand new type testing tool, which I mentioned here: jest-community/jest-runner-tsd#32.

Just checked it, your reproduction is passing without any issues using this runner. It is near ready to be published. Should be out in 4-6 weeks.

@mrazauskas
Copy link
Owner

mrazauskas commented Sep 25, 2023

Quick workaround. Add the following to the test file:

import '../types.d.ts';

tsd-lite does not see files which are not imported. It only picks compilerOptions from tsconfig.json and does not know anything more about files around. That鈥檚 a limitation unfortunately. But it is fixed in the new runner which will replace tsd-lite very soon (;

@Ver0s
Copy link
Author

Ver0s commented Sep 25, 2023

Thanks for such a quick response and explanation!

It's good to know that we had a good intuition and it's actually an existing limitation. We'll wait for the new runner - amazing work by the way! I'll leave it up to you to decide whether to close or keep this issue open but I personally think it's good to keep it open :)

@mrazauskas
Copy link
Owner

TSTyche, the testing tool I promised, is released. For details, see jest-community/jest-runner-tsd#32 (comment)

Thanks for raising the issue. It gave me another bit of motivation to move on with TSTyche. Give it a try, please.

It works as expect with case you have described here. So I can close now (;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants