Repository: skovhus/jest-codemods
Pull request #156 (2019-03-29 15:12:31)

jasmine-globals: Support jasmine.createSpy().and.
Supports `jasmine.createSpy(*).and.*()`:

`callFake`: https://jasmine.github.io/2.9/introduction#section-Spies:_%3Ccode%3Eand.callFake%3C/code%3E

`returnValue`: https://jasmine.github.io/2.9/introduction#section-Spies:_%3Ccode%3Eand.returnValue%3C/code%3E

Maps both to `jest.fn(implementation)`: https://jestjs.io/docs/en/jest-object#jestfnimplementation

Adds warning for all other cases


Patch:
diff --git a/src/transformers/jasmine-globals.js b/src/transformers/jasmine-globals.js
index 8c17765..5a8ed95 100644
--- a/src/transformers/jasmine-globals.js
+++ b/src/transformers/jasmine-globals.js
@@ -14,7 +14,66 @@ export default function jasmineGlobals(fileInfo, api, options) {
     const logWarning = (msg, path) => logger(fileInfo, msg, path);
 
     root
-        // find all `jasmine.createSpy('stuff')
+        // find `jasmine.createSpy(*).and.*()` expressions
+        .find(j.CallExpression, {
+            callee: {
+                type: 'MemberExpression',
+                object: {
+                    type: 'MemberExpression',
+                    property: { name: 'and' },
+                    object: {
+                        type: 'CallExpression',
+                        callee: {
+                            type: 'MemberExpression',
+                            property: {
+                                type: 'Identifier',
+                                name: 'createSpy',
+                            },
+                            object: {
+                                type: 'Identifier',
+                                name: 'jasmine',
+                            },
+                        },
+                    },
+                },
+            },
+        })
+        .forEach(path => {
+            const spyType = path.node.callee.property.name;
+            switch (spyType) {
+                // `jasmine.createSpy().and.callFake(*)` is equivalent of
+                // `jest.fn(*)`
+                case 'callFake': {
+                    path.node.callee = j.memberExpression(
+                        j.identifier('jest'),
+                        j.identifier('fn')
+                    );
+                    break;
+                }
+                // `jasmine.createSpy().and.returnValue(*)` is equivalent of
+                // `jest.fn(() => *)`
+                case 'returnValue': {
+                    path.node.callee = j.memberExpression(
+                        j.identifier('jest'),
+                        j.identifier('fn')
+                    );
+                    path.node.arguments = [
+                        j.arrowFunctionExpression([], path.node.arguments[0]),
+                    ];
+                    break;
+                }
+                default: {
+                    logWarning(
+                        `Unsupported Jasmine functionality "jasmine.createSpy().and.${spyType}".`,
+                        path
+                    );
+                    break;
+                }
+            }
+        });
+
+    root
+        // find all other `jasmine.createSpy` calls
         .find(j.CallExpression, {
             callee: {
                 type: 'MemberExpression',
@@ -114,7 +173,7 @@ export default function jasmineGlobals(fileInfo, api, options) {
         });
 
     root
-        //   find all `SpyOn` calls
+        //   find all `spyOn` calls
         .find(j.CallExpression, {
             callee: { type: 'Identifier', name: 'spyOn' },
         })


Test patch:
diff --git a/src/transformers/jasmine-globals.test.js b/src/transformers/jasmine-globals.test.js
index 43e4651..7d096dd 100644
--- a/src/transformers/jasmine-globals.test.js
+++ b/src/transformers/jasmine-globals.test.js
@@ -29,8 +29,6 @@ testChanged(
     jest.spyOn();
     spyOn(stuff);
     jest.spyOn().mockImplementation();
-    jasmine.createSpy();
-    jasmine.createSpy('lmao');
     `,
     `
     jest.spyOn().mockReturnValue();
@@ -40,11 +38,39 @@ testChanged(
     jest.spyOn();
     jest.spyOn(stuff).mockImplementation(() => {});
     jest.spyOn().mockImplementation();
+    `
+);
+
+testChanged(
+    'jasmine.createSpy',
+    `
+    jasmine.createSpy();
+    jasmine.createSpy('lmao');
+    const spy = jasmine.createSpy();
+    jasmine.createSpy().and.callFake(arg => arg);
+    jasmine.createSpy().and.returnValue('lmao');
+    const spy = jasmine.createSpy().and.returnValue('lmao');
+    `,
+    `
     jest.fn();
     jest.fn();
+    const spy = jest.fn();
+    jest.fn(arg => arg);
+    jest.fn(() => 'lmao');
+    const spy = jest.fn(() => 'lmao');
     `
 );
 
+test('not supported jasmine.createSpy().and.*', () => {
+    wrappedPlugin(`
+        jasmine.createSpy().and.unknownUtil();
+    `);
+
+    expect(consoleWarnings).toEqual([
+        'jest-codemods warning: (test.js line 2) Unsupported Jasmine functionality "jasmine.createSpy().and.unknownUtil".',
+    ]);
+});
+
 testChanged(
     'mock.calls.count()',
     `