Skip to content

Commit

Permalink
fix: Make auto-register work in already built packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Iku-turso committed Nov 17, 2022
1 parent fa9f961 commit 35ed955
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatMap, forEach, tap } from 'lodash/fp';
import { flatMap, forEach, tap, set } from 'lodash/fp';
import { pipeline } from '@ogre-tools/fp';
import { isInjectable } from '@ogre-tools/injectable';
import requireContextFake from './requireContextFake';
Expand Down Expand Up @@ -34,7 +34,7 @@ const verifyInjectables = ([[fileName, module]]) => {

export default ({ fs, path }) =>
({ di, targetModule, getRequireContexts }) => {
if (!targetModule.require.context) {
if (targetModule.require && !targetModule.require.context) {
targetModule.require.context = requireContextFake({
targetModule: targetModule,
fs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ describe('autoRegister', () => {
expect(actual).toBe('some-instance');
});

it('given operating in already built package, injects', () => {
const injectableStub = getInjectable({
id: 'some-injectable',
instantiate: () => 'some-instance',
});

const di = createContainer('some-container');

autoRegister({
di,
// Note: require being undefined in module implies package being already built.
targetModule: { require: undefined, path: '/some-module-path' },

getRequireContexts: () => [
// Note: in packages that are already built, require contexts are inlined.
getRequireContextStub({ default: injectableStub }),
],
});

const actual = di.inject(injectableStub);

expect(actual).toBe('some-instance');
});

it('given injectable file with no injectables, when auto-registering, throws', () => {
const requireContextStub = Object.assign(
() => ({
Expand Down

0 comments on commit 35ed955

Please sign in to comment.