Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jasmine): createSpyObj calls with missing (optional) baseName argument #608

Merged
merged 1 commit into from
Aug 29, 2024
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
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
Loading