Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getClassElementMetadataFromLegacyMetadata #75

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-clouds-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": patch
---

Updated `getClassMetadata` to correctly fetch name and target names
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { beforeAll, describe, expect, it } from '@jest/globals';

import { ServiceIdentifier } from '@inversifyjs/common';

import { InversifyCoreError } from '../../error/models/InversifyCoreError';
import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind';
import {
INJECT_TAG,
MULTI_INJECT_TAG,
Expand Down Expand Up @@ -38,11 +40,12 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => {
});

it('should throw an Error', () => {
const expectedErrorProperties: Partial<Error> = {
const expectedErrorProperties: Partial<InversifyCoreError> = {
kind: InversifyCoreErrorKind.missingInjectionDecorator,
message: 'Expected @inject, @multiInject or @unmanaged metadata',
};

expect(result).toBeInstanceOf(Error);
expect(result).toBeInstanceOf(InversifyCoreError);
expect(result).toStrictEqual(
expect.objectContaining(expectedErrorProperties),
);
Expand Down Expand Up @@ -121,12 +124,13 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => {
});

it('should throw an Error', () => {
const expectedErrorProperties: Partial<Error> = {
const expectedErrorProperties: Partial<InversifyCoreError> = {
kind: InversifyCoreErrorKind.missingInjectionDecorator,
message:
'Expected a single @inject, @multiInject or @unmanaged metadata',
};

expect(result).toBeInstanceOf(Error);
expect(result).toBeInstanceOf(InversifyCoreError);
expect(result).toStrictEqual(
expect.objectContaining(expectedErrorProperties),
);
Expand Down Expand Up @@ -225,15 +229,15 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => {
value: 'customTagValue',
};
nameMetadataFixture = {
key: NAME_TAG,
key: NAMED_TAG,
value: 'name-fixture',
};
optionalMetadataFixture = {
key: OPTIONAL_TAG,
value: true,
};
targetNameMetadataFixture = {
key: NAMED_TAG,
key: NAME_TAG,
value: 'target-name-fixture',
};
metadataListFixture = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LazyServiceIdentifier, ServiceIdentifier } from '@inversifyjs/common';

import { InversifyCoreError } from '../../error/models/InversifyCoreError';
import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind';
import {
INJECT_TAG,
MULTI_INJECT_TAG,
Expand Down Expand Up @@ -39,19 +41,22 @@ export function getClassElementMetadataFromLegacyMetadata(
}

if (multiInjectMetadata === undefined && injectMetadata === undefined) {
throw new Error('Expected @inject, @multiInject or @unmanaged metadata');
throw new InversifyCoreError(
InversifyCoreErrorKind.missingInjectionDecorator,
'Expected @inject, @multiInject or @unmanaged metadata',
);
}

const nameMetadata: LegacyMetadata | undefined = metadataList.find(
(metadata: LegacyMetadata): boolean => metadata.key === NAME_TAG,
(metadata: LegacyMetadata): boolean => metadata.key === NAMED_TAG,
);

const optionalMetadata: LegacyMetadata | undefined = metadataList.find(
(metadata: LegacyMetadata): boolean => metadata.key === OPTIONAL_TAG,
);

const targetNameMetadata: LegacyMetadata | undefined = metadataList.find(
(metadata: LegacyMetadata): boolean => metadata.key === NAMED_TAG,
(metadata: LegacyMetadata): boolean => metadata.key === NAME_TAG,
);

const managedClassElementMetadata: ManagedClassElementMetadata = {
Expand Down Expand Up @@ -90,7 +95,8 @@ function getUnmanagedClassElementMetadata(
multiInjectMetadata: LegacyMetadata | undefined,
): UnmanagedClassElementMetadata {
if (multiInjectMetadata !== undefined || injectMetadata !== undefined) {
throw new Error(
throw new InversifyCoreError(
InversifyCoreErrorKind.missingInjectionDecorator,
'Expected a single @inject, @multiInject or @unmanaged metadata',
);
}
Expand Down