Skip to content

Commit

Permalink
fix(jasmine): don't transform already transformed nested jasmine spie…
Browse files Browse the repository at this point in the history
…s again (#618)
  • Loading branch information
jase88 authored Sep 14, 2024
1 parent f12c300 commit ba5fbf7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
43 changes: 24 additions & 19 deletions src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,17 +384,9 @@ describe('createSpyObj', () => {
`,
`
const spyObj = {
'a': jest.fn(() => {
return 42;
}),
'b': jest.fn(() => {
return true;
}),
'c': jest.fn(() => {
return of(undefined);
})
'a': jest.fn(() => 42),
'b': jest.fn(() => true),
'c': jest.fn(() => of(undefined))
};
`
)
Expand All @@ -410,14 +402,8 @@ describe('createSpyObj', () => {
`,
`
const spyObj = {
'a': jest.fn(() => {
return 42;
}),
'b': jest.fn(() => {
return true;
}),
'a': jest.fn(() => 42),
'b': jest.fn(() => true),
'c': null
};
`
Expand Down Expand Up @@ -468,6 +454,25 @@ describe('createSpyObj', () => {
{ parser: 'ts' }
)
})

test('with methods object that contain nested jasmine Spies', () => {
expectTransformation(
`
const spyObj = jasmine.createSpyObj({
a: jasmine.createSpy(),
b: jasmine.createSpy().and.returnValue(true),
c: of(42)
});
`,
`
const spyObj = {
'a': jest.fn(),
'b': jest.fn(() => true),
'c': jest.fn(() => of(42))
};
`
)
})
})

describe('types', () => {
Expand Down
27 changes: 15 additions & 12 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,20 +766,23 @@ export default function jasmineGlobals(fileInfo, api, options) {
)
)
)
: spyObjMethods.properties.map((arg) =>
j.objectProperty(
: spyObjMethods.properties.map((arg) => {
const alreadyTransformed =
arg.value.type === 'CallExpression' &&
arg.value.callee.type === 'MemberExpression' &&
arg.value.callee.object.name === 'jest' &&
arg.value.callee.property.name === 'fn'

return j.objectProperty(
j.literal(arg.key.name),
j.callExpression(
j.memberExpression(j.identifier('jest'), j.identifier('fn')),
[
j.arrowFunctionExpression(
[],
j.blockStatement([j.returnStatement(arg.value)])
),
]
)
!alreadyTransformed
? j.callExpression(
j.memberExpression(j.identifier('jest'), j.identifier('fn')),
[j.arrowFunctionExpression([], arg.value)]
)
: arg.value
)
)
})

if (spyObjProperties !== undefined) {
properties.push(
Expand Down

0 comments on commit ba5fbf7

Please sign in to comment.