Repository: skovhus/jest-codemods
Pull request #194 (2020-06-02 08:08:19)

Add todo conversion support
Closes https://github.com/skovhus/jest-codemods/issues/132


Patch:
diff --git a/src/transformers/ava.ts b/src/transformers/ava.ts
index b146569..1c288c8 100644
--- a/src/transformers/ava.ts
+++ b/src/transformers/ava.ts
@@ -51,8 +51,9 @@ const avaToJestMethods = {
   after: 'afterAll',
   beforeEach: 'beforeEach',
   afterEach: 'afterEach',
-  skip: 'test.skip',
   only: 'test.only',
+  skip: 'test.skip',
+  todo: 'test.todo',
 }
 
 const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {
@@ -164,7 +165,7 @@ const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {
 
       function mapPathToJestCallExpression(p) {
         let jestMethod = 'test'
-        let jestMethodArgs = p.node.arguments
+        const jestMethodArgs = p.node.arguments
 
         // List like ['test', 'serial', 'cb']
         const avaMethods = getMemberExpressionElements(p.node.callee).filter(
@@ -173,12 +174,7 @@ const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {
 
         if (avaMethods.length === 1) {
           const avaMethod = avaMethods[0]
-          if (avaMethod === 'todo') {
-            const firstArg = jestMethodArgs && jestMethodArgs[0]
-            if (firstArg.type === 'Literal') {
-              jestMethodArgs = [j.literal(`TODO: ${firstArg.value}`)]
-            }
-          } else if (avaMethod in avaToJestMethods) {
+          if (avaMethod in avaToJestMethods) {
             jestMethod = avaToJestMethods[avaMethod]
           } else {
             jestMethod = avaMethod
diff --git a/src/transformers/tape.ts b/src/transformers/tape.ts
index 35f0192..5bcce6e 100644
--- a/src/transformers/tape.ts
+++ b/src/transformers/tape.ts
@@ -14,6 +14,8 @@ import {
 const SPECIAL_THROWS_CASE = '(special throws case)'
 const SPECIAL_PLAN_CASE = '(special plan case)'
 
+const SUPPORTED_TEST_METHODS = ['test.skip', 'test.todo']
+
 const tPropertiesMap = {
   ok: 'toBeTruthy',
   true: 'toBeTruthy',
@@ -220,6 +222,10 @@ export default function tapeToJest(fileInfo, api, options) {
                   p.value.callee.name = 'test.skip'
                 }
 
+                if (tapeOptionKey === 'todo' && tapeOptionValue === true) {
+                  p.value.callee.name = 'test.todo'
+                }
+
                 if (tapeOptionKey === 'timeout') {
                   logWarning('"timeout" option is currently not supported', p)
                 }
@@ -231,7 +237,7 @@ export default function tapeToJest(fileInfo, api, options) {
             }
           })
 
-          if (p.node.callee.name !== 'test.skip') {
+          if (!SUPPORTED_TEST_METHODS.includes(p.node.callee.name)) {
             p.node.callee.name = 'test'
           }
 


Test patch:
diff --git a/src/transformers/ava.test.ts b/src/transformers/ava.test.ts
index 347d207..634015c 100644
--- a/src/transformers/ava.test.ts
+++ b/src/transformers/ava.test.ts
@@ -326,7 +326,7 @@ import test from 'ava';
 test.todo('this should be a test some day');
 `,
   `
-test('TODO: this should be a test some day');
+test.todo('this should be a test some day');
 `
 )
 
diff --git a/src/transformers/tape.test.ts b/src/transformers/tape.test.ts
index 18fadc7..df102e3 100644
--- a/src/transformers/tape.test.ts
+++ b/src/transformers/tape.test.ts
@@ -361,6 +361,27 @@ test('my test', () => {
 `
 )
 
+testChanged(
+  `supports the todo option`,
+  `
+import test from 'tape';
+test({todo: true}, t => {
+    t.equal(1, 1);
+});
+test({todo: false}, t => {
+    t.equal(1, 1);
+});
+`,
+  `
+test.todo(() => {
+    expect(1).toBe(1);
+});
+test(() => {
+    expect(1).toBe(1);
+});
+`
+)
+
 test('not supported warnings: createStream', () => {
   wrappedPlugin(`
         import test from 'tape';