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

Improve support for mocking dependencies in DI #32

Merged
merged 4 commits into from
Apr 24, 2024

Conversation

kibertoad
Copy link
Owner

No description provided.


declare module 'awilix' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface ResolverOptions<T> {
asyncInit?: boolean | string
asyncInitPriority?: number // lower means it gets initted earlier
asyncDispose?: boolean | string | ((instance: T) => Promise<unknown>)
asyncDispose?: boolean | string | (<U extends T>(instance: U) => Promise<unknown>)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change it wasn't possible to pass a class that would extend the original class as a resolver override.

E. g. something like this:

consumerErrorResolver: asClass(FakeConsumerErrorResolver, SINGLETON_CONFIG),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I was just writing up a repro for this! https://codesandbox.io/p/devbox/awilix-ts-interface-j5sll2

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wahahaha, awesome!

[Key in keyof TDependencies]: Resolver<TDependencies[Key]>
}

export function asMockClass<T = object>(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for cases when we know that passed class does not fully implement the original class contract, and we are fine with that.

@kibertoad kibertoad merged commit 2946185 into main Apr 24, 2024
6 checks passed
@kibertoad kibertoad deleted the fix/support-subclasses branch April 24, 2024 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants