From 5cf91708a2758e35558e83961191fcf5249f6691 Mon Sep 17 00:00:00 2001 From: Vittorio Date: Sun, 16 Feb 2020 12:46:05 +0000 Subject: [PATCH 1/2] feat(extendsMappedType): make sure extending mapped types do not prevent to compile --- .../descriptor/mock/mockProperties.ts | 4 +++- .../descriptor/extends/mappedTypes.test.ts | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/transformer/descriptor/extends/mappedTypes.test.ts diff --git a/src/transformer/descriptor/mock/mockProperties.ts b/src/transformer/descriptor/mock/mockProperties.ts index 752fa9c05..555ba3218 100644 --- a/src/transformer/descriptor/mock/mockProperties.ts +++ b/src/transformer/descriptor/mock/mockProperties.ts @@ -8,7 +8,9 @@ import { PropertyLike } from './propertyLike'; import { SignatureLike } from './signatureLike'; export function GetMockPropertiesFromSymbol(propertiesSymbol: ts.Symbol[], signatures: ReadonlyArray, scope: Scope): ts.Expression { - const properties: PropertyLike[] = propertiesSymbol.map((prop: ts.Symbol) => prop.declarations[0]) as PropertyLike[]; + const properties: PropertyLike[] = propertiesSymbol + .filter((prop: ts.Symbol) => !!prop.declarations) // Dynamically generated properties (mapped types) do not have declarations + .map((prop: ts.Symbol) => prop?.declarations[0]) as PropertyLike[]; const signaturesDeclarations: SignatureLike[] = signatures.map((signature: ts.Signature) => signature.declaration) as SignatureLike[]; diff --git a/test/transformer/descriptor/extends/mappedTypes.test.ts b/test/transformer/descriptor/extends/mappedTypes.test.ts new file mode 100644 index 000000000..a40015638 --- /dev/null +++ b/test/transformer/descriptor/extends/mappedTypes.test.ts @@ -0,0 +1,22 @@ +import {createMock} from 'ts-auto-mock'; + +describe('extends MappedTypes', () => { + it('should not convert the properties', () => { + enum SOME_ENUM { + FIRST = 'FIRST', + SECOND = 'SECOND', + } + + + interface IBase { + propertyA: string; + propertyB: number; + } + + interface InterfaceWithExtends extends Record {} + + const type: InterfaceWithExtends = createMock(); + expect(type.FIRST).toBeUndefined(); + expect(type.SECOND).toBeUndefined(); + }); +}); From f32d3719f2675f22a0ea135e290cdd967a457b97 Mon Sep 17 00:00:00 2001 From: Vittorio Date: Sun, 16 Feb 2020 14:10:25 +0000 Subject: [PATCH 2/2] feat(extendsMappedType): add extends mapped type to not supported types --- .../descriptor/mock/mockProperties.ts | 2 +- ui/src/views/types-not-supported.mdx | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/transformer/descriptor/mock/mockProperties.ts b/src/transformer/descriptor/mock/mockProperties.ts index 555ba3218..8169878ef 100644 --- a/src/transformer/descriptor/mock/mockProperties.ts +++ b/src/transformer/descriptor/mock/mockProperties.ts @@ -10,7 +10,7 @@ import { SignatureLike } from './signatureLike'; export function GetMockPropertiesFromSymbol(propertiesSymbol: ts.Symbol[], signatures: ReadonlyArray, scope: Scope): ts.Expression { const properties: PropertyLike[] = propertiesSymbol .filter((prop: ts.Symbol) => !!prop.declarations) // Dynamically generated properties (mapped types) do not have declarations - .map((prop: ts.Symbol) => prop?.declarations[0]) as PropertyLike[]; + .map((prop: ts.Symbol) => prop.declarations[0]) as PropertyLike[]; const signaturesDeclarations: SignatureLike[] = signatures.map((signature: ts.Signature) => signature.declaration) as SignatureLike[]; diff --git a/ui/src/views/types-not-supported.mdx b/ui/src/views/types-not-supported.mdx index d070c570e..7ba2218fa 100644 --- a/ui/src/views/types-not-supported.mdx +++ b/ui/src/views/types-not-supported.mdx @@ -42,3 +42,32 @@ Object.getOwnPropertySymbols(mock); //it should return the symbol testSymbol ``` TsAutoMock will create a property with the random name generated by typescript (example __@aSymbol) instead of the correct symbol. + +## Extends Mapped Type +```ts + +enum SOME_ENUM { + FIRST = 'FIRST', + SECOND = 'SECOND', +} + +interface IBase { + someField: string; + anotherField: number; +} + +interface InterfaceWithExtendsMappedType extends Record {} + +const mock = createMock(); + +mock.FIRST // undefined, it should be IBase[] +mock.SECOND // undefined, it should be IBase[] + +[issue](https://github.com/Typescript-TDD/ts-auto-mock/issues/238) +Unfortunately this functionality doesnt work yet because when getting properties from an interface that extend +a mapped type typescript returns a different type of property that is difficult to mock. + +There is a branch created with a working version but it needs more investigation because the implementation is not readable and it may cause more issues +[link](https://github.com/Typescript-TDD/ts-auto-mock/tree/feature/extends-mapped-type) + +``` \ No newline at end of file