Skip to content

Commit

Permalink
fix(di): resolve LazyInject returning undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewthigpenbylight committed Sep 13, 2024
1 parent 1098fe7 commit c1bd482
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/di/src/common/decorators/lazyInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ describe("LazyInject", () => {

expect(lazyService).toEqual({});
});

it("should not return undefined if the package is imported but the bean has not been assigned yet", async () => {
@Injectable()
class MyInjectable {
@LazyInject("MyLazyModule", () => import("./__mock__/lazy.import.module"))
lazy: Promise<MyLazyModule>;
}

const injector = new InjectorService();
const service = await injector.invoke<MyInjectable>(MyInjectable);
const originalLazyInvoke = injector.lazyInvoke.bind(injector);
const promise1 = service.lazy;
let promise2: Promise<MyLazyModule> | undefined;
vi.spyOn(injector, "lazyInvoke").mockImplementationOnce((token) => {
promise2 = service.lazy;
return originalLazyInvoke(token);
});

const lazyService1 = await promise1;
const lazyService2 = await promise2;

expect(lazyService1).not.toBeUndefined();
expect(lazyService2).not.toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion packages/di/src/common/decorators/lazyInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function LazyInject(
injectProperty(target, propertyKey, {
resolver(injector) {
return async () => {
if (!token) {
if (!token || !bean) {
const exports = await importPackage(packageName, resolver, optional);
token = exports[key];

Expand Down

0 comments on commit c1bd482

Please sign in to comment.