Repository: skovhus/jest-codemods
Pull request #234 (2021-12-18 22:04:51)

Support for chai-arrays plugin
Closes https://github.com/skovhus/jest-codemods/issues/233


Patch:
diff --git a/src/transformers/chai-should.ts b/src/transformers/chai-should.ts
index 017d150..9d29c8a 100644
--- a/src/transformers/chai-should.ts
+++ b/src/transformers/chai-should.ts
@@ -55,7 +55,21 @@ const fns = [
   'gte',
   'lte',
   'within',
-]
+
+  // https://www.chaijs.com/plugins/chai-arrays/ plugin:
+  'array',
+  'Uint8Array',
+  'Uint16Array',
+  'Uint32Array',
+  'Uint8ClampedArray',
+
+  'ofSize',
+  'equalTo',
+  'containing',
+  'containingAllOf',
+  // TODO: containingAnyOf
+  // TODO: sorted
+].map((name) => name.toLowerCase())
 
 const members = [
   'ok',
@@ -510,7 +524,9 @@ export default function transformer(fileInfo, api, options) {
         const numberOfArgs = args.length
         const [firstArg] = args
 
-        switch (p.value.callee.property.name.toLowerCase()) {
+        const propertyName = value.callee.property.name.toLowerCase()
+
+        switch (propertyName) {
           case 'eq':
           case 'equal':
           case 'equals':
@@ -570,13 +586,30 @@ export default function transformer(fileInfo, api, options) {
               false
             )
           }
+          case 'containing':
+            return createCall('toContain', args, rest, containsNot)
+          case 'containingallof':
+            return createCall(
+              'toEqual',
+              [
+                createCallChain(
+                  containsNot
+                    ? ['expect', 'not', 'arrayContaining']
+                    : ['expect', 'arrayContaining'],
+                  [j.arrayExpression(args[0].elements)]
+                ),
+              ],
+              updateExpect(value, (node) => node),
+              false
+            )
           case 'eql':
             if (numberOfArgs === 1 && args[0].type === 'Literal') {
               return createCall('toBe', args, rest, containsNot)
             } else {
               return createCall('toEqual', args, rest, containsNot)
             }
-
+          case 'equalto':
+            return createCall('toEqual', args, rest, containsNot)
           case 'above':
           case 'greaterthan':
           case 'gt':
@@ -633,6 +666,7 @@ export default function transformer(fileInfo, api, options) {
             return createCall('toBeInstanceOf', args, rest, containsNot)
           case 'length':
           case 'lengthof':
+          case 'ofsize':
             return createCall('toHaveLength', args, restRaw, containsNot)
           case 'property':
             return createCall('toHaveProperty', args, rest, containsNot)
@@ -676,6 +710,13 @@ export default function transformer(fileInfo, api, options) {
                     )
                   )
                 )
+
+          case 'array':
+          case 'uint8array':
+          case 'uint16array':
+          case 'uint32array':
+          case 'uint8clampedarray':
+            return typeOf(p, value, [{ value: propertyName }], containsNot)
           default:
             return value
         }


Test patch:
diff --git a/src/transformers/chai-should.test.ts b/src/transformers/chai-should.test.ts
index cd6da6b..992cccc 100644
--- a/src/transformers/chai-should.test.ts
+++ b/src/transformers/chai-should.test.ts
@@ -908,5 +908,36 @@ it('converts before, after and context to beforeAll, afterAll and describe', ()
   )
 })
 
+test('supports chai-arrays plugin', () => {
+  expectTransformation(
+    `
+        expect([1, 2, 3]).to.be.array();
+        expect(new Uint16Array([1])).to.be.Uint16Array();
+
+        expect([1, 2, 3]).to.be.ofSize(3);
+        expect([1, 2, 3]).not.to.be.ofSize(5);
+
+        expect([1, 2, 3]).to.be.containing(1);
+
+        expect([1, 2, 3]).to.be.containingAllOf([1, 3]);
+
+        expect([1, 2, 3]).to.be.equalTo([1, 2, 3]);
+    `,
+    `
+        expect(Array.isArray([1, 2, 3])).toBe(true);
+        expect(new Uint16Array([1])).toBeInstanceOf(Uint16Array);
+
+        expect([1, 2, 3]).toHaveLength(3);
+        expect([1, 2, 3]).not.toHaveLength(5);
+
+        expect([1, 2, 3]).toContain(1);
+
+        expect([1, 2, 3]).toEqual(expect.arrayContaining([1, 3]));
+
+        expect([1, 2, 3]).toEqual([1, 2, 3]);
+    `
+  )
+})
+
 // TODO: warn about chaining not working
 // E.g. expect({ foo: 'baz' }).to.have.property('foo').and.not.equal('bar');