Fix and Type Tests (and some type errors I found along the way)#6003
Fix and Type Tests (and some type errors I found along the way)#6003nabbydude wants to merge 24 commits intoianstormtaylor:mainfrom
Conversation
…s into a top level dir
🦋 Changeset detectedLatest commit: 9f9d348 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
I've created a PR against your PR branch to improve the tests you mentioned: nabbydude#1 |
Personally, I like the intentionality of always returning a boolean. It means you can't accidentally do something meaningless like For coercing values of type
Agreed. Needing to figure out where to put and how to name a new test file with the current pattern is a huge disincentive to writing tests.
Definitely. I quite like Vitest, but switching everything to use Jest would also be nice. |
Co-authored-by: Joe Anderson <joe@anderbell.studio>
|
ran in to some stumbling blocks while writing new tests for the main PR I'm working on, going to take a sec to disentangle the tweaks and push here, will convert to draft in meantime |
|
@12joan does this still need changes? |
Description
This only changes types, test files, and configs.
Currently opening up anything JSX in a test directory highlights all the tags with errors. This fixes that and types all mocha test-runner files (which was necessary to solve the issue). Also consolidates most of the boilerplate between packages to top level files and JSX props have now been more definitively typed.
Also addresses some bugs I found along the way:
In
slate/srcEditor#setSelectionwas typed to allow Partial Ranges when it should be allowing any Partial Selection (which matters if Selection is CustomTyped to something beyond Range). This means it allowsnullnow but it already handles that as a noop. Functionality is unchanged.In
slate/testmatchpredicates potentially returning undefined. (should matches be altered to accept this?)packages/slate/test/interfaces/Editor/levels/match.tsxwas using non-existent propertywithMatch,I fixed it to be an actual match function but if it was still passing anyways this is probably a bad test.@12joan improved the test to be more meaningful. Thanks!packages/slate/test/interfaces/Node/elements/path.tsxandpackages/slate/test/interfaces/Node/elements/range.tsxare using options ofNode.elementsthat as far as I can tell never existed? The tests and the method they're testing have been unchanged since Next #3093.Disabled them until proper tests can be written.Also improved by @12joan. Thanks x2as constcasts.slate-hyperscript) instead of the instance intest/index.tsContext
I tried to avoid touching test files as much as possible to keep file changes to a minimum. EDIT: I had to alter imports for ~70 files so this is less true but ah well I still tried 😅
TSConfigs
I've added
tsconfigs to eachtestdirectory so vscode and tsc can understand where to find types for checking purposes, they all inherit from a new fileconfig/typescript/tsconfig.test.json, which has thenoEmitflag set because mocha and jest can handle their own transpilation.Types
support/types.tscontains the JSX definitions andCustomTypesextensions for nodes etc used in testing. Arbitrary properties used in testing have been added here, including every single letter as a flag, since I saw that used a lot. in the future I'd like to make this more robust, maybe using string template types as keys, something like so:to allow for arbitrary properties that are still typed.
The actual
test/interfaces/CustomTypessuite had to be excluded from thetsconfigto avoid conflicts with the rest of the tests' types. It still runs in mocha just fine and it doesn't use any JSX so it seems to be happy in its own little corner.Types in Libs
Enabled
skipLibCheckin the base tsconfig to skip typechecking for dependencies. Should save some time and also it was giving me a strange error (claiming a package referenced by a package didnt have typedefs for a particular export when we clearly had the @types package for it and it definitely was exporting that type), I assume this was just some yarn pnp strangeness, it was a type-only import so I'm happy to write it off as not our problem. (@testing-library/jest-domimportingtype ARIARolefromaria-query, for anyone searching for the same issue in the future)Implicit Any
Basically every one of these files has a function with an untyped parameter, which strict typescript doesn't like. To avoid changing nearly every single test file I just set the flag to allow this.
Implicit Any, JSX Edition
Turns out all JSX tags return the same type, no matter what the tag or its property is and no matter what the JSX function it gets compiled into is typed to return. This is frustratingly a limitation of typescript as a language.
This means the best we can type JSX values is as
Node(and even that is assuming there's never a top-level selection tag). This would cause more errors as they try to get passed as more specific kinds like Text or Element but are too wide. So I have left them asanyfor now as well to avoid writing casts/typeguards in many more files.Test Utils
JSX Tags with attributes
void,inline,readOnly, andnonSelectablewill create nodes that respond true to those respective functions. This was already the case but I made it attach during creation instead of being injected right before the test is run..spec.ts files
I added an exception to the
fixturesfunction (which crawls for test files and run them), when it encounters a file ending in.spec.(extension)it will load it without wrapping it in a test, so that we can define our own suites and tests inside the files. I'm doing that now for tests I'm writing.This scheme allows tests with the existing system to coexist with ones that want to group multiple tests together without having to add things to the index file.
Future
My focus for this PR was just getting types to work and not actually altering tests in a meaningful way, but someday™️ I'd love to improve upon them since it seems like (as is the case for many projects) they're pretty incomplete and even the ones we have aren't always the best. I'm gonna ramble about a wishlist but my thoughts are worth the same two cents as anyone else's:
{ input, run, output }paradigm but (afaik) there's no way to make those values typesafe and what qualifies as theinputand what just gets created insiderunseems v unclear and arbitraryslate-domcurrently has no tests at all.slate-reactuses jest while the other packages use mocha, would love to consolidate thisexpect(x).toEqual(y)paradigm but tbf it's really just sugar on top of assert (jest has it built in or chai provides it for mocha)Checks
yarn test.yarn lint. (Fix errors withyarn fix.)yarn start.)yarn changeset add.)