Writing tests in typescript #2736
Replies: 6 comments
-
I have no objection to this in principle, but presumably we would want some standardized test harness to run the TypeScript tests rather than rolling our own. I presume there is no such thing as mocha-ts or the like? Presuming not, is there a de facto standard typescript test harness and how bad would the switchover be, or would we just run both harnesses and gradually move individual files from one to the other as they were touched? If we went this path would we be essentially obliged to eliminate the uses of 'any' in the current type declarations for clean testing, and if so, is there much hope for removing those 'any's? As always, my comments/thoughts on TypeScript matters are relatively uninformed by real-world TypeScript experience. |
Beta Was this translation helpful? Give feedback.
-
We wouldn't use a separate test harness, we'd still use mocha. I think there are basically two possible approaches: (1) compile the ts to js, be sure to include sourcemaps, watch for changes, run mocha via its normal CLI / nodeJS on the generated code. Or (2) use mocha --require ts-node/register --extensions ts,tsx --watch --watch-files src 'tests/**/*.{ts,tsx}' [...args] (2) seems like a good option for MathJS. [Aside: I don't think (1) would be that hard, but why bother. It'd be different if src/ was written in typescript and we had to compile to JS anyway. Then we'd have to have all that infrastructure in place.]
No... TS will basically let you do anything with an explicit any. We'd continue to get little type assurance from them, but that's already true. We would get new errors if the test file imports things that don't have type declarations... but then we'd type them! |
Beta Was this translation helpful? Give feedback.
-
Note that for running index.ts as we currently do (no mocha) we couldn't get ts-node to work, but rather ended up with
Well, but what I really meant is that I for one would not be very comfortable with any warnings in mathjs's main suite of unit tests, although ultimately of course that's for @josdejong to call. So I'd then strongly recommend some scheme to ensure there were no warnings in the bulk of unit tests, and I was worried that might entail purging all of the 'any's. |
Beta Was this translation helpful? Give feedback.
-
It makes sense to me @ChristopherChudzicki , thanks for starting this discussion. I see two challenges:
I'm not sure though if this currently can work out nicely, so an experiment to try it out would be great. Shall we keep the |
Beta Was this translation helpful? Give feedback.
-
An experiment definitely seems like a good idea idea. Re
One reason I think this could be worthwhile is that I think it would actually reduce the number of things you have to do to add a function. In particular, I think separate tests for the types and the runtime behavior would no longer be necessary. If the runtime functionality is tested in typescript, then we're also testing the type declarations for that behavior. At least I suspect this is mostly true. There may be some cases where |
Beta Was this translation helpful? Give feedback.
-
@ChristopherChudzicki Potentially relevant discussion: #2537. I was considering breaking index.ts and index.d.ts apart as part of that PR but decided to hold off. Maybe we can collaborate after it's merged. |
Beta Was this translation helpful? Give feedback.
-
In #2544 (comment) @gwhitney pointed out that
index.d.ts
andindex.ts
are becoming rather large. Settingindex.d.ts
aside (admittedly, the problem is worse in that file!), the comment got me thinking... maybe it would be beneficial to just convert (gradually) thetests/
directory to typescript. Some benefits:index.ts
file (doesn't help withindex.d.ts
)Would need to:
tests/*.ts
... That should be straightforward after Linting: StandardJS for src/, test/; Prettier for types/ #2544Beta Was this translation helpful? Give feedback.
All reactions