Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import ts from 'typescript';
import {
addVitestTypeImport,
addVitestValueImport,
createPropertyAccess,
getPromiseResolveRejectMethod,
Expand Down Expand Up @@ -459,7 +460,118 @@ export function transformCreateSpyObj(node: ts.Node, ctx: RefactorContext): ts.N
}
}

return ts.factory.createObjectLiteralExpression(properties, true);
const spyObjLiteral = ts.factory.createObjectLiteralExpression(properties, true);

let parent = node.parent;
while (parent && ts.isParenthesizedExpression(parent)) {
parent = parent.parent;
}

// An existing cast on the call already gives the literal its type.
if (parent && (ts.isAsExpression(parent) || ts.isTypeAssertionExpression(parent))) {
return spyObjLiteral;
}

// The literal only has the spied members, so it is not assignable to the `MockedObject<T>`
// that a `jasmine.SpyObj<T>` annotation is transformed to. Jasmine's untyped `createSpyObj`
// returns `any`, which is why the original assignment compiled.
const spyObjTypeArguments = node.typeArguments ?? getAssignedSpyObjTypeArguments(parent);
if (!spyObjTypeArguments) {
return spyObjLiteral;
}

addVitestTypeImport(ctx.pendingVitestTypeImports, 'MockedObject');

return ts.factory.createAsExpression(
spyObjLiteral,
ts.factory.createTypeReferenceNode('MockedObject', spyObjTypeArguments),
);
}

function getSpyObjTypeArguments(
type: ts.TypeNode | undefined,
): ts.NodeArray<ts.TypeNode> | undefined {
// `jasmine.SpyObj<T> | undefined` under `strictNullChecks`.
if (type && ts.isUnionTypeNode(type)) {
for (const member of type.types) {
const typeArguments = getSpyObjTypeArguments(member);
if (typeArguments) {
return typeArguments;
}
}

return undefined;
}

if (
type &&
ts.isTypeReferenceNode(type) &&
Comment thread
thekhegay marked this conversation as resolved.
Resolved
ts.isQualifiedName(type.typeName) &&
ts.isIdentifier(type.typeName.left) &&
type.typeName.left.text === 'jasmine' &&
type.typeName.right.text === 'SpyObj'
) {
return type.typeArguments;
}

return undefined;
}

/**
* Finds the declaration of a variable in the blocks that enclose a node, nearest first,
* so that a variable declared in another `describe` block with the same name is not used.
*/
function findEnclosingVariableDeclaration(
node: ts.Node,
name: string,
): ts.VariableDeclaration | undefined {
for (let current = node.parent; current; current = current.parent) {
if (!ts.isBlock(current) && !ts.isSourceFile(current)) {
continue;
}

for (const statement of current.statements) {
if (!ts.isVariableStatement(statement)) {
continue;
}

const declaration = statement.declarationList.declarations.find(
(d) => ts.isIdentifier(d.name) && d.name.text === name,
);
if (declaration) {
return declaration;
}
}
}

return undefined;
}

/**
* Finds the `T` of a `jasmine.SpyObj<T>` annotation on the variable that a `createSpyObj` call
* initializes (`const spy: jasmine.SpyObj<T> = ...`) or is assigned to (`spy = ...`), given the
* call's parent outside any parentheses.
*/
function getAssignedSpyObjTypeArguments(
parent: ts.Node | undefined,
): ts.NodeArray<ts.TypeNode> | undefined {
if (!parent) {
return undefined;
}

if (ts.isVariableDeclaration(parent)) {
return getSpyObjTypeArguments(parent.type);
}

if (
!ts.isBinaryExpression(parent) ||
parent.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
!ts.isIdentifier(parent.left)
) {
return undefined;
}

return getSpyObjTypeArguments(findEnclosingVariableDeclaration(parent, parent.left.text)?.type);
}

function createSpyObjWithArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,118 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,

describe('transformCreateSpyObj', () => {
const testCases = [
{
description:
'should cast jasmine.createSpyObj to MockedObject when assigned to a jasmine.SpyObj variable',
input: `
let myService: jasmine.SpyObj<MyService>;
myService = jasmine.createSpyObj('MyService', ['methodA']);
`,
expected: `
import type { MockedObject } from 'vitest';

let myService: MockedObject<MyService>;
myService = {
methodA: vi.fn().mockName('MyService.methodA'),
} as MockedObject<MyService>;
`,
},
{
description:
'should cast jasmine.createSpyObj to MockedObject when it initializes a jasmine.SpyObj variable',
input: `const myService: jasmine.SpyObj<MyService> = jasmine.createSpyObj('MyService', ['methodA']);`,
expected: `
import type { MockedObject } from 'vitest';
const myService: MockedObject<MyService> = {
methodA: vi.fn().mockName('MyService.methodA'),
} as MockedObject<MyService>;
`,
},
{
description:
'should cast jasmine.createSpyObj to MockedObject when assigned to a jasmine.SpyObj union variable',
input: `
let myService: jasmine.SpyObj<MyService> | undefined;
myService = jasmine.createSpyObj('MyService', ['methodA']);
`,
expected: `
import type { MockedObject } from 'vitest';

let myService: MockedObject<MyService> | undefined;
myService = {
methodA: vi.fn().mockName('MyService.methodA'),
} as MockedObject<MyService>;
`,
},
{
description:
'should cast jasmine.createSpyObj with an explicit type argument to MockedObject',
input: `const myService = jasmine.createSpyObj<MyService>('MyService', ['methodA']);`,
expected: `
import type { MockedObject } from 'vitest';
const myService = {
methodA: vi.fn().mockName('MyService.methodA'),
} as MockedObject<MyService>;
`,
},
{
description:
'should not add a second cast to a jasmine.createSpyObj call that is already cast',
input: `
let myService: jasmine.SpyObj<MyService>;
myService = jasmine.createSpyObj('MyService', ['methodA']) as jasmine.SpyObj<MyService>;
const other = jasmine.createSpyObj<Other>('Other', ['methodB']) as any;
`,
expected: `
import type { MockedObject } from 'vitest';

let myService: MockedObject<MyService>;
myService = {
methodA: vi.fn().mockName('MyService.methodA'),
} as MockedObject<MyService>;
const other = {
methodB: vi.fn().mockName('Other.methodB'),
} as any;
`,
},
{
description:
'should cast jasmine.createSpyObj to the jasmine.SpyObj type of the variable in the enclosing describe block',
input: `
describe('Foo', () => {
let svc: jasmine.SpyObj<Foo>;
beforeEach(() => {
svc = jasmine.createSpyObj('Foo', ['a']);
});
});
describe('Bar', () => {
let svc: jasmine.SpyObj<Bar>;
beforeEach(() => {
svc = jasmine.createSpyObj('Bar', ['b']);
});
});
`,
expected: `
import type { MockedObject } from 'vitest';

describe('Foo', () => {
let svc: MockedObject<Foo>;
beforeEach(() => {
svc = {
a: vi.fn().mockName('Foo.a'),
} as MockedObject<Foo>;
});
});
describe('Bar', () => {
let svc: MockedObject<Bar>;
beforeEach(() => {
svc = {
b: vi.fn().mockName('Bar.b'),
} as MockedObject<Bar>;
});
});
`,
},
{
description: 'should transform jasmine.createSpyObj with an array of methods',
input: `const myService = jasmine.createSpyObj('MyService', ['methodA', 'methodB']);`,
Expand Down
Loading