Skip to content

Commit

Permalink
test(core): add tests for autoloaded circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Nov 14, 2024
1 parent e17daed commit 7d0fa02
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ describe('Knifecycle', () => {
assert.deepEqual(Object.keys(dependencies), ['hash', 'ENV']);
});

test('and instanciate services once', async () => {
test('and instantiate services once', async () => {
$.register(
initializer(
{
Expand Down Expand Up @@ -1370,6 +1370,64 @@ describe('Knifecycle', () => {
}
});

test('when autoloaded dependencies are circularly dependent', async () => {
$.register(
service(
async () => {
return 'mainService';
},
'mainService',
['parentService1'],
),
);
$.register(
service(
async () => {
return 'parentService1';
},
'parentService1',
['parentService2'],
),
);
$.register(
initializer(
{
type: 'service',
name: '$autoload',
inject: ['?ENV', '?log'],
singleton: true,
},
async () => async (serviceName) => ({
path: `/path/of/${serviceName}`,
initializer: initializer(
{
type: 'service',
name: serviceName,
inject: ['parentService1'],
},
async () => `THE_${serviceName.toUpperCase()}:` + serviceName,
),
}),
),
);

try {
await $.run<any>(['test', 'log']);
throw new YError('E_UNEXPECTED_SUCCESS');
} catch (err) {
assert.equal((err as YError).code, 'E_BAD_AUTOLOADED_INITIALIZER');
assert.deepEqual((err as YError).params, ['parentService2']);
assert.equal(
((err as YError).wrappedErrors[0] as YError).code,
'E_CIRCULAR_DEPENDENCY',
);
assert.deepEqual(
((err as YError).wrappedErrors[0] as YError).params,
['parentService2', 'parentService1', 'parentService2'],
);
}
});

test('when the autoloader returns bad data', async () => {
$.register(
initializer(
Expand Down

0 comments on commit 7d0fa02

Please sign in to comment.