Skip to content
Merged
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 @@ -103,11 +103,18 @@ function transformPrimarySpy(node: ts.CallExpression, refactorCtx: RefactorConte
) {
addVitestValueImport(pendingVitestValueImports, 'vi');

// `spyOnProperty` defaults to the getter while `vi.spyOn` needs the access type to spy on
// a property instead of a method.
const args =
node.expression.text === 'spyOnProperty' && node.arguments.length === 2
? [...node.arguments, ts.factory.createStringLiteral('get', true)]
: node.arguments;

const viSpyOnCall = ts.factory.updateCallExpression(
node,
createPropertyAccess('vi', 'spyOn'),
node.typeArguments,
node.arguments,
args,
);

if (isChainedWithAnd(node)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ describe('Jasmine to Vitest Transformer - transformSpies', () => {
},
{
description:
'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop").mockReturnValue(undefined)',
'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop", "get").mockReturnValue(undefined)',
input: `spyOnProperty(service, 'myProp');`,
expected: `vi.spyOn(service, 'myProp').mockReturnValue(undefined);`,
expected: `vi.spyOn(service, 'myProp', 'get').mockReturnValue(undefined);`,
},
{
description: 'should default the access type of a chained spyOnProperty to "get"',
input: `spyOnProperty(service, 'myProp').and.returnValue(42);`,
expected: `vi.spyOn(service, 'myProp', 'get').mockReturnValue(42);`,
},
{
description: 'should keep an explicit spyOnProperty access type',
input: `spyOnProperty(service, 'myProp', 'set');`,
expected: `vi.spyOn(service, 'myProp', 'set').mockReturnValue(undefined);`,
},
{
description: 'should transform .and.stub() to .mockImplementation(() => {})',
Expand Down
Loading