Skip to content

Commit

Permalink
Merge pull request #6 from ikenox/add-test-cases
Browse files Browse the repository at this point in the history
Add test cases
  • Loading branch information
ikenox committed Oct 17, 2023
2 parents fceb6a1 + 397a7cd commit 823faa9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const singletonContainer = singletonScope.instanciate({});
// Define another scope. You can specify scope-specific parameter. As an example,
// here is `{ request: Request }`.
const requestScope = scope<{ request: Request }>()
// Provide other-scoped dependencies in this scope together
// Note that the merged singleton-scoped dependencies are still singleton
.static(singletonContainer)
.provide({
// Define request-scoped dependencies.
Expand Down
2 changes: 2 additions & 0 deletions src/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const singletonContainer = singletonScope.instanciate({});
// Define another scope. You can specify scope-specific parameter. As an example,
// here is `{ request: Request }`.
const requestScope = scope<{ request: Request }>()
// Provide other-scoped dependencies in this scope together
// Note that the merged singleton-scoped dependencies are still singleton
.static(singletonContainer)
.provide({
// Define request-scoped dependencies.
Expand Down
26 changes: 19 additions & 7 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,32 @@ test('cache', ({ expect }) => {
expect(container1.num).not.toBe(container2.num);
});

test("container instantiation processses doen't evaluate a passed another container dependency instances", ({
expect,
}) => {
describe('lazy evaluation', () => {
const container = scope()
.provide({
dep: () => {
depA: () => {
throw new Error('this code should never be called');
},
depB: () => 123,
})
.instanciate({});

expect(() => {
scope().static(container).instanciate({});
}).not.toThrowError();
test('dependencies are not evaluated when merged by `static` method', ({
expect,
}) => {
expect(() => {
scope().static(container).instanciate({});
}).not.toThrowError();
});

test('unspecified variable is not evaluated on object destruction', ({
expect,
}) => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { depB } = container;
}).not.toThrowError();
});
});

describe('type-level tests', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class ContainerScope<
/**
* Merges external static objects (mainly other container instances) to provide
* via the container. It allows the container to provide other-scoped container's
* dependency instances together. The merged container keeps original scope.
* dependency instances together.
* The merged container keeps original scope, meaning that a singleton-scoped
* container which is merged into request-scoped keeps singletion.
*/
static<P extends Record<string, unknown> & WithoutReserved<Instances>>(
p: P
Expand Down

0 comments on commit 823faa9

Please sign in to comment.