Skip to content

Commit

Permalink
fix(jasmine): returnValues() should be transformed to a chain of mock…
Browse files Browse the repository at this point in the history
…ReturnValueOnce() (#628)

* fix(jasmine): returnValues() should be transformed to a chain of mockReturnValueOnce()

* chore: prettier

* fix: returnValues callee could be variable or expression
  • Loading branch information
jase88 authored Dec 20, 2024
1 parent 33a62f9 commit 681a52c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ test('spyOn', () => {
existingSpy.and.callThrough();
spyOn(something, 'foo').and.stub().and.returnValue(42);
mySpy.and.stub();
anotherSpy.and.returnValues('a', 'b');
`,
`
jest.spyOn().mockReturnValue();
jest.spyOn(stuff);
jest.spyOn(stuff).mockImplementation(() => 'lol');
existingSpy.mockImplementation(() => 'lol');
jest.spyOn(stuff).mockReturnValue('lmao');
jest.spyOn(stuff).mockReturnValue(1).mockReturnValue(2).mockReturnValue(3);
jest.spyOn(stuff).mockReturnValueOnce(1).mockReturnValueOnce(2).mockReturnValueOnce(3);
existingSpy.mockReturnValue('lmao');
jest.spyOn();
jest.spyOn(stuff).mockImplementation(() => {});
Expand All @@ -161,6 +162,7 @@ test('spyOn', () => {
existingSpy.mockRestore();
jest.spyOn(something, 'foo').mockReturnValue(42);
mySpy.mockImplementation(() => {});
anotherSpy.mockReturnValueOnce('a').mockReturnValueOnce('b');
`
)
})
Expand Down
10 changes: 5 additions & 5 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ export default function jasmineGlobals(fileInfo, api, options) {
}

case 'returnValues': {
let mockReturnValuesCall = j.callExpression(
j.memberExpression(j.identifier('jest'), j.identifier('spyOn')),
path.node.callee.object.object.arguments ?? []
)
let mockReturnValuesCall = path.node.callee.object.object

for (const argument of path.node.arguments) {
mockReturnValuesCall = j.callExpression(
j.memberExpression(mockReturnValuesCall, j.identifier('mockReturnValue')),
j.memberExpression(
mockReturnValuesCall,
j.identifier('mockReturnValueOnce')
),
[argument]
)
}
Expand Down

0 comments on commit 681a52c

Please sign in to comment.