Repository: chaijs/chai
Pull request #814 (2016-10-02 17:18:24)

Make `empty` assertion work with es6 collections
Follow-up of #763 and #812.

vieiralucas: Hello @shvaikalesh, I'm very happy to see you here again.

This change seems very clean, awesome work here.
I just made a comment on the message you added on weak collections. Let me know what you think, feel free to disagree with me. 
shvaikalesh: Not that fast, forgot the tests :(
lucasfcosta: Well done, LGTM too!
@vieiralucas would you like to do the honors and squash + merge this?
meeber: Sec let me check something
meeber: @shvaikalesh Annoyingly, `Map` and `Set` constructor arguments aren't supported in IE11 so will need to set the values manually in the tests. Also recommend adding a little note like [this](https://github.com/chaijs/chai/blob/5b170b2e3bae6e918ef723c168668d829d7bc61b/test/expect.js#L1409-L1415). Otherwise looks great!
shvaikalesh: @meeber Nice catch, I kinda forgot about partial implementations.
meeber: LGTM! Thank you for your contributions! :D

Patch:
diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js
index 829c174..355d948 100644
--- a/lib/chai/core/assertions.js
+++ b/lib/chai/core/assertions.js
@@ -536,6 +536,13 @@ module.exports = function (chai, _) {
       case 'string':
         itemsCount = val.length;
         break;
+      case 'map':
+      case 'set':
+        itemsCount = val.size;
+        break;
+      case 'weakmap':
+      case 'weakset':
+        throw new TypeError('.empty was passed a weak collection');
       case 'function':
         var name = val.name ? ' ' + val.name : '';
         throw new TypeError('.empty was passed a function' + name);


Test patch:
diff --git a/test/expect.js b/test/expect.js
index c706021..7b1a75f 100644
--- a/test/expect.js
+++ b/test/expect.js
@@ -646,6 +646,60 @@ describe('expect', function () {
     expect({}).to.be.empty;
     expect({foo: 'bar'}).not.to.be.empty;
 
+    if (typeof WeakMap === 'function') {
+      err(function(){
+        expect(new WeakMap).not.to.be.empty;
+      }, ".empty was passed a weak collection");
+    }
+
+    if (typeof WeakSet === 'function') {
+      err(function(){
+        expect(new WeakSet).not.to.be.empty;
+      }, ".empty was passed a weak collection");
+    }
+
+    if (typeof Map === 'function') {
+      expect(new Map).to.be.empty;
+
+      // Not using Map constructor args because not supported in IE 11.
+      var map = new Map;
+      map.set('a', 1);
+      expect(map).not.to.be.empty;
+
+      err(function(){
+        expect(new Map).not.to.be.empty;
+      }, "expected {} not to be empty");
+
+      map = new Map;
+      map.key = 'val';
+      expect(map).to.be.empty;
+
+      err(function(){
+        expect(map).not.to.be.empty;
+      }, "expected { key: 'val' } not to be empty");
+    }
+
+    if (typeof Set === 'function') {
+      expect(new Set).to.be.empty;
+
+      // Not using Set constructor args because not supported in IE 11.
+      var set = new Set;
+      set.add(1);
+      expect(set).not.to.be.empty;
+
+      err(function(){
+        expect(new Set).not.to.be.empty;
+      }, "expected {} not to be empty");
+
+      set = new Set;
+      set.key = 'val';
+      expect(set).to.be.empty;
+
+      err(function(){
+        expect(set).not.to.be.empty;
+      }, "expected { key: 'val' } not to be empty");
+    }
+
     err(function(){
       expect('').not.to.be.empty;
     }, "expected \'\' not to be empty");
diff --git a/test/should.js b/test/should.js
index b827784..e07e722 100644
--- a/test/should.js
+++ b/test/should.js
@@ -626,6 +626,60 @@ describe('should', function() {
     ({}).should.be.empty;
     ({foo: 'bar'}).should.not.be.empty;
 
+    if (typeof WeakMap === 'function') {
+      err(function(){
+        (new WeakMap).should.not.be.empty;
+      }, ".empty was passed a weak collection");
+    }
+
+    if (typeof WeakSet === 'function') {
+      err(function(){
+        (new WeakSet).should.not.be.empty;
+      }, ".empty was passed a weak collection");
+    }
+
+    if (typeof Map === 'function') {
+      (new Map).should.be.empty;
+
+      // Not using Map constructor args because not supported in IE 11.
+      var map = new Map;
+      map.set('a', 1);
+      map.should.not.be.empty;
+
+      err(function(){
+        (new Map).should.not.be.empty;
+      }, "expected {} not to be empty");
+
+      map = new Map;
+      map.key = 'val';
+      map.should.be.empty;
+
+      err(function(){
+        map.should.not.be.empty;
+      }, "expected { key: 'val' } not to be empty");
+    }
+
+    if (typeof Set === 'function') {
+      (new Set).should.be.empty;
+
+      // Not using Set constructor args because not supported in IE 11.
+      var set = new Set;
+      set.add(1);
+      set.should.not.be.empty;
+
+      err(function(){
+        (new Set).should.not.be.empty;
+      }, "expected {} not to be empty");
+
+      set = new Set;
+      set.key = 'val';
+      set.should.be.empty;
+
+      err(function(){
+        set.should.not.be.empty;
+      }, "expected { key: 'val' } not to be empty");
+    }
+
     err(function(){
       ''.should.not.be.empty;
     }, "expected \'\' not to be empty");