forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjasmine-misc.ts
More file actions
247 lines (222 loc) · 7.09 KB
/
jasmine-misc.ts
File metadata and controls
247 lines (222 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
/**
* @fileoverview This file contains transformers for miscellaneous Jasmine APIs that don't
* fit into other categories. This includes timer mocks (`jasmine.clock`), the `fail()`
* function, and configuration settings like `jasmine.DEFAULT_TIMEOUT_INTERVAL`. It also
* includes logic to identify and add TODO comments for unsupported Jasmine features.
*/
import ts from '../../../third_party/typescript';
import { addVitestValueImport, createViCallExpression } from '../utils/ast-helpers';
import { getJasmineMethodName, isJasmineCallExpression } from '../utils/ast-validation';
import { addTodoComment } from '../utils/comment-helpers';
import { RefactorContext } from '../utils/refactor-context';
import { TodoCategory } from '../utils/todo-notes';
export function transformTimerMocks(
node: ts.Node,
{ sourceFile, reporter, pendingVitestValueImports }: RefactorContext,
): ts.Node {
if (
!ts.isCallExpression(node) ||
!ts.isPropertyAccessExpression(node.expression) ||
!ts.isIdentifier(node.expression.name)
) {
return node;
}
const pae = node.expression;
const clockCall = pae.expression;
if (!isJasmineCallExpression(clockCall, 'clock')) {
return node;
}
let newMethodName: string | undefined;
switch (pae.name.text) {
case 'install':
newMethodName = 'useFakeTimers';
break;
case 'tick':
newMethodName = 'advanceTimersByTime';
break;
case 'uninstall':
newMethodName = 'useRealTimers';
break;
case 'mockDate':
newMethodName = 'setSystemTime';
break;
}
if (newMethodName) {
addVitestValueImport(pendingVitestValueImports, 'vi');
reporter.reportTransformation(
sourceFile,
node,
`Transformed \`jasmine.clock().${pae.name.text}\` to \`vi.${newMethodName}\`.`,
);
let newArgs: readonly ts.Expression[] = node.arguments;
if (newMethodName === 'useFakeTimers') {
newArgs = [];
}
if (newMethodName === 'setSystemTime' && node.arguments.length === 0) {
newArgs = [
ts.factory.createNewExpression(ts.factory.createIdentifier('Date'), undefined, []),
];
}
return createViCallExpression(newMethodName, newArgs);
}
return node;
}
export function transformFail(node: ts.Node, { sourceFile, reporter }: RefactorContext): ts.Node {
if (
ts.isExpressionStatement(node) &&
ts.isCallExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) &&
node.expression.expression.text === 'fail'
) {
reporter.reportTransformation(sourceFile, node, 'Transformed `fail()` to `throw new Error()`.');
const reason = node.expression.arguments[0];
const replacement = ts.factory.createThrowStatement(
ts.factory.createNewExpression(
ts.factory.createIdentifier('Error'),
undefined,
reason ? [reason] : [],
),
);
return ts.setOriginalNode(ts.setTextRange(replacement, node), node);
}
return node;
}
export function transformDefaultTimeoutInterval(
node: ts.Node,
{ sourceFile, reporter, pendingVitestValueImports }: RefactorContext,
): ts.Node {
if (
ts.isExpressionStatement(node) &&
ts.isBinaryExpression(node.expression) &&
node.expression.operatorToken.kind === ts.SyntaxKind.EqualsToken
) {
const assignment = node.expression;
if (
ts.isPropertyAccessExpression(assignment.left) &&
ts.isIdentifier(assignment.left.expression) &&
assignment.left.expression.text === 'jasmine' &&
assignment.left.name.text === 'DEFAULT_TIMEOUT_INTERVAL'
) {
addVitestValueImport(pendingVitestValueImports, 'vi');
reporter.reportTransformation(
sourceFile,
node,
'Transformed `jasmine.DEFAULT_TIMEOUT_INTERVAL` to `vi.setConfig()`.',
);
const timeoutValue = assignment.right;
const setConfigCall = createViCallExpression('setConfig', [
ts.factory.createObjectLiteralExpression(
[ts.factory.createPropertyAssignment('testTimeout', timeoutValue)],
false,
),
]);
return ts.factory.updateExpressionStatement(node, setConfigCall);
}
}
return node;
}
export function transformGlobalFunctions(
node: ts.Node,
{ sourceFile, reporter }: RefactorContext,
): ts.Node {
if (
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
(node.expression.text === 'setSpecProperty' || node.expression.text === 'setSuiteProperty')
) {
const functionName = node.expression.text;
reporter.reportTransformation(
sourceFile,
node,
`Found unsupported global function \`${functionName}\`.`,
);
const category = 'unsupported-global-function';
reporter.recordTodo(category, sourceFile, node);
addTodoComment(node, category, { name: functionName });
}
return node;
}
const UNSUPPORTED_JASMINE_CALLS_CATEGORIES = new Set<TodoCategory>([
'addMatchers',
'addCustomEqualityTester',
'mapContaining',
'setContaining',
]);
// A type guard to ensure that the methodName is one of the categories handled by this transformer.
function isUnsupportedJasmineCall(
methodName: string,
): methodName is 'addMatchers' | 'addCustomEqualityTester' | 'mapContaining' | 'setContaining' {
return UNSUPPORTED_JASMINE_CALLS_CATEGORIES.has(methodName as TodoCategory);
}
export function transformUnsupportedJasmineCalls(
node: ts.Node,
{ sourceFile, reporter }: RefactorContext,
): ts.Node {
const methodName = getJasmineMethodName(node);
if (methodName && isUnsupportedJasmineCall(methodName)) {
reporter.reportTransformation(
sourceFile,
node,
`Found unsupported call \`jasmine.${methodName}\`.`,
);
reporter.recordTodo(methodName, sourceFile, node);
addTodoComment(node, methodName);
}
return node;
}
// If any additional properties are added to transforms, they should also be added to this list.
const HANDLED_JASMINE_PROPERTIES = new Set([
// Spies
'createSpy',
'createSpyObj',
'spyOnAllFunctions',
// Clock
'clock',
// Matchers
'any',
'anything',
'stringMatching',
'objectContaining',
'arrayContaining',
'arrayWithExactContents',
'truthy',
'falsy',
'empty',
'notEmpty',
'mapContaining',
'setContaining',
// Other
'DEFAULT_TIMEOUT_INTERVAL',
'addMatchers',
'addCustomEqualityTester',
]);
export function transformUnknownJasmineProperties(
node: ts.Node,
{ sourceFile, reporter }: RefactorContext,
): ts.Node {
if (
ts.isPropertyAccessExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === 'jasmine'
) {
const propName = node.name.text;
if (!HANDLED_JASMINE_PROPERTIES.has(propName)) {
reporter.reportTransformation(
sourceFile,
node,
`Found unknown jasmine property \`jasmine.${propName}\`.`,
);
const category = 'unknown-jasmine-property';
reporter.recordTodo(category, sourceFile, node);
addTodoComment(node, category, { name: propName });
}
}
return node;
}