Skip to content

Commit

Permalink
fix(hydratedMocks): fix recursion (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
uittorio committed May 20, 2021
1 parent 9ed9035 commit 8567273
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 67 deletions.
80 changes: 15 additions & 65 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"all-contributors-cli": "6.20.0",
"clean-webpack-plugin": "^3.0.0",
"clean-webpack-plugin": "^4.0.0-alpha.0",
"commitizen": "^4.2.3",
"conventional-changelog-angular": "^5.0.12",
"copy-webpack-plugin": "^8.1.1",
Expand Down
16 changes: 15 additions & 1 deletion src/transformer/descriptor/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ export function GetType(node: ts.Node, scope: Scope): ts.Node {
node.typeName
);

return GetType(declaration, scope);
const type: ts.Node = GetType(declaration, scope);

// if the type is a reusable and finite (reusable types except for typeAlias)
// we can return the original node
// so it can be re used and we handle recursion

if (
type.kind === ts.SyntaxKind.InterfaceDeclaration ||
type.kind === ts.SyntaxKind.ClassDeclaration ||
type.kind === ts.SyntaxKind.TypeLiteral
) {
return node;
}

return type;
}

if (ts.isThisTypeNode(node)) {
Expand Down
44 changes: 44 additions & 0 deletions test/createHydratedMock/create-hydrated-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,48 @@ describe('create-hydrated-mock', () => {
});
});
});

describe('for recursive types', () => {
describe('for interfaces', () => {
type B = A;

interface A {
recursiveProp: B | null;
aProp: string | null;
}

it('should use the define type', () => {
const type: A = createHydratedMock<A>();
expect(type.recursiveProp?.aProp).toBe('');
});
});

describe('for classes', () => {
type B = A;

class A {
public recursiveProp: B | null;
public aProp: string | null;
}

it('should use the define type', () => {
const type: A = createHydratedMock<A>();
expect(type.recursiveProp?.aProp).toBe('');
});
});

describe('for type literal', () => {
type B = A;

type A = {
recursiveProp: B | null;
aProp: string | null;
};

it('should use the define type', () => {
const type: A = createHydratedMock<A>();
expect(type.recursiveProp?.aProp).toBe('');
});
});
});
});

0 comments on commit 8567273

Please sign in to comment.