Skip to content

Commit 8567273

Browse files
authored
fix(hydratedMocks): fix recursion (#736)
1 parent 9ed9035 commit 8567273

File tree

4 files changed

+75
-67
lines changed

4 files changed

+75
-67
lines changed

package-lock.json

Lines changed: 15 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@typescript-eslint/eslint-plugin": "^4.22.0",
6565
"@typescript-eslint/parser": "^4.22.0",
6666
"all-contributors-cli": "6.20.0",
67-
"clean-webpack-plugin": "^3.0.0",
67+
"clean-webpack-plugin": "^4.0.0-alpha.0",
6868
"commitizen": "^4.2.3",
6969
"conventional-changelog-angular": "^5.0.12",
7070
"copy-webpack-plugin": "^8.1.1",

src/transformer/descriptor/type/type.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@ export function GetType(node: ts.Node, scope: Scope): ts.Node {
4141
node.typeName
4242
);
4343

44-
return GetType(declaration, scope);
44+
const type: ts.Node = GetType(declaration, scope);
45+
46+
// if the type is a reusable and finite (reusable types except for typeAlias)
47+
// we can return the original node
48+
// so it can be re used and we handle recursion
49+
50+
if (
51+
type.kind === ts.SyntaxKind.InterfaceDeclaration ||
52+
type.kind === ts.SyntaxKind.ClassDeclaration ||
53+
type.kind === ts.SyntaxKind.TypeLiteral
54+
) {
55+
return node;
56+
}
57+
58+
return type;
4559
}
4660

4761
if (ts.isThisTypeNode(node)) {

test/createHydratedMock/create-hydrated-mock.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,48 @@ describe('create-hydrated-mock', () => {
229229
});
230230
});
231231
});
232+
233+
describe('for recursive types', () => {
234+
describe('for interfaces', () => {
235+
type B = A;
236+
237+
interface A {
238+
recursiveProp: B | null;
239+
aProp: string | null;
240+
}
241+
242+
it('should use the define type', () => {
243+
const type: A = createHydratedMock<A>();
244+
expect(type.recursiveProp?.aProp).toBe('');
245+
});
246+
});
247+
248+
describe('for classes', () => {
249+
type B = A;
250+
251+
class A {
252+
public recursiveProp: B | null;
253+
public aProp: string | null;
254+
}
255+
256+
it('should use the define type', () => {
257+
const type: A = createHydratedMock<A>();
258+
expect(type.recursiveProp?.aProp).toBe('');
259+
});
260+
});
261+
262+
describe('for type literal', () => {
263+
type B = A;
264+
265+
type A = {
266+
recursiveProp: B | null;
267+
aProp: string | null;
268+
};
269+
270+
it('should use the define type', () => {
271+
const type: A = createHydratedMock<A>();
272+
expect(type.recursiveProp?.aProp).toBe('');
273+
});
274+
});
275+
});
232276
});

0 commit comments

Comments
 (0)