Skip to content

Commit cf280ab

Browse files
committed
feat(unregistered): threw an error to give fast feedback when using before registering
1 parent 5878f0d commit cf280ab

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/container.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ export function register(name, dependency) {
55
}
66

77
export function use(name) {
8+
if (!dependencies[name]) throw new Error('Attempted to use a dependency that has not been registered');
9+
810
return dependencies[name];
911
}

test/unit/container-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import any from '@travi/any';
33
import {register, use} from '../../src/container';
44

55
suite('ioc container', () => {
6-
it('should store dependency instances', () => {
6+
test('that dependencies are stored and are retrievable by name', () => {
77
const dependency = any.simpleObject();
88
const name = any.string();
99

1010
register(name, dependency);
1111

1212
assert.equal(use(name), dependency);
1313
});
14+
15+
test('that an error is throw when attemting to retrieve a dependency that has not been registered', () => {
16+
assert.throws(() => use(any.word()), 'Attempted to use a dependency that has not been registered');
17+
});
1418
});

0 commit comments

Comments
 (0)