Repository: skovhus/jest-codemods
Pull request #161 (2019-04-04 20:07:04)

jasmine-globals: Support expect.<>(*) functions
These utils should be 1:1

`any`: [Jasmine](https://jasmine.github.io/2.9/introduction#section-Matching_Anything_with_%3Ccode%3Ejasmine.any%3C/code%3E) / [Jest](https://jestjs.io/docs/en/expect#expectany)

`anything`: [Jasmine](https://jasmine.github.io/2.9/introduction#section-Matching_existence_with_%3Ccode%3Ejasmine.anything%3C/code%3E) / [Jest](https://jestjs.io/docs/en/expect#expectanything)

`arrayContaining `: [Jasmine](https://jasmine.github.io/2.9/introduction#section-Partial_Array_Matching_with_%3Ccode%3Ejasmine.arrayContaining%3C/code%3E) / [Jest](https://jestjs.io/docs/en/expect#expectarraycontainingarray)

`objectContaining `: [Jasmine](https://jasmine.github.io/2.9/introduction#section-Partial_Matching_with_%3Ccode%3Ejasmine.objectContaining%3C/code%3E) / [Jest](https://jestjs.io/docs/en/expect#expectobjectcontainingobject)

`stringMatching `: [Jasmine](https://jasmine.github.io/2.9/introduction#section-String_Matching_with_%3Ccode%3Ejasmine.stringMatching%3C/code%3E) / [Jest](https://jestjs.io/docs/en/expect#expectstringmatchingstring-regexp)


skovhus: I’m thinking if it males sense to run the Jasmine global transformer as part of the mocha transformer. We already run the `jasmine-this` transformer. Thoughts?
JHilker: Hmm, I'm not sure how much sense that makes, I'm not super familiar with the Mocha testing framework, but I'd expect there are enough differences that you'd want to run this on demand if it applied to you? I'd be hesitant to add it by default without some extensive testing.

> We already run the `jasmine-this` transformer. 

It looked like this was a bit of a special case since the `this` context was a bit more universal and language agnostic. I also haven't hit a case I needed this transform yet, so I haven't dug into it too much.

Patch:
diff --git a/README.md b/README.md
index b351964..2cbbe6a 100644
--- a/README.md
+++ b/README.md
@@ -33,32 +33,23 @@ We strive to make the migration as smooth as possible, but some manual intervent
 and tweaks to your tests are to be expected.
 
 
-## Install
-
-```
-$ npm install -g jest-codemods
-```
-
-This installs the runner as `jest-codemods`.
-
-
 ## Usage (CLI)
 
 
 To use the interactive CLI run
 
-	$ jest-codemods
-
+	$ npx jest-codemods
 
+If you do not have `npx` installed, you can install the `jest-codemods` command globally by running `npm install -g jest-codemods`.
 
 For more options
 ```
-$ jest-codemods --help
+$ npx jest-codemods --help
 
     Codemods for migrating test files to Jest.
 
     Usage
-      $ jest-codemods <path> [options]
+      $ npx jest-codemods <path> [options]
 
     path	        Files or directory to transform. Can be a glob like src/**.test.js
 
diff --git a/src/cli/index.js b/src/cli/index.js
index 6232a27..b9c7161 100644
--- a/src/cli/index.js
+++ b/src/cli/index.js
@@ -13,7 +13,7 @@ const cli = meow(
         description: 'Codemods for migrating test files to Jest.',
         help: `
     Usage
-      $ jest-codemods <path> [options]
+      $ npx jest-codemods <path> [options]
 
     path    Files or directory to transform. Can be a glob like src/**.test.js
 
diff --git a/src/transformers/jasmine-globals.js b/src/transformers/jasmine-globals.js
index 3ad0c4c..cc0f7f3 100644
--- a/src/transformers/jasmine-globals.js
+++ b/src/transformers/jasmine-globals.js
@@ -465,5 +465,36 @@ export default function jasmineGlobals(fileInfo, api, options) {
             }
         });
 
+    const jasmineToExpectFunctionNames = [
+        'any',
+        'anything',
+        'arrayContaining',
+        'objectContaining',
+        'stringMatching',
+    ];
+
+    jasmineToExpectFunctionNames.forEach(functionName => {
+        // jasmine.<jasmineToExpectFunctionName>(*)
+        root
+            .find(j.CallExpression, {
+                callee: {
+                    type: 'MemberExpression',
+                    object: {
+                        type: 'Identifier',
+                        name: 'jasmine',
+                    },
+                    property: {
+                        type: 'Identifier',
+                        name: functionName,
+                    },
+                },
+            })
+            .forEach(path => {
+                // `jasmine.<jasmineToExpectFunctionName>(*)` is equivalent of
+                // `expect.<jasmineToExpectFunctionName>(*)`
+                path.node.callee.object.name = 'expect';
+            });
+    });
+
     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 c8c377d..e997402 100644
--- a/src/transformers/jasmine-globals.test.js
+++ b/src/transformers/jasmine-globals.test.js
@@ -178,3 +178,21 @@ test('not supported jasmine.clock()', () => {
         'jest-codemods warning: (test.js line 3) Unsupported Jasmine functionality "jasmine.clock().unknownUtil".',
     ]);
 });
+
+testChanged(
+    'jasmine.<jasmineToExpectFunctionName>(*)',
+    `
+    jasmine.any(Function);
+    jasmine.anything();
+    jasmine.arrayContaining(['foo']);
+    jasmine.objectContaining({ foo: 'bar' });
+    jasmine.stringMatching('text');
+    `,
+    `
+    expect.any(Function);
+    expect.anything();
+    expect.arrayContaining(['foo']);
+    expect.objectContaining({ foo: 'bar' });
+    expect.stringMatching('text');
+    `
+);