@openrewrite/recipes-angular 1.34.0 → 1.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -6
- package/dist/index.js.map +1 -1
- package/dist/libraries/components/package-version.d.ts +1 -1
- package/dist/libraries/components/package-version.d.ts.map +1 -1
- package/dist/libraries/components/package-version.js +1 -1
- package/dist/libraries/components/package-version.js.map +1 -1
- package/dist/migration/add-static-false-to-view-queries.d.ts.map +1 -1
- package/dist/migration/add-static-false-to-view-queries.js +16 -13
- package/dist/migration/add-static-false-to-view-queries.js.map +1 -1
- package/dist/migration/angular-version.d.ts +22 -2
- package/dist/migration/angular-version.d.ts.map +1 -1
- package/dist/migration/angular-version.js +65 -18
- package/dist/migration/angular-version.js.map +1 -1
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.js +76 -0
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.d.ts +14 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.js +225 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.js +379 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.d.ts +18 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.js +446 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.js +209 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.js +567 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.d.ts +16 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.js +90 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.js +142 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/support.d.ts +88 -0
- package/dist/migration/jasmine-to-vitest/support.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/support.js +561 -0
- package/dist/migration/jasmine-to-vitest/support.js.map +1 -0
- package/dist/migration/replace-inject-flags.js +2 -2
- package/dist/migration/replace-inject-flags.js.map +1 -1
- package/dist/migration/upgrade-to-angular-10.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-10.js +3 -2
- package/dist/migration/upgrade-to-angular-10.js.map +1 -1
- package/dist/migration/upgrade-to-angular-11.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-11.js +3 -2
- package/dist/migration/upgrade-to-angular-11.js.map +1 -1
- package/dist/migration/upgrade-to-angular-12.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-12.js +3 -2
- package/dist/migration/upgrade-to-angular-12.js.map +1 -1
- package/dist/migration/upgrade-to-angular-13.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-13.js +3 -2
- package/dist/migration/upgrade-to-angular-13.js.map +1 -1
- package/dist/migration/upgrade-to-angular-14.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-14.js +3 -2
- package/dist/migration/upgrade-to-angular-14.js.map +1 -1
- package/dist/migration/upgrade-to-angular-15.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-15.js +3 -2
- package/dist/migration/upgrade-to-angular-15.js.map +1 -1
- package/dist/migration/upgrade-to-angular-16.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-16.js +3 -2
- package/dist/migration/upgrade-to-angular-16.js.map +1 -1
- package/dist/migration/upgrade-to-angular-17.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-17.js +3 -2
- package/dist/migration/upgrade-to-angular-17.js.map +1 -1
- package/dist/migration/upgrade-to-angular-18.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-18.js +3 -2
- package/dist/migration/upgrade-to-angular-18.js.map +1 -1
- package/dist/migration/upgrade-to-angular-19.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-19.js +3 -2
- package/dist/migration/upgrade-to-angular-19.js.map +1 -1
- package/dist/migration/upgrade-to-angular-20.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-20.js +3 -2
- package/dist/migration/upgrade-to-angular-20.js.map +1 -1
- package/dist/migration/upgrade-to-angular-21.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-21.js +3 -2
- package/dist/migration/upgrade-to-angular-21.js.map +1 -1
- package/dist/migration/upgrade-to-angular-22.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-22.js +3 -2
- package/dist/migration/upgrade-to-angular-22.js.map +1 -1
- package/dist/migration/upgrade-to-angular-8.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-8.js +3 -2
- package/dist/migration/upgrade-to-angular-8.js.map +1 -1
- package/dist/migration/upgrade-to-angular-9.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-9.js +3 -2
- package/dist/migration/upgrade-to-angular-9.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +24 -0
- package/src/libraries/components/package-version.ts +2 -2
- package/src/migration/add-static-false-to-view-queries.ts +16 -12
- package/src/migration/angular-version.ts +65 -16
- package/src/migration/jasmine-to-vitest/add-vitest-imports.ts +51 -0
- package/src/migration/jasmine-to-vitest/migrate-fake-async.ts +211 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.ts +346 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-matchers.ts +410 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-misc.ts +190 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-spies.ts +533 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.ts +72 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-types.ts +97 -0
- package/src/migration/jasmine-to-vitest/support.ts +516 -0
- package/src/migration/replace-inject-flags.ts +2 -2
- package/src/migration/upgrade-to-angular-10.ts +4 -3
- package/src/migration/upgrade-to-angular-11.ts +4 -3
- package/src/migration/upgrade-to-angular-12.ts +4 -3
- package/src/migration/upgrade-to-angular-13.ts +4 -3
- package/src/migration/upgrade-to-angular-14.ts +4 -3
- package/src/migration/upgrade-to-angular-15.ts +4 -3
- package/src/migration/upgrade-to-angular-16.ts +4 -3
- package/src/migration/upgrade-to-angular-17.ts +4 -3
- package/src/migration/upgrade-to-angular-18.ts +4 -3
- package/src/migration/upgrade-to-angular-19.ts +4 -3
- package/src/migration/upgrade-to-angular-20.ts +4 -3
- package/src/migration/upgrade-to-angular-21.ts +4 -3
- package/src/migration/upgrade-to-angular-22.ts +4 -3
- package/src/migration/upgrade-to-angular-8.ts +4 -3
- package/src/migration/upgrade-to-angular-9.ts +4 -3
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {Cursor, ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
8
|
+
import {J, isFieldAccess, isIdentifier, isMethodInvocation} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {JS, MethodMatcher} from "@openrewrite/rewrite/javascript";
|
|
10
|
+
import {create} from "mutative";
|
|
11
|
+
import {
|
|
12
|
+
JasmineToVitestVisitor,
|
|
13
|
+
Piece,
|
|
14
|
+
args,
|
|
15
|
+
hasMarker,
|
|
16
|
+
isGlobalCall,
|
|
17
|
+
isNamed,
|
|
18
|
+
isStringLiteral,
|
|
19
|
+
isTransparentWrapper,
|
|
20
|
+
jasmineMethodName,
|
|
21
|
+
printTrimmed,
|
|
22
|
+
quoteOf,
|
|
23
|
+
replaceWith,
|
|
24
|
+
select,
|
|
25
|
+
stringLiteral,
|
|
26
|
+
unwrap,
|
|
27
|
+
walk
|
|
28
|
+
} from "./support";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @see https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts
|
|
32
|
+
*/
|
|
33
|
+
export class MigrateJasmineSpiesToVitest extends Recipe {
|
|
34
|
+
readonly name = "org.openrewrite.angular.migration.migrate-jasmine-spies-to-vitest";
|
|
35
|
+
readonly displayName = "Migrate Jasmine spies to Vitest mocks";
|
|
36
|
+
readonly description = "Rewrites Jasmine spies in Angular tests to Vitest's mocking API: `spyOn` becomes " +
|
|
37
|
+
"`vi.spyOn(...).mockReturnValue(undefined)`, keeping Jasmine's stub-by-default behavior unless a strategy " +
|
|
38
|
+
"follows; `.and.returnValue`, `.and.callFake`, `.and.resolveTo` and the other strategies become the " +
|
|
39
|
+
"matching `mock*` calls; `jasmine.createSpy` and `jasmine.createSpyObj` become `vi.fn()` and an object of " +
|
|
40
|
+
"them; and `spy.calls.count()`, `argsFor`, `first`, `mostRecent` and `all` read `vi.mocked(spy).mock`. " +
|
|
41
|
+
"A strategy Vitest has no equivalent for, such as `withArgs`, is left with a `TODO: vitest-migration` " +
|
|
42
|
+
"comment, as the Angular CLI's `refactor-jasmine-vitest` schematic does.";
|
|
43
|
+
|
|
44
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
45
|
+
return new SpiesVisitor();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const CALL_INFO_PROPERTIES = new Map<string, { mockProperty: string; valueAccess?: string }>([
|
|
50
|
+
['args', {mockProperty: 'calls'}],
|
|
51
|
+
['returnValue', {mockProperty: 'results', valueAccess: 'value'}],
|
|
52
|
+
['object', {mockProperty: 'contexts'}],
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
class SpiesVisitor extends JasmineToVitestVisitor {
|
|
56
|
+
private quote = "'";
|
|
57
|
+
|
|
58
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
59
|
+
this.quote = quoteOf(cu);
|
|
60
|
+
return super.visitJsCompilationUnit(cu, p);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected override async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
64
|
+
const cursorBefore = this.cursor;
|
|
65
|
+
const visited = await super.visitMethodInvocation(method, p);
|
|
66
|
+
if (!visited || !isMethodInvocation(visited)) return visited;
|
|
67
|
+
const m = visited;
|
|
68
|
+
return (await this.primarySpy(m, cursorBefore))
|
|
69
|
+
?? (await this.spyStrategy(m, cursorBefore))
|
|
70
|
+
?? this.spyOnAllFunctions(m)
|
|
71
|
+
?? (await this.createSpy(m))
|
|
72
|
+
?? (await this.createSpyObj(m))
|
|
73
|
+
?? (await this.spyReset(m))
|
|
74
|
+
?? (await this.thisFor(m))
|
|
75
|
+
?? (await this.callInspection(m, cursorBefore))
|
|
76
|
+
?? m;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected override async visitFieldAccess(fieldAccess: J.FieldAccess, p: ExecutionContext): Promise<J | undefined> {
|
|
80
|
+
const visited = await super.visitFieldAccess(fieldAccess, p);
|
|
81
|
+
if (!visited || !isFieldAccess(visited)) return visited;
|
|
82
|
+
return (await this.callInfoProperty(visited)) ?? visited;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `spyOn(obj, 'm')` to `vi.spyOn(obj, 'm').mockReturnValue(undefined)`, Jasmine's stub by default. */
|
|
86
|
+
private async primarySpy(m: J.MethodInvocation, cursor: Cursor): Promise<J | undefined> {
|
|
87
|
+
if (!isGlobalCall(m, 'spyOn', 'spyOnProperty')) return undefined;
|
|
88
|
+
const a = args(m);
|
|
89
|
+
const spyOnArgs: Piece[] = m.name.simpleName === 'spyOnProperty' && a.length === 2
|
|
90
|
+
? [a[0], ', ', a[1], `, ${stringLiteral('get', this.quote)}`]
|
|
91
|
+
: [a];
|
|
92
|
+
const typeArgs = m.typeParameters
|
|
93
|
+
? '<' + (await Promise.all(m.typeParameters.elements.map(t => printTrimmed(t.element, this.cursor)))).join(', ') + '>'
|
|
94
|
+
: '';
|
|
95
|
+
if (isChainedWithAnd(cursor)) {
|
|
96
|
+
return replaceWith(m, this.cursor, `vi.spyOn${typeArgs}(`, ...spyOnArgs, ')');
|
|
97
|
+
}
|
|
98
|
+
return replaceWith(m, this.cursor, `vi.spyOn${typeArgs}(`, ...spyOnArgs, ').mockReturnValue(undefined)');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private async spyStrategy(m: J.MethodInvocation, cursor: Cursor): Promise<J | undefined> {
|
|
102
|
+
const and = unwrapAnd(select(m));
|
|
103
|
+
if (!and) return undefined;
|
|
104
|
+
const spyCall = and.spy;
|
|
105
|
+
const strategy = m.name.simpleName;
|
|
106
|
+
const a = args(m);
|
|
107
|
+
|
|
108
|
+
// Vitest mocks have no `withArgs`, so the whole chain is left alone, with one note on its outermost call.
|
|
109
|
+
if (chainContainsWithArgs(spyCall)) {
|
|
110
|
+
if (!isChainedWithAnd(cursor)) {
|
|
111
|
+
this.todo(m, 'Vitest does not have a direct equivalent for spy.withArgs().' +
|
|
112
|
+
' Please migrate this manually, for example with mockImplementation() that checks the arguments.',
|
|
113
|
+
'https://vitest.dev/api/mock.html#mockimplementation');
|
|
114
|
+
}
|
|
115
|
+
return m;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const rename = (name: string, argumentsOf: J.MethodInvocation = m) => renameStrategy(m, and.node, spyCall, name, argumentsOf);
|
|
119
|
+
switch (strategy) {
|
|
120
|
+
case 'returnValue': {
|
|
121
|
+
const promise = promiseResolveReject(a[0]);
|
|
122
|
+
if (promise) {
|
|
123
|
+
return rename(promise.method === 'resolve' ? 'mockResolvedValue' : 'mockRejectedValue', promise.call);
|
|
124
|
+
}
|
|
125
|
+
return rename('mockReturnValue');
|
|
126
|
+
}
|
|
127
|
+
case 'resolveTo':
|
|
128
|
+
return rename('mockResolvedValue');
|
|
129
|
+
case 'rejectWith':
|
|
130
|
+
return rename('mockRejectedValue');
|
|
131
|
+
case 'callFake':
|
|
132
|
+
return rename('mockImplementation');
|
|
133
|
+
case 'returnValues': {
|
|
134
|
+
if (a.length === 0) return replaceWith(m, this.cursor, spyCall);
|
|
135
|
+
const pieces: Piece[] = [spyCall];
|
|
136
|
+
for (const v of a) pieces.push('.mockReturnValueOnce(', v, ')');
|
|
137
|
+
return replaceWith(m, this.cursor, ...pieces);
|
|
138
|
+
}
|
|
139
|
+
case 'callThrough':
|
|
140
|
+
if (!isSpyCreation(spyCall)) {
|
|
141
|
+
// On an existing spy it restores the original implementation, so it cannot be dropped.
|
|
142
|
+
this.todo(m, 'Vitest does not have a direct equivalent for .and.callThrough() on an existing spy.' +
|
|
143
|
+
' Please migrate this manually, for example with spy.mockReset(), which restores' +
|
|
144
|
+
' the original implementation but also clears the recorded calls.',
|
|
145
|
+
'https://vitest.dev/api/mock.html#mockreset');
|
|
146
|
+
return m;
|
|
147
|
+
}
|
|
148
|
+
return replaceWith(m, this.cursor, spyCall);
|
|
149
|
+
case 'stub':
|
|
150
|
+
return replaceWith(m, this.cursor, spyCall, '.mockImplementation(() => {})');
|
|
151
|
+
case 'throwError': {
|
|
152
|
+
const error = a[0];
|
|
153
|
+
const thrown: Piece[] = !error ? ['new Error()']
|
|
154
|
+
: error.kind === J.Kind.NewClass ? [error] : ['new Error(', error, ')'];
|
|
155
|
+
return replaceWith(m, this.cursor, spyCall, '.mockImplementation(() => {\nthrow ', ...thrown, ';\n})');
|
|
156
|
+
}
|
|
157
|
+
case 'identity':
|
|
158
|
+
return replaceWith(m, this.cursor, spyCall, '.getMockName()');
|
|
159
|
+
default:
|
|
160
|
+
this.todo(m, `Unsupported spy strategy ".and.${strategy}()" found. Please migrate this manually.`,
|
|
161
|
+
'https://vitest.dev/api/mocked.html#mock');
|
|
162
|
+
return m;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private spyOnAllFunctions(m: J.MethodInvocation): J | undefined {
|
|
167
|
+
if (jasmineMethodName(m) !== 'spyOnAllFunctions') return undefined;
|
|
168
|
+
this.todo(m, 'Vitest does not have a direct equivalent for jasmine.spyOnAllFunctions().' +
|
|
169
|
+
' Please spy on individual methods manually using vi.spyOn().', 'https://vitest.dev/api/vi.html#vi-spyon');
|
|
170
|
+
return m;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** `jasmine.createSpy(name, fn)` to `vi.fn(fn).mockName(name)`. */
|
|
174
|
+
private async createSpy(m: J.MethodInvocation): Promise<J | undefined> {
|
|
175
|
+
if (jasmineMethodName(m) !== 'createSpy') return undefined;
|
|
176
|
+
const [name, original] = args(m);
|
|
177
|
+
const fn: Piece[] = original ? ['vi.fn(', original, ')'] : ['vi.fn()'];
|
|
178
|
+
return name
|
|
179
|
+
? replaceWith(m, this.cursor, ...fn, '.mockName(', name, ')')
|
|
180
|
+
: replaceWith(m, this.cursor, ...fn);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** `jasmine.createSpyObj('svc', ['a'])` to `{ a: vi.fn().mockName('svc.a') }`. */
|
|
184
|
+
private async createSpyObj(m: J.MethodInvocation): Promise<J | undefined> {
|
|
185
|
+
if (jasmineMethodName(m) !== 'createSpyObj') return undefined;
|
|
186
|
+
const a = args(m);
|
|
187
|
+
const first = a[0];
|
|
188
|
+
const hasBaseName = isStringLiteral(first);
|
|
189
|
+
const baseName = hasBaseName ? (first as J.Literal).value as string : undefined;
|
|
190
|
+
const methods = unwrap(hasBaseName ? a[1] : first);
|
|
191
|
+
const propertiesArg = hasBaseName ? a[2] : a[1];
|
|
192
|
+
|
|
193
|
+
if (a.length < 2 && hasBaseName) {
|
|
194
|
+
this.todo(m, 'jasmine.createSpyObj called with a single argument is not supported for transformation.',
|
|
195
|
+
'https://vitest.dev/api/vi.html#vi-fn');
|
|
196
|
+
return m;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const mockName = (method: string) => baseName !== undefined
|
|
200
|
+
? `.mockName(${stringLiteral(`${baseName}.${method}`, this.quote)})` : '';
|
|
201
|
+
const entries: Piece[][] = [];
|
|
202
|
+
if (methods?.kind === J.Kind.NewArray) {
|
|
203
|
+
for (const element of (methods as J.NewArray).initializer?.elements ?? []) {
|
|
204
|
+
const e = element.element;
|
|
205
|
+
if (isStringLiteral(e)) {
|
|
206
|
+
entries.push([`${propertyKey(e.value as string)}: vi.fn()${mockName(e.value as string)}`]);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
} else if (methods && isObjectLiteral(methods)) {
|
|
210
|
+
for (const prop of objectProperties(methods)) {
|
|
211
|
+
const key = prop.name.element;
|
|
212
|
+
if (prop.kind === JS.Kind.PropertyAssignment && isIdentifier(key) && prop.initializer) {
|
|
213
|
+
entries.push([`${key.simpleName}: vi.fn()${mockName(key.simpleName)}.mockReturnValue(`, prop.initializer, ')']);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
this.todo(m, 'Cannot transform jasmine.createSpyObj with a dynamic variable. Please migrate this manually.',
|
|
218
|
+
'https://vitest.dev/api/vi.html#vi-fn');
|
|
219
|
+
return m;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
let dynamicProperties = false;
|
|
223
|
+
if (propertiesArg) {
|
|
224
|
+
const props = unwrap(propertiesArg);
|
|
225
|
+
if (props && isObjectLiteral(props)) {
|
|
226
|
+
for (const prop of objectProperties(props)) {
|
|
227
|
+
entries.push([await printTrimmed(prop, this.cursor)]);
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
dynamicProperties = true;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Jasmine's spy object claims the type the test declares for it without TypeScript checking it
|
|
235
|
+
// against the object's members, so the literal standing in for it says the same with a cast.
|
|
236
|
+
// Without one, a `MockedObject<Keycloak>` holding only `init` and `login` fails with TS2322.
|
|
237
|
+
const declared = await this.declaredSpyObjType(m);
|
|
238
|
+
const pieces: Piece[] = ['({'];
|
|
239
|
+
entries.forEach((entry, i) => {
|
|
240
|
+
pieces.push('\n', ...entry, i < entries.length - 1 ? ',' : '');
|
|
241
|
+
});
|
|
242
|
+
pieces.push(entries.length ? '\n}' : '}');
|
|
243
|
+
pieces.push(declared ? ` as unknown as ${declared})` : ')');
|
|
244
|
+
const result = await replaceWith(m, this.cursor, ...pieces);
|
|
245
|
+
if (dynamicProperties) {
|
|
246
|
+
this.todo(result, 'Cannot transform jasmine.createSpyObj with a dynamic property map. Please migrate this manually.',
|
|
247
|
+
'https://vitest.dev/api/vi.html#vi-fn');
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The type a spy object is declared as, as written: `jasmine.SpyObj<T>` for `createSpyObj<T>(...)`,
|
|
254
|
+
* or the annotation of the variable or field it initializes or is assigned to. Undefined where
|
|
255
|
+
* nothing declares one, or where the declaration is `any` or `unknown`, which take a literal as it is.
|
|
256
|
+
*/
|
|
257
|
+
private async declaredSpyObjType(m: J.MethodInvocation): Promise<string | undefined> {
|
|
258
|
+
if (m.typeParameters?.elements.length) {
|
|
259
|
+
const typeArgs = await Promise.all(m.typeParameters.elements.map(t => printTrimmed(t.element, this.cursor)));
|
|
260
|
+
return `jasmine.SpyObj<${typeArgs.join(', ')}>`;
|
|
261
|
+
}
|
|
262
|
+
const parents: any[] = [];
|
|
263
|
+
for (let c: Cursor | undefined = this.cursor.parent; c && parents.length < 2; c = c.parent) {
|
|
264
|
+
const v = c.value;
|
|
265
|
+
if (!v || typeof v.kind !== 'string' || v.kind.endsWith('Padded') || isTransparentWrapper(v)) continue;
|
|
266
|
+
parents.push(v);
|
|
267
|
+
}
|
|
268
|
+
const [parent, grandparent] = parents;
|
|
269
|
+
let annotations: J[] = [];
|
|
270
|
+
if (parent?.kind === J.Kind.NamedVariable && grandparent?.kind === J.Kind.VariableDeclarations) {
|
|
271
|
+
annotations = grandparent.typeExpression ? [grandparent.typeExpression] : [];
|
|
272
|
+
} else if (parent?.kind === J.Kind.Assignment) {
|
|
273
|
+
const target = unwrap(parent.variable);
|
|
274
|
+
const name = isIdentifier(target) ? target.simpleName
|
|
275
|
+
: isFieldAccess(target) && isNamed(target.target, 'this') ? target.name.element.simpleName
|
|
276
|
+
: undefined;
|
|
277
|
+
const cu = this.cursor.firstEnclosing((t: any): t is JS.CompilationUnit => t?.kind === JS.Kind.CompilationUnit);
|
|
278
|
+
if (name && cu) annotations = annotationsOf(cu, name);
|
|
279
|
+
}
|
|
280
|
+
const written = new Set<string>();
|
|
281
|
+
for (const a of annotations) {
|
|
282
|
+
const t = a.kind === JS.Kind.TypeInfo ? (a as JS.TypeInfo).typeIdentifier : a;
|
|
283
|
+
written.add(await printTrimmed(t as J, this.cursor));
|
|
284
|
+
}
|
|
285
|
+
// Two declarations of the name with different types: which one the assignment reaches is a scope question.
|
|
286
|
+
if (written.size !== 1) return undefined;
|
|
287
|
+
const [type] = written;
|
|
288
|
+
return type === 'any' || type === 'unknown' ? undefined : type;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** `spy.calls.reset()` to `spy.mockClear()`. */
|
|
292
|
+
private async spyReset(m: J.MethodInvocation): Promise<J | undefined> {
|
|
293
|
+
const spy = callsTarget(m, 'reset');
|
|
294
|
+
if (!spy) return undefined;
|
|
295
|
+
return replaceWith(m, this.cursor, spy, '.mockClear()');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** `spy.calls.thisFor(i)` to `vi.mocked(spy).mock.contexts[i]`. */
|
|
299
|
+
private async thisFor(m: J.MethodInvocation): Promise<J | undefined> {
|
|
300
|
+
const spy = callsTarget(m, 'thisFor');
|
|
301
|
+
if (!spy) return undefined;
|
|
302
|
+
const index = args(m)[0];
|
|
303
|
+
return replaceWith(m, this.cursor, 'vi.mocked(', spy, ').mock.contexts[', ...(index ? [index] : ['0']), ']');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private async callInspection(m: J.MethodInvocation, cursor: Cursor): Promise<J | undefined> {
|
|
307
|
+
const spy = isMethodInvocation(m) && m.select ? callsSpy(select(m)) : undefined;
|
|
308
|
+
if (!spy) return undefined;
|
|
309
|
+
const mocked: Piece[] = ['vi.mocked(', spy, ').mock.calls'];
|
|
310
|
+
switch (m.name.simpleName) {
|
|
311
|
+
case 'any':
|
|
312
|
+
return replaceWith(m, this.cursor, ...mocked, '.length > 0');
|
|
313
|
+
case 'count':
|
|
314
|
+
return replaceWith(m, this.cursor, ...mocked, '.length');
|
|
315
|
+
case 'first':
|
|
316
|
+
// `.args`, `.returnValue` and `.object` of the first call are read by `callInfoProperty`.
|
|
317
|
+
if (readsCallInfoProperty(cursor)) return m;
|
|
318
|
+
return replaceWith(m, this.cursor, ...mocked, '[0]');
|
|
319
|
+
case 'all':
|
|
320
|
+
if (isIndexedForCallInfo(cursor)) return m;
|
|
321
|
+
return replaceWith(m, this.cursor, ...mocked);
|
|
322
|
+
case 'allArgs':
|
|
323
|
+
return replaceWith(m, this.cursor, ...mocked);
|
|
324
|
+
case 'argsFor':
|
|
325
|
+
return replaceWith(m, this.cursor, ...mocked, '[', args(m)[0], ']');
|
|
326
|
+
case 'saveArgumentsByValue':
|
|
327
|
+
this.todo(m, 'Vitest does not have a direct equivalent for spy.calls.saveArgumentsByValue().' +
|
|
328
|
+
' Please migrate this manually by cloning and storing the arguments in a local variable.');
|
|
329
|
+
return m;
|
|
330
|
+
case 'mostRecent':
|
|
331
|
+
if (!readsCallInfoProperty(cursor)) {
|
|
332
|
+
this.todo(m, 'Direct usage of mostRecent() is not supported.' +
|
|
333
|
+
' Please refactor to access .args, .returnValue, or .object directly' +
|
|
334
|
+
' or use vi.mocked(spy).mock.lastCall.', 'https://vitest.dev/api/mocked.html#mock-lastcall');
|
|
335
|
+
}
|
|
336
|
+
return m;
|
|
337
|
+
}
|
|
338
|
+
return undefined;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* `.args`, `.returnValue` and `.object` of `spy.calls.first()`, `spy.calls.mostRecent()` or
|
|
343
|
+
* `spy.calls.all()[i]`, read off `vi.mocked(spy).mock`.
|
|
344
|
+
*/
|
|
345
|
+
private async callInfoProperty(f: J.FieldAccess): Promise<J | undefined> {
|
|
346
|
+
const property = f.name.element.simpleName;
|
|
347
|
+
const mapping = CALL_INFO_PROPERTIES.get(property);
|
|
348
|
+
if (!mapping) return undefined;
|
|
349
|
+
const target = unwrap(f.target)!;
|
|
350
|
+
const optional = hasMarker(f.target, 'org.openrewrite.javascript.marker.Optional');
|
|
351
|
+
let call: J | undefined = target;
|
|
352
|
+
let index: J | undefined;
|
|
353
|
+
if (target.kind === J.Kind.ArrayAccess) {
|
|
354
|
+
const access = target as J.ArrayAccess;
|
|
355
|
+
index = access.dimension.index.element;
|
|
356
|
+
call = unwrap(access.indexed);
|
|
357
|
+
}
|
|
358
|
+
if (!call || !isMethodInvocation(call) || !call.select) return undefined;
|
|
359
|
+
const spy = callsSpy(select(call));
|
|
360
|
+
if (!spy) return undefined;
|
|
361
|
+
const method = call.name.simpleName;
|
|
362
|
+
const base: Piece[] = ['vi.mocked(', spy, `).mock.${mapping.mockProperty}`];
|
|
363
|
+
const value = mapping.valueAccess;
|
|
364
|
+
if (index) {
|
|
365
|
+
if (method !== 'all') return undefined;
|
|
366
|
+
return replaceWith(f, this.cursor, ...base, '[', index, ']', value ? `${optional ? '?.' : '.'}${value}` : '');
|
|
367
|
+
}
|
|
368
|
+
if (method === 'first') {
|
|
369
|
+
return replaceWith(f, this.cursor, ...base, '[0]', value ? `${optional ? '?.' : '.'}${value}` : '');
|
|
370
|
+
}
|
|
371
|
+
if (method === 'mostRecent') {
|
|
372
|
+
if (property === 'args') {
|
|
373
|
+
return replaceWith(f, this.cursor, 'vi.mocked(', spy, ').mock.lastCall');
|
|
374
|
+
}
|
|
375
|
+
return replaceWith(f, this.cursor, ...base, '.at(-1)', value ? `?.${value}` : '');
|
|
376
|
+
}
|
|
377
|
+
return undefined;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** The type annotations of every variable or field named `name` the file declares. */
|
|
382
|
+
function annotationsOf(cu: J, name: string): J[] {
|
|
383
|
+
const found: J[] = [];
|
|
384
|
+
walk(cu, (n: any) => {
|
|
385
|
+
if (n.kind !== J.Kind.VariableDeclarations || !n.typeExpression) return;
|
|
386
|
+
if (n.variables.some((v: any) => isIdentifier(v.element.name) && v.element.name.simpleName === name)) {
|
|
387
|
+
found.push(n.typeExpression);
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
return found;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function isObjectLiteral(e: J): e is J.NewClass {
|
|
394
|
+
return e.kind === J.Kind.NewClass && !(e as J.NewClass).class && !!(e as J.NewClass).body;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function objectProperties(o: J.NewClass): any[] {
|
|
398
|
+
return (o.body?.statements ?? []).map(s => s.element).filter(s => s.kind !== J.Kind.Empty);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function propertyKey(name: string): string {
|
|
402
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : `'${name}'`;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** `spy` in `spy.calls`. */
|
|
406
|
+
function callsSpy(e: J | undefined): J | undefined {
|
|
407
|
+
if (e && isFieldAccess(e) && e.name.element.simpleName === 'calls') return e.target;
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** `spy` in `spy.calls.<method>(...)`. */
|
|
412
|
+
function callsTarget(m: J.MethodInvocation, method: string): J | undefined {
|
|
413
|
+
return m.name.simpleName === method ? callsSpy(select(m)) : undefined;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The spy a `spy.and` reads the strategy of. */
|
|
417
|
+
function unwrapAnd(e: J | undefined): { spy: J, node: J } | undefined {
|
|
418
|
+
if (e && isFieldAccess(e) && e.name.element.simpleName === 'and') return {spy: e.target, node: e};
|
|
419
|
+
if (e && isElementAccess(e, 'and')) return {spy: (e as J.ArrayAccess).indexed, node: e};
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* `spy.and.returnValue(x)` to `spy.mockReturnValue(x)`, edited in place so that the comments and
|
|
425
|
+
* line breaks of the chain stay where they were.
|
|
426
|
+
*/
|
|
427
|
+
function renameStrategy(m: J.MethodInvocation, and: J, spy: J, name: string, argumentsOf: J.MethodInvocation): J {
|
|
428
|
+
// The space before `.and` is where a chain broken over lines keeps its newline and comments.
|
|
429
|
+
const before = isFieldAccess(and) ? and.name.before : m.select!.after;
|
|
430
|
+
return create(m, d => {
|
|
431
|
+
d.select = {...d.select!, element: spy as any, after: before as any};
|
|
432
|
+
d.name.simpleName = name;
|
|
433
|
+
d.arguments = argumentsOf.arguments as any;
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** `target['name']`. */
|
|
438
|
+
function isElementAccess(e: J, name: string): boolean {
|
|
439
|
+
if (e.kind !== J.Kind.ArrayAccess) return false;
|
|
440
|
+
const index = (e as J.ArrayAccess).dimension.index.element;
|
|
441
|
+
return isStringLiteral(index) && index.value === name;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function chainContainsWithArgs(e: J): boolean {
|
|
445
|
+
let current: J | undefined = unwrap(e);
|
|
446
|
+
while (current) {
|
|
447
|
+
if (isMethodInvocation(current)) {
|
|
448
|
+
if (current.name.simpleName === 'withArgs') return true;
|
|
449
|
+
current = unwrap(select(current));
|
|
450
|
+
} else if (isFieldAccess(current)) {
|
|
451
|
+
current = unwrap(current.target);
|
|
452
|
+
} else {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** Whether a spy is being created here, before Vitest's names for it or after. */
|
|
460
|
+
function isSpyCreation(e: J): boolean {
|
|
461
|
+
let current = unwrap(e);
|
|
462
|
+
// `vi.fn().mockName('x')`, as `jasmine.createSpy('x')` is rewritten.
|
|
463
|
+
if (current && isMethodInvocation(current) && current.name.simpleName === 'mockName') {
|
|
464
|
+
current = unwrap(select(current));
|
|
465
|
+
}
|
|
466
|
+
if (!current || !isMethodInvocation(current)) return false;
|
|
467
|
+
const call: J.MethodInvocation = current;
|
|
468
|
+
if (isGlobalCall(call as J, 'spyOn', 'spyOnProperty')) return true;
|
|
469
|
+
if (isNamed(select(call), 'vi') && (call.name.simpleName === 'spyOn' || call.name.simpleName === 'fn')) return true;
|
|
470
|
+
return jasmineMethodName(call) === 'createSpy';
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const PROMISE_RESOLVE = new MethodMatcher('Promise resolve(..)');
|
|
474
|
+
const PROMISE_REJECT = new MethodMatcher('Promise reject(..)');
|
|
475
|
+
|
|
476
|
+
function promiseResolveReject(e: J | undefined): { method: 'resolve' | 'reject', args: J[], call: J.MethodInvocation } | undefined {
|
|
477
|
+
if (!e || !isMethodInvocation(e)) return undefined;
|
|
478
|
+
if (PROMISE_RESOLVE.matches(e.methodType)) return {method: 'resolve', args: args(e), call: e};
|
|
479
|
+
if (PROMISE_REJECT.matches(e.methodType)) return {method: 'reject', args: args(e), call: e};
|
|
480
|
+
return undefined;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** The trees above the cursor's, past padding and wrappers that do not change a value. */
|
|
484
|
+
function ancestors(cursor: Cursor): { node: any, child: any }[] {
|
|
485
|
+
const out: { node: any, child: any }[] = [];
|
|
486
|
+
let child = cursor.value;
|
|
487
|
+
for (let c = cursor.parent; c; c = c.parent) {
|
|
488
|
+
const v = c.value;
|
|
489
|
+
if (!v || typeof v !== 'object' || typeof v.kind !== 'string') continue;
|
|
490
|
+
if (v.kind.endsWith('RightPadded') || v.kind.endsWith('LeftPadded') || v.kind.endsWith('Container')) continue;
|
|
491
|
+
out.push({node: v, child});
|
|
492
|
+
child = v;
|
|
493
|
+
}
|
|
494
|
+
return out;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** Whether `.and` follows the spy at the cursor, through any `withArgs(...)` and wrappers. */
|
|
498
|
+
function isChainedWithAnd(cursor: Cursor): boolean {
|
|
499
|
+
for (const {node, child} of ancestors(cursor)) {
|
|
500
|
+
if (isFieldAccess(node) && node.target === child) {
|
|
501
|
+
if (node.name.element.simpleName === 'and') return true;
|
|
502
|
+
return false;
|
|
503
|
+
}
|
|
504
|
+
if (node.kind === J.Kind.ArrayAccess && node.indexed === child) {
|
|
505
|
+
return isElementAccess(node, 'and');
|
|
506
|
+
}
|
|
507
|
+
if (isMethodInvocation(node) && node.select?.element === child && node.name.simpleName === 'withArgs') continue;
|
|
508
|
+
if (isTransparentWrapper(node)) continue;
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function nearestAncestor(cursor: Cursor): { node: any, child: any } | undefined {
|
|
515
|
+
for (const a of ancestors(cursor)) {
|
|
516
|
+
if (!isTransparentWrapper(a.node)) return a;
|
|
517
|
+
}
|
|
518
|
+
return undefined;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** Whether the call at the cursor is read for `.args`, `.returnValue` or `.object`. */
|
|
522
|
+
function readsCallInfoProperty(cursor: Cursor): boolean {
|
|
523
|
+
const parent = nearestAncestor(cursor)?.node;
|
|
524
|
+
return !!parent && isFieldAccess(parent) && CALL_INFO_PROPERTIES.has(parent.name.element.simpleName);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Whether the `spy.calls.all()` at the cursor is `spy.calls.all()[i].args` or the like. */
|
|
528
|
+
function isIndexedForCallInfo(cursor: Cursor): boolean {
|
|
529
|
+
const list = ancestors(cursor).filter(a => !isTransparentWrapper(a.node));
|
|
530
|
+
const [access, property] = list;
|
|
531
|
+
return !!access && access.node.kind === J.Kind.ArrayAccess && access.node.indexed !== undefined
|
|
532
|
+
&& !!property && isFieldAccess(property.node) && CALL_INFO_PROPERTIES.has(property.node.name.element.simpleName);
|
|
533
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {Option, Recipe} from "@openrewrite/rewrite";
|
|
8
|
+
import {MigrateJasmineLifecycleToVitest} from "./migrate-jasmine-lifecycle";
|
|
9
|
+
import {MigrateJasmineMatchersToVitest} from "./migrate-jasmine-matchers";
|
|
10
|
+
import {MigrateJasmineSpiesToVitest} from "./migrate-jasmine-spies";
|
|
11
|
+
import {MigrateFakeAsyncToVitestTimers} from "./migrate-fake-async";
|
|
12
|
+
import {MigrateJasmineMiscToVitest} from "./migrate-jasmine-misc";
|
|
13
|
+
import {MigrateJasmineTypesToVitest} from "./migrate-jasmine-types";
|
|
14
|
+
import {AddVitestImports} from "./add-vitest-imports";
|
|
15
|
+
|
|
16
|
+
export class MigrateJasmineToVitest extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.angular.MigrateJasmineToVitest";
|
|
18
|
+
readonly displayName = "Migrate Jasmine tests to Vitest";
|
|
19
|
+
readonly description = "Rewrites the Jasmine APIs in an Angular project's tests (`*.spec.ts` files and the helpers " +
|
|
20
|
+
"that name `jasmine`) to Vitest, as the Angular CLI's `ng g @schematics/angular:refactor-jasmine-vitest` " +
|
|
21
|
+
"schematic does: spies and their strategies, matchers, focused, skipped and pending tests, `done` callbacks, " +
|
|
22
|
+
"`jasmine.clock()`, `fail()` and the `jasmine` types. What has no Vitest equivalent is left with a " +
|
|
23
|
+
"`// TODO: vitest-migration:` comment for a person. Files that name `jest` are left to the Jest migration. " +
|
|
24
|
+
"This covers the test code only: moving the workspace's builder, `tsconfig.spec.json` and dependencies off " +
|
|
25
|
+
"Karma is `org.openrewrite.angular.MigrateKarmaToVitest`'s part.";
|
|
26
|
+
|
|
27
|
+
@Option({
|
|
28
|
+
displayName: "Add imports",
|
|
29
|
+
description: "Whether to import the Vitest globals a test uses, such as `describe`, `it`, `expect` and `vi`. " +
|
|
30
|
+
"Angular's `unit-test` builder runs Vitest with globals on, so they are not needed unless a project turned them off.",
|
|
31
|
+
required: false,
|
|
32
|
+
example: "false"
|
|
33
|
+
})
|
|
34
|
+
addImports?: boolean;
|
|
35
|
+
|
|
36
|
+
@Option({
|
|
37
|
+
displayName: "Browser mode",
|
|
38
|
+
description: "Whether the tests run in Vitest's browser mode, which has a `toHaveClass` matcher of its own.",
|
|
39
|
+
required: false,
|
|
40
|
+
example: "false"
|
|
41
|
+
})
|
|
42
|
+
browserMode?: boolean;
|
|
43
|
+
|
|
44
|
+
@Option({
|
|
45
|
+
displayName: "Migrate fakeAsync",
|
|
46
|
+
description: "Whether to rewrite `fakeAsync`, `tick`, `flush` and `flushMicrotasks` to Vitest fake timers. " +
|
|
47
|
+
"Left off, they keep running under zone.js's Vitest patch.",
|
|
48
|
+
required: false,
|
|
49
|
+
example: "false"
|
|
50
|
+
})
|
|
51
|
+
fakeAsync?: boolean;
|
|
52
|
+
|
|
53
|
+
constructor(options?: { addImports?: boolean, browserMode?: boolean, fakeAsync?: boolean }) {
|
|
54
|
+
super(options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async recipeList(): Promise<Recipe[]> {
|
|
58
|
+
return [
|
|
59
|
+
// `fit` and `xdescribe` first, so the recipes below see `it.only` and `describe.skip`.
|
|
60
|
+
new MigrateJasmineLifecycleToVitest(),
|
|
61
|
+
// Matchers before spies: `jasmine.any` inside a `createSpyObj` map is `expect.any` by then.
|
|
62
|
+
new MigrateJasmineMatchersToVitest({browserMode: !!this.browserMode}),
|
|
63
|
+
new MigrateJasmineSpiesToVitest(),
|
|
64
|
+
...(this.fakeAsync ? [new MigrateFakeAsyncToVitestTimers()] : []),
|
|
65
|
+
// Last of the value rewrites: what is still `jasmine.*` by now is what no recipe handles.
|
|
66
|
+
new MigrateJasmineMiscToVitest(),
|
|
67
|
+
...(this.addImports ? [new AddVitestImports()] : []),
|
|
68
|
+
// After the value imports, so `type Mock` joins an `import { vi } from 'vitest'` already there.
|
|
69
|
+
new MigrateJasmineTypesToVitest(),
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
}
|