Skip to content

Commit

Permalink
Add missing export, add asMockValue (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Aug 21, 2024
1 parent 2000442 commit cbc5ecf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Note that this will resolve all non-disabled dependencies within the container,

## Mocking dependencies

Sometimes you may want to intentionally inject objects that do not fully conform to the type definition of an original class. For that you can use `asMockClass` or `asMockFunction` resolvers:
Sometimes you may want to intentionally inject objects that do not fully conform to the type definition of an original class. For that you can use `asMockClass`, `asMockFunction` or `asMockValue` resolvers:

```ts
type DiContainerType = {
Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export {
AwilixManager,
asMockClass,
asMockFunction,
asMockValue,
getByPredicate,
eagerInject,
asyncInit,
asyncDispose,
Expand Down
6 changes: 6 additions & 0 deletions lib/awilixManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
type DisposableResolver,
asClass,
asFunction,
asValue,
} from 'awilix'
import type { FunctionReturning } from 'awilix/lib/container'
import type { Resolver } from 'awilix/lib/resolvers'

declare module 'awilix' {
interface ResolverOptions<T> {
Expand Down Expand Up @@ -36,6 +38,10 @@ export function asMockClass<T = object>(
return asClass(Type as Constructor<T>, opts)
}

export function asMockValue<T = object>(value: unknown): Resolver<T> {
return asValue(value as T)
}

export function asMockFunction<T = object>(
fn: FunctionReturning<unknown>,
opts?: BuildResolverOptions<T>,
Expand Down
26 changes: 26 additions & 0 deletions test/awilixManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AwilixManager,
asMockClass,
asMockFunction,
asMockValue,
asyncDispose,
asyncInit,
getByPredicate,
Expand Down Expand Up @@ -140,6 +141,31 @@ describe('asMockFunction', () => {
})
})

describe('asMockValue', () => {
it('Supports passing a mock instance that does not fully implement the real class', () => {
type DiContainerType = {
asyncInitClass: AsyncInitClass
asyncInitClass2: AsyncInitClass
}
const diConfiguration: NameAndRegistrationPair<DiContainerType> = {
asyncInitClass: asClass(AsyncInitClass),
asyncInitClass2: asMockValue(new AsyncDisposeClass()),
}

const diContainer = createContainer<DiContainerType>({
injectionMode: 'PROXY',
})

for (const [dependencyKey, dependencyValue] of Object.entries(diConfiguration)) {
diContainer.register(dependencyKey, dependencyValue as Resolver<unknown>)
}

const { asyncInitClass, asyncInitClass2 } = diContainer.cradle
expect(asyncInitClass).toBeInstanceOf(AsyncInitClass)
expect(asyncInitClass2).toBeInstanceOf(AsyncDisposeClass)
})
})

describe('awilixManager', () => {
describe('constructor', () => {
it('throws an error if strictBooleanEnforced is set and undefined is passed', () => {
Expand Down

0 comments on commit cbc5ecf

Please sign in to comment.