Skip to content

Commit a2d58ad

Browse files
authored
fix(union): ensure union type with null or unknown will mock the first type (#322)
BREAKING CHANGE: union types that resolve in null or unknown will now not be converted to undefined. before ```ts type = string | null // undefined ``` after ```ts type = string | null // '' (empty string) ```
1 parent a106e44 commit a2d58ad

File tree

3 files changed

+56
-55
lines changed

3 files changed

+56
-55
lines changed

src/transformer/descriptor/union/union.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@ export function GetUnionDescriptor(node: ts.UnionTypeNode, scope: Scope): ts.Exp
1717
}
1818

1919
function isNotDefinedType(typeNode: ts.Node): boolean {
20-
return typeNode.kind === ts.SyntaxKind.VoidKeyword
21-
|| typeNode.kind === ts.SyntaxKind.NullKeyword
22-
|| typeNode.kind === ts.SyntaxKind.UnknownKeyword
23-
|| typeNode.kind === ts.SyntaxKind.UndefinedKeyword;
20+
return typeNode.kind === ts.SyntaxKind.VoidKeyword || typeNode.kind === ts.SyntaxKind.UndefinedKeyword;
2421
}

test/transformer/descriptor/union/union.test.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createMock } from 'ts-auto-mock';
2+
import { ImportInterface } from '../utils/interfaces/importInterface';
23

34
describe('for union', () => {
45
interface Interface {
@@ -10,22 +11,14 @@ describe('for union', () => {
1011
expect(properties.a).toBe('');
1112
});
1213

13-
describe('union optional', () => {
14+
describe('type interface import', () => {
1415
class MyClass {
15-
public test: string | void;
16-
public test2: string | null;
17-
public test3: string | unknown;
18-
public test4: string | undefined;
19-
public test5: undefined | string | number;
16+
public test: string | ImportInterface;
2017
}
2118

22-
it('should not set the value', () => {
19+
it('should set the value', () => {
2320
const properties: MyClass = createMock<MyClass>();
24-
expect(properties.test).toBeUndefined();
25-
expect(properties.test2).toBeUndefined();
26-
expect(properties.test3).toBeUndefined();
27-
expect(properties.test4).toBeUndefined();
28-
expect(properties.test5).toBeUndefined();
21+
expect(properties.test).toBe('');
2922
});
3023
});
3124

@@ -69,4 +62,26 @@ describe('for union', () => {
6962
expect(properties.test).toEqual([]);
7063
});
7164
});
65+
66+
describe('union type null', () => {
67+
interface Interface {
68+
a: string | null;
69+
}
70+
71+
it('should set the first type', () => {
72+
const properties: Interface = createMock<Interface>();
73+
expect(properties.a).toBe('');
74+
});
75+
});
76+
77+
describe('union type unknown', () => {
78+
interface Interface {
79+
a: string | unknown;
80+
}
81+
82+
it('should set the first type', () => {
83+
const properties: Interface = createMock<Interface>();
84+
expect(properties.a).toBe('');
85+
});
86+
});
7287
});
Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createMock } from 'ts-auto-mock';
2-
import { ImportInterface } from '../utils/interfaces/importInterface';
32
import { ImportType } from '../utils/types/type';
43

54
describe('union optional', () => {
65
describe('type reference', () => {
7-
type Type = null;
6+
type Type = void;
7+
88
class MyClass {
99
public test: string | Type;
1010
}
@@ -26,20 +26,20 @@ describe('union optional', () => {
2626
});
2727
});
2828

29-
describe('type interface import', () => {
29+
describe('type object declared', () => {
3030
class MyClass {
31-
public test: string | ImportInterface;
31+
public test: string | { a: string } | undefined;
3232
}
3333

3434
it('should not set the value', () => {
3535
const properties: MyClass = createMock<MyClass>();
36-
expect(properties.test).toBe('');
36+
expect(properties.test).toBeUndefined();
3737
});
3838
});
3939

40-
describe('type object declared', () => {
40+
describe('type declared value', () => {
4141
class MyClass {
42-
public test: string | { a: string } | null;
42+
public test: '2' | undefined;
4343
}
4444

4545
it('should not set the value', () => {
@@ -48,9 +48,11 @@ describe('union optional', () => {
4848
});
4949
});
5050

51-
describe('type declared value', () => {
51+
describe('type reference optional', () => {
52+
type TypeOptional = string | void;
53+
5254
class MyClass {
53-
public test: '2' | null;
55+
public test: TypeOptional | number;
5456
}
5557

5658
it('should not set the value', () => {
@@ -59,42 +61,29 @@ describe('union optional', () => {
5961
});
6062
});
6163

62-
describe('type reference optional', () => {
63-
type TypeOptional = string | null;
64-
65-
class MyClass {
66-
public test: TypeOptional | number;
67-
}
68-
69-
it('should not set the value', () => {
70-
const properties: MyClass = createMock<MyClass>();
71-
expect(properties.test).toBeUndefined();
72-
});
73-
});
74-
75-
describe('type reference optional second', () => {
76-
type TypeOptional = string | null;
64+
describe('type reference optional as second type', () => {
65+
type TypeOptional = string | void;
7766

78-
class MyClass {
79-
public test: number | TypeOptional;
80-
}
67+
class MyClass {
68+
public test: number | TypeOptional;
69+
}
8170

82-
it('should not set the value', () => {
83-
const properties: MyClass = createMock<MyClass>();
84-
expect(properties.test).toBeUndefined();
85-
});
71+
it('should not set the value', () => {
72+
const properties: MyClass = createMock<MyClass>();
73+
expect(properties.test).toBeUndefined();
74+
});
8675
});
8776

8877
describe('type reference optional and extends', () => {
89-
type TypeOptional = { a: string} & { b: number} | null;
78+
type TypeOptional = { a: string } & { b: number } | void;
9079

91-
class MyClass {
92-
public test: number | TypeOptional;
93-
}
80+
class MyClass {
81+
public test: number | TypeOptional;
82+
}
9483

95-
it('should not set the value', () => {
96-
const properties: MyClass = createMock<MyClass>();
97-
expect(properties.test).toBeUndefined();
98-
});
84+
it('should not set the value', () => {
85+
const properties: MyClass = createMock<MyClass>();
86+
expect(properties.test).toBeUndefined();
87+
});
9988
});
10089
});

0 commit comments

Comments
 (0)