-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to break cyclic dependency with
optional()
restrict resolving `optional()` dependency in a constructor
- Loading branch information
Showing
21 changed files
with
267 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/@wroud/di-tools-analyzer/src/isOptionalDependency.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { isOptionalDependency } from "./isOptionalDependency.js"; | ||
import { all, createService, optional, single } from "@wroud/di"; | ||
|
||
describe("isOptionalDependency", () => { | ||
it("should detect optional resolver", async () => { | ||
const service = createService("Service"); | ||
expect(isOptionalDependency(optional(service))).toBe(true); | ||
expect(isOptionalDependency(single(service))).toBe(false); | ||
expect(isOptionalDependency(all(service))).toBe(false); | ||
}); | ||
it("should detect optional resolver in combined resolver", async () => { | ||
const service = createService("Service"); | ||
|
||
expect(isOptionalDependency(all(optional(service)))).toBe(true); | ||
expect(isOptionalDependency(optional(all(service)))).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/@wroud/di/src/di/ServiceProvider.optional.development.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
///<reference types="@wroud/tests-runner/jest-extended.d.ts"/> | ||
import { describe, expect, it } from "vitest"; | ||
import { ServiceContainerBuilder } from "./ServiceContainerBuilder.js"; | ||
import { createService } from "./createService.js"; | ||
import { createSyncMockedService } from "../tests/createSyncMockedService.js"; | ||
import { optional } from "../service-type-resolvers/optional.js"; | ||
import "../debugDevelopment.js"; | ||
|
||
describe("ServiceProvider development", () => { | ||
it("should not resolve from constructor", () => { | ||
const AService = createService<InstanceType<typeof A>>("A"); | ||
const BService = createService<InstanceType<typeof B>>("B"); | ||
|
||
const A = createSyncMockedService("A", () => []); | ||
const B = createSyncMockedService( | ||
"B", | ||
() => [optional(AService)], | ||
(a) => { | ||
a.resolve(); | ||
}, | ||
); | ||
|
||
const serviceProvider = new ServiceContainerBuilder() | ||
.addSingleton(AService, A) | ||
.addSingleton(BService, B) | ||
.build(); | ||
|
||
expect(() => serviceProvider.getService(BService)).toThrowError( | ||
"Optional service A cannot be resolved during construction of service B. Please convert it to a regular dependency.", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.