Repository: skovhus/jest-codemods
Pull request #168 (2019-05-28 22:18:22)

Support more chai asserts
Replacing the stale PR https://github.com/skovhus/jest-codemods/pull/69/files


Patch:
diff --git a/src/transformers/chai-assert.js b/src/transformers/chai-assert.js
index d511296..5aed199 100644
--- a/src/transformers/chai-assert.js
+++ b/src/transformers/chai-assert.js
@@ -126,6 +126,11 @@ const assertToExpectMapping = [
         assert: 'sameDeepMembers',
         expect: 'toEqual',
     },
+    {
+        assert: 'nestedProperty',
+        expect: 'toHaveProperty',
+        includeNegative: 'notNestedProperty',
+    },
 ];
 
 const objectChecks = [
@@ -164,7 +169,6 @@ const unsupportedAssertions = [
     'doesNotIncrease',
     'decreases',
     'doesNotDecrease',
-    'ifError',
 ];
 
 export default function transformer(fileInfo, api, options) {
@@ -294,6 +298,28 @@ export default function transformer(fileInfo, api, options) {
         );
     });
 
+    // assert.nestedPropertyVal -> expect(obj).toHaveProperty()
+    ast.find(
+        j.CallExpression,
+        getAssertionExpression(chaiAssertExpression, 'nestedPropertyVal')
+    ).replaceWith(path =>
+        makeExpectation('toHaveProperty', path.value.arguments[0], [
+            path.value.arguments[1],
+            path.value.arguments[2],
+        ])
+    );
+
+    // assert.notNestedPropertyVal -> expect(obj).not.toHaveProperty()
+    ast.find(
+        j.CallExpression,
+        getAssertionExpression(chaiAssertExpression, 'notNestedPropertyVal')
+    ).replaceWith(path =>
+        makeNegativeExpectation('toHaveProperty', path.value.arguments[0], [
+            path.value.arguments[1],
+            path.value.arguments[2],
+        ])
+    );
+
     // assert.fail -> expect(false).toBeTruthy()
     ast.find(
         j.CallExpression,
@@ -394,6 +420,12 @@ export default function transformer(fileInfo, api, options) {
         )
     );
 
+    // assert.ifError -> expect(*).toBeFalsy()
+    ast.find(
+        j.CallExpression,
+        getAssertionExpression(chaiAssertExpression, 'ifError')
+    ).replaceWith(path => makeExpectation('toBeFalsy', path.value.arguments[0]));
+
     // assert.includeMembers -> expect([]).toEqual(expect.arrayContaining([]))
     ast.find(
         j.CallExpression,


Test patch:
diff --git a/src/transformers/chai-assert.test.js b/src/transformers/chai-assert.test.js
index 3a8d640..a29d2eb 100644
--- a/src/transformers/chai-assert.test.js
+++ b/src/transformers/chai-assert.test.js
@@ -117,6 +117,13 @@ const mappings = [
     ['assert.propertyVal(foo, bar, baz);', 'expect(foo.bar).toBe(baz);'],
     ['assert.propertyNotVal(foo, bar, baz);', 'expect(foo.bar).not.toBe(baz);'],
     ['assert.notPropertyVal(foo, bar, baz);', 'expect(foo.bar).not.toBe(baz);'],
+    ['assert.nestedProperty(foo, bar);', 'expect(foo).toHaveProperty(bar);'],
+    ['assert.notNestedProperty(foo, bar);', 'expect(foo).not.toHaveProperty(bar);'],
+    ['assert.nestedPropertyVal(foo, bar, baz);', 'expect(foo).toHaveProperty(bar, baz);'],
+    [
+        'assert.notNestedPropertyVal(foo, bar, baz);',
+        'expect(foo).not.toHaveProperty(bar, baz);',
+    ],
     [
         "assert.deepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'matcha' });",
         "expect({ tea: { green: 'matcha' } }).toHaveProperty('tea', { green: 'matcha' });",
@@ -150,6 +157,7 @@ const mappings = [
     ],
     ['assert.sameMembers(foo, bar, baz);', 'expect(foo).toEqual(bar);'],
     ['assert.sameDeepMembers(foo, bar, baz);', 'expect(foo).toEqual(bar);'],
+    ['assert.ifError(foo);', 'expect(foo).toBeFalsy();'],
     [
         'assert.includeMembers([1, 2, 3], [2, 1, 2]);',
         'expect([1, 2, 3]).toEqual(expect.arrayContaining([2, 1, 2]));',
@@ -212,7 +220,6 @@ test('not supported assertions', () => {
         'doesNotIncrease',
         'decreases',
         'doesNotDecrease',
-        'ifError',
     ];
 
     const fileInput = unsupportedAssertions.reduce(
@@ -232,7 +239,6 @@ assert.${assertion}(foo, bar, baz);`,
         'jest-codemods warning: (test.js line 6) Unsupported Chai Assertion "doesNotIncrease".',
         'jest-codemods warning: (test.js line 7) Unsupported Chai Assertion "decreases".',
         'jest-codemods warning: (test.js line 8) Unsupported Chai Assertion "doesNotDecrease".',
-        'jest-codemods warning: (test.js line 9) Unsupported Chai Assertion "ifError".',
     ]);
 });