feat: add generic parameter for IEventPublisher in EventBus - #1930
feat: add generic parameter for IEventPublisher in EventBus#1930lucas-gregoire wants to merge 3 commits into
Conversation
04605e0 to
c41d5b4
Compare
|
@copilot resolve the merge conflicts in this pull request |
4632dad to
7a0859d
Compare
| Query, | ||
| QueryBus, | ||
| } from '../../src'; | ||
| import { expectTypeOf } from 'vitest'; |
There was a problem hiding this comment.
To draw your attention, it seems like vitest ignores this import. Try including a failing assertion (like expectTypeOf(false).toBeNumber();) in this file. Run the test from command line and it will pass.
The feature is marked experimental. It is not working as expected rather often. Here similar findings: ngrx/platform#5148 (comment) or flint-fyi/flint#2398 (comment).
There was a problem hiding this comment.
Thanks @mrazauskas, you were right, and it was worse: the assertions were never
checked at all. expectTypeOf(false).toBeNumber() passed with 44/44 green.
Digging in, both causes turn out to be documented behaviour rather than bugs:
typecheck.enabled
defaults tofalse, and nothing else in the pipeline rantscovertest/,
sincenpm run buildonly coverssrc.typecheck.include
defaults to'**/*.{test,spec}-d.?(c|m)[jt]s?(x)', so a*.spec.tsfile is
simply never a type test. The run still printedType Errors no errors.typecheck.tsconfig
"tries to find closest tsconfig.json", which here is the root one, whose
node10resolution can't read Vitest 4's ESM-only type exports. With
ignoreSourceErrors: false
the run then exits 1 on 5 source errors while also printing
Type Errors no errors.
So in this case it was our configuration rather than the feature misbehaving,
though I agree the reporting makes it very easy to miss, which is your point.
Fixed along the documented path: renamed to generics.spec-d.ts so it matches
the default include, set typecheck: { enabled: true, tsconfig: 'test/tsconfig.json' }, and moduleResolution: "bundler" in the test tsconfig.
And since the guide states that "Vitest
doesn't run these files, they are only statically analyzed by the compiler", I
dropped the try/catch + expect(true).toBeTruthy() scaffolding and the runtime
bus instances with it.
Verified by mutation rather than by a green run: forcing EventBus#publish back
to any in src/ now fails with 2 type errors, as does a wrong expectTypeOf
or a removed @ts-expect-error.
|
some new merge conflicts popped up after merging your other PR |
The expectTypeOf assertions in generics.spec.ts were never verified. They
are a no-op at runtime, and nothing in the pipeline ran tsc over test/, so
a deliberately wrong assertion like expectTypeOf(false).toBeNumber() passed
with 44/44 green.
Enabling Vitest's typecheck is not sufficient on its own either: its default
include only matches *.{test,spec}-d.ts, so *.spec.ts files are silently
skipped, and it falls back to the root tsconfig, whose "node10" resolution
cannot read the ESM-only type exports of Vitest 4 / Vite 8.
- rename generics.spec.ts to generics.spec-d.ts so it is picked up, and drop
the try/catch/finally + expect(true).toBeTruthy() scaffolding, which
asserted nothing at runtime
- enable typecheck in vitest.config.e2e.ts, pointed at test/tsconfig.json
- set moduleResolution "bundler" for the test tsconfig
Verified by mutation: a wrong expectTypeOf, a removed @ts-expect-error, and
a return-type regression on EventBus#publish each fail the e2e run.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
7a0859d to
e27d3ad
Compare
|
@kamilmysliwiec conflicts resolved, rebased on I also fixed @mrazauskas' review point along the way: the e2e type assertions |
Yes, all is just about configuration. Although the tool is reporting tests as passing, it does nothing in fact. For me, this looks rather dangerous. Any accidental change in the future will not be caught. As another example, nestjs/graphql#3936 is similarly adding type tests that do not run, but the report is all green again. |


PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently, the
EventBus.publishandEventBus.publishAllmethods always returnany, whereas they could infer types from thepublishandpublishAllmethods of theIEventPublisherinterface.What is the new behavior?
This PR introduces a new generic parameter for the
EventBuswhich represents the type of the event publisher (extendingIEventPublisher), allowing to infer the return types of thepublishandpublishAllmethods.If the event publisher does not have a
publishAllmethod—like in the case ofDefaultPubSub—then the return type ofpublishAllis an array of thepublishresults from the publisher. This clarifies the behavior of thepublishAllmethod.By default,
publishstill returnsanywhilepublishAllnow returnsany[]which better matches the returned array.Does this PR introduce a breaking change?
publishAllnow returnsany[]instead ofanyby default. I think this is a "soft" breaking change.For this breaking change, here are the situations to consider:
publishAllwithout using its result: no change.publishAll:publishAllmethod:publishAllmethod:publishAll: this cast will still compile but could be replaced by the inference allowed by the new generic parameter.Personally, I think this is a breaking change with not much impact.
Other information
I also added some missing generics tests on query handlers.