Repository: skovhus/jest-codemods
Pull request #155 (2019-03-14 18:58:35)

jasmine-globals: Basic jasmine.clock() support
Adds basic support for `jasmine.clock()` timer mocking by replacing it with jest timers:

Jasmine:
https://jasmine.github.io/api/edge/Clock.html
https://jasmine.github.io/2.9/introduction#section-Jasmine_Clock

Jest:
https://jestjs.io/docs/en/timer-mocks.html


Patch:
diff --git a/src/transformers/jasmine-globals.js b/src/transformers/jasmine-globals.js
index 537ceac..8c17765 100644
--- a/src/transformers/jasmine-globals.js
+++ b/src/transformers/jasmine-globals.js
@@ -3,6 +3,7 @@
  */
 
 import finale from '../utils/finale';
+import logger from '../utils/logger';
 
 export default function jasmineGlobals(fileInfo, api, options) {
     const j = api.jscodeshift;
@@ -10,6 +11,8 @@ export default function jasmineGlobals(fileInfo, api, options) {
 
     const emptyArrowFn = j('() => {}').__paths[0].value.program.body[0].expression;
 
+    const logWarning = (msg, path) => logger(fileInfo, msg, path);
+
     root
         // find all `jasmine.createSpy('stuff')
         .find(j.CallExpression, {
@@ -267,5 +270,69 @@ export default function jasmineGlobals(fileInfo, api, options) {
             j(path).replaceWith(path.node.callee.object);
         });
 
+    root
+        // find all `jasmine.clock()`
+        .find(j.CallExpression, {
+            callee: {
+                type: 'MemberExpression',
+                object: {
+                    type: 'CallExpression',
+                    callee: {
+                        object: {
+                            type: 'Identifier',
+                            name: 'jasmine',
+                        },
+                        property: {
+                            type: 'Identifier',
+                            name: 'clock',
+                        },
+                    },
+                },
+            },
+        })
+        .forEach(path => {
+            const usageType = path.node.callee.property.name;
+            switch (usageType) {
+                case 'install': {
+                    // make it `jest.useFakeTimers()`
+                    path.node.callee = j.memberExpression(
+                        j.identifier('jest'),
+                        j.identifier('useFakeTimers')
+                    );
+                    break;
+                }
+                case 'uninstall': {
+                    // make it `jest.useRealTimers()`
+                    path.node.callee = j.memberExpression(
+                        j.identifier('jest'),
+                        j.identifier('useRealTimers')
+                    );
+                    break;
+                }
+                case 'tick': {
+                    // make it `jest.advanceTimersByTime(ms)`
+                    path.node.callee = j.memberExpression(
+                        j.identifier('jest'),
+                        j.identifier('advanceTimersByTime')
+                    );
+                    break;
+                }
+                case 'mockDate': {
+                    logWarning(
+                        'Unsupported Jasmine functionality "jasmine.clock().mockDate(*)".',
+                        path
+                    );
+                    break;
+                }
+                default: {
+                    logWarning(
+                        `Unsupported Jasmine functionality "jasmine.clock().${usageType}".`,
+                        path
+                    );
+                    break;
+                }
+            }
+        });
+
     return finale(fileInfo, j, root, options);
 }


Test patch:
diff --git a/src/transformers/jasmine-globals.test.js b/src/transformers/jasmine-globals.test.js
index 8de172e..43e4651 100644
--- a/src/transformers/jasmine-globals.test.js
+++ b/src/transformers/jasmine-globals.test.js
@@ -13,6 +13,12 @@ function testChanged(msg, source, expectedOutput) {
     });
 }
 
+let consoleWarnings = [];
+beforeEach(() => {
+    consoleWarnings = [];
+    console.warn = v => consoleWarnings.push(v);
+});
+
 testChanged(
     'spyOn',
     `
@@ -76,3 +82,29 @@ testChanged(
     stuff;
     `
 );
+
+testChanged(
+    'jasmine.clock()',
+    `
+    jasmine.clock().install();
+    jasmine.clock().uninstall();
+    jasmine.clock().tick(50);
+    `,
+    `
+    jest.useFakeTimers();
+    jest.useRealTimers();
+    jest.advanceTimersByTime(50);
+    `
+);
+
+test('not supported jasmine.clock()', () => {
+    wrappedPlugin(`
+        jasmine.clock().mockDate(new Date(2013, 9, 23));
+        jasmine.clock().unknownUtil();
+    `);
+
+    expect(consoleWarnings).toEqual([
+        'jest-codemods warning: (test.js line 2) Unsupported Jasmine functionality "jasmine.clock().mockDate(*)".',
+        'jest-codemods warning: (test.js line 3) Unsupported Jasmine functionality "jasmine.clock().unknownUtil".',
+    ]);
+});