chore: introduce TSTyche for type testing#6532
Conversation
| expectAssignable<FastifyInstance>(fastify().register(import('./dummy-plugin.mjs'))) | ||
| expectAssignable<FastifyInstance>(fastify().register(import('./dummy-plugin.mjs'), { foo: 1 })) |
There was a problem hiding this comment.
After tsconfig.json was added, TypeScript requires ESM file to be imported.
| expectAssignable<ServerWithTypeProviderAndLogger>( | ||
| // @ts-expect-error | ||
| serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger) | ||
| ) | ||
| // @ts-expect-error No overload matches this call. | ||
| serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger) |
There was a problem hiding this comment.
Same as other // @ts-expect-error tests above. expectAssignable() seems to be redundant.
| expectType<((...args: [payload: string]) => typeof res)>(res.code(200).send) | ||
| expectType<((...args: [payload: number]) => typeof res)>(res.code(400).send) | ||
| expectType<((...args: [payload: { error: string }]) => typeof res)>(res.code(500).send) | ||
| expectError<(payload?: unknown) => typeof res>(res.code(200).send) |
There was a problem hiding this comment.
The exact types was tested few lines above.
| expectType<((...args: [payload: string]) => typeof res)>(res.code(200).send) | ||
| expectType<((...args: [payload: number]) => typeof res)>(res.code(400).send) | ||
| expectType<((...args: [payload: { [x: string]: unknown; error?: string }]) => typeof res)>(res.code(500).send) | ||
| expectError<(payload?: unknown) => typeof res>(res.code(200).send) |
There was a problem hiding this comment.
The exact types was tested few lines above.
| '@typescript-eslint/no-unused-expressions': 'off', | ||
| '@typescript-eslint/no-unused-vars': 'off' |
There was a problem hiding this comment.
These are added only temporary. I plan to remove them after migrating the assertions. For instance, introducing .toHaveProperty() in three places allows enabling no-unused-expressions.
| // preClose hook types should be exported correctly https://github.com/fastify/fastify/pull/5335 | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| preCloseAsyncHookHandler, | ||
| preCloseHookHandler |
There was a problem hiding this comment.
I included tests for both preClose* types, so they are not reported as unused vars by ESLint anymore.
Eomm
left a comment
There was a problem hiding this comment.
Pinged the TS team
LGTM - let's remove tsd after this PR
Signed-off-by: mrazauskas <[email protected]>
|
I wanted to let you know that all Fastify libraries and plugins have been migrated from I think we can merge this PR so that we can proceed with the complete removal of tsd from this repository as well, since there is also this PR: #6525 🔥 The only PR left to merge is this one regarding the Fastify CLI: fastify/fastify-cli#886 🔥 |
- Replace tsd with tstyche for type testing - Update test:types script to use tstyche - Convert initialization.test-d.ts to initialization.tst.ts - Update index.d.ts to use ES module export syntax PR of Reference: - fastify/fastify#6532 - fastify/fastify#6525
- Replace tsd with tstyche for type testing - Update test:types script to use tstyche - Convert initialization.test-d.ts to initialization.tst.ts - Update index.d.ts to use ES module export syntax PR of Reference: - fastify/fastify#6532 - fastify/fastify#6525
- Replace tsd with tstyche for type testing - Update test:types script to use tstyche - Convert initialization.test-d.ts to initialization.tst.ts - Update index.d.ts to use ES module export syntax PR of Reference: - fastify/fastify#6532 - fastify/fastify#6525
* refactor(types): migrate from tsd to tstyche - Replace tsd with tstyche for type testing - Update test:types script to use tstyche - Convert initialization.test-d.ts to initialization.tst.ts - Update index.d.ts to use ES module export syntax PR of Reference: - fastify/fastify#6532 - fastify/fastify#6525 * refactor(types): add toBeCallableWith assertion for app.register * refactor(types): add route-level constraints assertion * refactor(types): add child.get constraints assertion * Apply suggestions from code review Co-authored-by: Tom Mrazauskas <[email protected]> * Apply suggestions from code review Co-authored-by: Tom Mrazauskas <[email protected]> * Update test/types/initialization.tst.ts * fix(scripts): update test command to run all test files * refactor(types): update type definitions and test initialization --------- Co-authored-by: Tom Mrazauskas <[email protected]>
Context #6033
This PR is introducing TSTyche for testing TypeScript types.
I have a branch that is replacing
tsdwithtstychehere #6525. To be honest, I wanted to look through it once again, but that is too massive to review.This is clean up PR that allows to run the same type tests files using
tsdandtstyche.Changes
expectError()is replaced withexpectNotAssignable()or// @ts-expect-errortsdsexpectError()is provided a type argument (likeexpectError<string>("abc")), it catches assignability error and therefore it is replaced withexpectNotAssignable()for better readability// @ts-expect-errorfollowed by an error message, these are checked (and required!) by TSTycheexpectNotDeprecated()is removed. It does nothing. All exported types and expressions would pass this test.Other Changes
test/types/import.ts, it was testing thatFastifyListenOptionsandFastifyLogFnare exported. I aded tests forFastifyListenOptionstotest/types/instance.test-d.tsandFastifyLogFnis already tested intest/types/logger.test-d.tsandtest/types/request.test-d.tstest:typescriptscript is renamed totest:typesChecklist
npm run test && npm run benchmark --if-presentand the Code of conduct