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

Research and implement a testing strategy for types provided through @carbon/react #16361

Open
4 tasks
tay1orjones opened this issue May 8, 2024 · 4 comments
Open
4 tasks

Comments

@tay1orjones
Copy link
Member

tay1orjones commented May 8, 2024

Background

Once #12513 is fully complete, it would be beneficial if we had a way to be more confident in future changes to the component types. Specifically to ensure that types provided through @carbon/react follow the established Semantic Versioning guidelines for the package.

Important

Types will continue to be provided on an as-is basis and remain exempt from semver for the forseeable future as we establish a more robust framework around maintaining types. Completing the work in this issue does not mean that we're able to bound types to semver.

Click to expand details

#### A change is made to component typings/definitions
semver bump: **minor**
Typescript definitions for component APIs are provided on an as-is basis and are
not bound to semver. This includes type additions, changes, or removals.
Breaking changes may happen across patch or minor versions. The component API
may shift and types could become outdated.
If a typing is out of date or incorrect, please let us know by opening an issue
or even better, propose a fix via PR! We appreciate the community's involvement
in this effort to keep component types up to date and as correct as possible!

Potential approaches

Snapshot the type interfaces

For component proptypes we have a very helpful setup where the proptypes for all components are collected and snapshotted into a single file, PublicAPI-test.js.snap. As changes to proptypes are made, the snapshot requires being updated, which shows up in the PR diff. This makes it really clear what's changing and makes it much easier to spot potential issues. We could attempt to mirror this setup in some way to provide more explicit visibility on every PR when a potentially semver-incompatible change is made to component types. The prominent solution for this would be to use https://api-extractor.com/

Implicitly test types in storybook

If we converted our story files to TypeScript we'd be "testing" the component types. It wouldn't cover all use cases but it would be a quick way to get closer to how an end user/consumer actually uses the types. It should only be slightly more overhead to write story files in typescript, and could encourage greater TypeScript familiarity amongst our community contributors.

Directly test the types

This approach has probably the most overhead. Options to research include:

Additional concerns

  • We should try to avoid having to manually write and maintain tests, instead favoring automation where possible
  • Would be awesome to have as much coverage as possible, including testing generics, even though they may only be rarely used in the codebase.
  • ...

Related resources

Tasks

Preview Give feedback
@mrazauskas
Copy link

@tay1orjones It is really nice to see TSTyche in the list. I am its author. Long story short, I was writing and contributing type tests to Jest repo for some time. At the moment there are more than 1000 assertions. Working at this scale was a pain with the available tooling. That is why I wrote TSTyche.

For instance, at large scale there was a need to have describe() and test() helpers with .skip and .only. That’s a must for deducing an issue or building new feature. Also we wanted to test on different versions of TypeScript easily. TSTyche can do all this and much more. All Jest’s type test now are ran by TSTyche.

Differently from all other type testing tools, TSTyche is wrapping around TypeScript’s language service. It allows per file queries instead of working with the whole TS program. And more. For example, another feature I am thinking about is an API which makes possible testing in memory generate files, i.e. passing arbitrary text to the compiler and asking for diagnostics (and/or processing type test assertions).

If this sounds like the automation solution you are looking for, I am more than interested to collaborate. The implementation on TSTyche side is rather trivial. The fs host object must be passed to the language service factory. Obviously, that can be a virtual fs which is generating test files on demand.

To be honest, I never had a chance to work with component type tests. And that sounds even more interesting ;D

@mrazauskas
Copy link

Earlier today TSTyche 2.1 shipped with new .toAcceptProps() matcher that is designed to test well.. prop types of JSX components! Thanks for inspiration.

@tay1orjones
Copy link
Member Author

@mrazauskas Thanks for popping in here! The features you listed are definitely intriguing and something we'll consider when we take up this work. toAcceptProps in particular could be really valuable.

Copy link
Contributor

Thank you for submitting a feature request. Your proposal is open and will soon be triaged by the Carbon team.

If your proposal is accepted and the Carbon team has bandwidth they will take on the issue, or else request you or other volunteers from the community to work on this issue.

@sstrubberg sstrubberg moved this from Triage to Next ➡ in Roadmap Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Status: Next ➡
Development

No branches or pull requests

3 participants