Repository: skovhus/jest-codemods
Pull request #157 (2019-03-29 16:07:55)

jasmine-globals: Support *.calls.mostRecent()
Supports `*.calls.mostRecent()`

https://jasmine.github.io/2.9/introduction#section-36

Replaces it with `*.mock.calls[*.mock.calls.length - 1]`, the same approach currently used by the `*.mostRecentCall` logic

Jest calls docs: https://jestjs.io/docs/en/mock-function-api#mockfnmockcalls


Patch:
diff --git a/src/transformers/jasmine-globals.js b/src/transformers/jasmine-globals.js
index 8c17765..210bb52 100644
--- a/src/transformers/jasmine-globals.js
+++ b/src/transformers/jasmine-globals.js
@@ -177,7 +177,7 @@ export default function jasmineGlobals(fileInfo, api, options) {
             },
         })
         .forEach(path => {
-            // turn it into `stuff.mock.calls[stuff.mock.calls.length - 1]
+            // turn it into `stuff.mock.calls[stuff.mock.calls.length - 1]`
             path.node.object = j.memberExpression(path.node.object, j.identifier('mock'));
             path.node.object = j.memberExpression(
                 path.node.object,
@@ -191,6 +191,44 @@ export default function jasmineGlobals(fileInfo, api, options) {
             path.node.computed = true;
         });
 
+    root
+        // find `*.calls.mostRecent()`
+        .find(j.CallExpression, {
+            callee: {
+                type: 'MemberExpression',
+                object: {
+                    type: 'MemberExpression',
+                    property: {
+                        type: 'Identifier',
+                        name: 'calls',
+                    },
+                },
+                property: {
+                    type: 'Identifier',
+                    name: 'mostRecent',
+                },
+            },
+        })
+        .forEach(path => {
+            const expressionMockCalls = j.memberExpression(
+                j.memberExpression(path.node.callee.object.object, j.identifier('mock')),
+                j.identifier('calls')
+            );
+
+            // turn it into `*.mock.calls[*.mock.calls.length - 1]`
+            j(path).replaceWith(
+                j.memberExpression(
+                    expressionMockCalls,
+                    j.binaryExpression(
+                        '-',
+                        j.memberExpression(expressionMockCalls, j.identifier('length')),
+                        j.literal(1)
+                    ),
+                    true
+                )
+            );
+        });
+
     root
         // find anything that accesses property on `args`
         // like `stuff.mostRecentCall.args[0]`


Test patch:
diff --git a/src/transformers/jasmine-globals.test.js b/src/transformers/jasmine-globals.test.js
index 43e4651..840357d 100644
--- a/src/transformers/jasmine-globals.test.js
+++ b/src/transformers/jasmine-globals.test.js
@@ -46,15 +46,13 @@ testChanged(
 );
 
 testChanged(
-    'mock.calls.count()',
+    '*.calls.count()',
     `
     someMock.calls.count();
     stuff.someMock.calls.count();
     getMock(stuff).calls.count();
     getMock(stuff).calls.count() > 1;
     wyoming.cheyenne.callCount
-    wyoming.cheyenne.stuff.mostRecentCall.args[0]
-    georgia.atlanta.mostRecentCall.args.map(fn)
     args.map()
     oklahoma.argsForCall[0]
     idaho.argsForCall[0][1]
@@ -70,8 +68,6 @@ testChanged(
     getMock(stuff).mock.calls.length;
     getMock(stuff).mock.calls.length > 1;
     wyoming.cheyenne.mock.calls.length
-    wyoming.cheyenne.stuff.mock.calls[wyoming.cheyenne.stuff.mock.calls.length - 1][0]
-    georgia.atlanta.mock.calls[georgia.atlanta.mock.calls.length - 1].map(fn)
     args.map()
     oklahoma.mock.calls[0]
     idaho.mock.calls[0][1]
@@ -83,6 +79,34 @@ testChanged(
     `
 );
 
+testChanged(
+    '*.mostRecentCall',
+    `
+    wyoming.cheyenne.stuff.mostRecentCall.args[0]
+    georgia.atlanta.mostRecentCall.args.map(fn)
+    `,
+    `
+    wyoming.cheyenne.stuff.mock.calls[wyoming.cheyenne.stuff.mock.calls.length - 1][0]
+    georgia.atlanta.mock.calls[georgia.atlanta.mock.calls.length - 1].map(fn)
+    `
+);
+
+testChanged(
+    '*.calls.mostRecent()',
+    `
+    const foo = someMock.calls.mostRecent();
+    someMock.calls.mostRecent()[0];
+
+    foo.mostRecent();
+    `,
+    `
+    const foo = someMock.mock.calls[someMock.mock.calls.length - 1];
+    someMock.mock.calls[someMock.mock.calls.length - 1][0];
+
+    foo.mostRecent();
+    `
+);
+
 testChanged(
     'jasmine.clock()',
     `