Skip to content

Commit

Permalink
fix(jasmine): createSpyObj calls with missing (optional) baseName arg…
Browse files Browse the repository at this point in the history
…ument should be handled (#608)
  • Loading branch information
jase88 authored Aug 29, 2024
1 parent d62e50d commit 3d2b8f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,33 @@ describe('createSpyObj', () => {
`
)
})

test('without base name and methods names only', () => {
expectTransformation(
`
const spyObj = jasmine.createSpyObj(['someMethod']);
`,
`
const spyObj = {
'someMethod': jest.fn()
};
`
)
})

test('without base name and methods names as well as properties object', () => {
expectTransformation(
`
const spyObj = jasmine.createSpyObj(['someMethod'], {property: true});
`,
`
const spyObj = {
'someMethod': jest.fn(),
'property': true
};
`
)
})
})

test('return value', () => {
Expand Down
16 changes: 12 additions & 4 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,21 @@ export default function jasmineGlobals(fileInfo, api, options) {
})
.filter((path) => {
const args = path.node.arguments
const isArrayOrObjectExpression = (arg) =>
arg.type === 'ArrayExpression' || arg.type === 'ObjectExpression'

const firstArgumentIsMethodNames = isArrayOrObjectExpression(args[0])
if (firstArgumentIsMethodNames) {
// ensure that optional baseName is always filled, to simplify arguments handling
args.unshift(j.literal(''))
}

const [, spyObjMethods, spyObjProperties] = args

return (
(args.length === 2 || args.length === 3) &&
(args[1].type === 'ArrayExpression' || args[1].type === 'ObjectExpression') &&
(args[2] === undefined ||
args[2].type === 'ArrayExpression' ||
args[2].type === 'ObjectExpression')
isArrayOrObjectExpression(spyObjMethods) &&
(spyObjProperties === undefined || isArrayOrObjectExpression(spyObjProperties))
)
})
.forEach((path) => {
Expand Down

0 comments on commit 3d2b8f2

Please sign in to comment.