Repository: skovhus/jest-codemods (ts)

Issue:
jasmine arrayWithExactContents
Given two arrays:
```ts
const array1 = [3, 1, 2];
const array2 = [1, 2, 3];
```

Input:
```ts
expect(array1).toEqual(jasmine.arrayWithExactContents(array2));
```

should be transformed to:
```ts
expect(array1.sort()).toEqual(array2.sort());
```

Pull request:
feat(jasmine): transformation of arrayWithExactContents
fixes #610 

Patch:
diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts
index 5196390..6592d07 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -727,5 +727,62 @@ export default function jasmineGlobals(fileInfo, api, options) {
       }
     })
 
+  root
+    .find(j.CallExpression, {
+      callee: {
+        type: 'MemberExpression',
+        object: {
+          type: 'CallExpression',
+          callee: {
+            type: 'Identifier',
+            name: 'expect',
+          },
+        },
+        property: {
+          type: 'Identifier',
+          name: (name) => name === 'toEqual' || name === 'toStrictEqual',
+        },
+      },
+      arguments: [
+        {
+          type: 'CallExpression',
+          callee: {
+            type: 'MemberExpression',
+            object: {
+              type: 'Identifier',
+              name: 'jasmine',
+            },
+            property: {
+              type: 'Identifier',
+              name: 'arrayWithExactContents',
+            },
+          },
+        },
+      ],
+    })
+    .replaceWith((path) => {
+      const expectArgument = path.value.callee.object.arguments[0]
+      const jasmineArgument = path.value.arguments[0].arguments[0]
+      const methodName = path.value.callee.property.name
+
+      const newExpectArgument = j.callExpression(
+        j.memberExpression(expectArgument, j.identifier('sort')),
+        []
+      )
+
+      const newJasmineArgument = j.callExpression(
+        j.memberExpression(jasmineArgument, j.identifier('sort')),
+        []
+      )
+
+      return j.callExpression(
+        j.memberExpression(
+          j.callExpression(j.identifier('expect'), [newExpectArgument]),
+          j.identifier(methodName)
+        ),
+        [newJasmineArgument]
+      )
+    })
+
   return finale(fileInfo, j, root, options)
 }


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 3f76eb4..6570cef 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -454,6 +454,19 @@ describe('types', () => {
     ))
 })
 
+test('arrayWithExactContents', () => {
+  expectTransformation(
+    `
+    expect(array1).toEqual(jasmine.arrayWithExactContents(array2));
+    expect([3,1,2]).toStrictEqual(jasmine.arrayWithExactContents([1,2,3]));
+    `,
+    `
+    expect(array1.sort()).toEqual(array2.sort());
+    expect([3,1,2].sort()).toStrictEqual([1,2,3].sort());
+    `
+  )
+})
+
 test('return value', () => {
   expectTransformation(
     `