Skip to content

Commit e0c97cd

Browse files
authored
feat(jasmine): transformation of arrayWithExactContents (#612)
1 parent a332b86 commit e0c97cd

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/transformers/jasmine-globals.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,19 @@ describe('types', () => {
454454
))
455455
})
456456

457+
test('arrayWithExactContents', () => {
458+
expectTransformation(
459+
`
460+
expect(array1).toEqual(jasmine.arrayWithExactContents(array2));
461+
expect([3,1,2]).toStrictEqual(jasmine.arrayWithExactContents([1,2,3]));
462+
`,
463+
`
464+
expect(array1.sort()).toEqual(array2.sort());
465+
expect([3,1,2].sort()).toStrictEqual([1,2,3].sort());
466+
`
467+
)
468+
})
469+
457470
test('return value', () => {
458471
expectTransformation(
459472
`

src/transformers/jasmine-globals.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,5 +727,62 @@ export default function jasmineGlobals(fileInfo, api, options) {
727727
}
728728
})
729729

730+
root
731+
.find(j.CallExpression, {
732+
callee: {
733+
type: 'MemberExpression',
734+
object: {
735+
type: 'CallExpression',
736+
callee: {
737+
type: 'Identifier',
738+
name: 'expect',
739+
},
740+
},
741+
property: {
742+
type: 'Identifier',
743+
name: (name) => name === 'toEqual' || name === 'toStrictEqual',
744+
},
745+
},
746+
arguments: [
747+
{
748+
type: 'CallExpression',
749+
callee: {
750+
type: 'MemberExpression',
751+
object: {
752+
type: 'Identifier',
753+
name: 'jasmine',
754+
},
755+
property: {
756+
type: 'Identifier',
757+
name: 'arrayWithExactContents',
758+
},
759+
},
760+
},
761+
],
762+
})
763+
.replaceWith((path) => {
764+
const expectArgument = path.value.callee.object.arguments[0]
765+
const jasmineArgument = path.value.arguments[0].arguments[0]
766+
const methodName = path.value.callee.property.name
767+
768+
const newExpectArgument = j.callExpression(
769+
j.memberExpression(expectArgument, j.identifier('sort')),
770+
[]
771+
)
772+
773+
const newJasmineArgument = j.callExpression(
774+
j.memberExpression(jasmineArgument, j.identifier('sort')),
775+
[]
776+
)
777+
778+
return j.callExpression(
779+
j.memberExpression(
780+
j.callExpression(j.identifier('expect'), [newExpectArgument]),
781+
j.identifier(methodName)
782+
),
783+
[newJasmineArgument]
784+
)
785+
})
786+
730787
return finale(fileInfo, j, root, options)
731788
}

0 commit comments

Comments
 (0)