Repository: unexpectedjs/unexpected
Pull request #632 (2019-05-09 21:43:41)

Inspect Buffers with Buffer.from([0x00, ...]) instead of Buffer([0x00, ...])
Avoids noisy deprecation warning from node 10+ when the output is copy/pasted into code:

> DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.


Patch:
diff --git a/documentation/assertions/Buffer/when-decoded-as.md b/documentation/assertions/Buffer/when-decoded-as.md
index 407d782e..6d8b18e4 100644
--- a/documentation/assertions/Buffer/when-decoded-as.md
+++ b/documentation/assertions/Buffer/when-decoded-as.md
@@ -28,7 +28,8 @@ expect(
 ```
 
 ```output
-expected Buffer([0xE2, 0x98, 0xBA]) when decoded as 'utf-8' to equal 'happy face'
+expected Buffer.from([0xE2, 0x98, 0xBA])
+when decoded as 'utf-8' to equal 'happy face'
 
 -☺
 +happy face
diff --git a/documentation/assertions/any/to-equal.md b/documentation/assertions/any/to-equal.md
index ffdd798b..a8fbd6f9 100644
--- a/documentation/assertions/any/to-equal.md
+++ b/documentation/assertions/any/to-equal.md
@@ -84,8 +84,8 @@ expect(
 ```
 
 ```output
-expected Buffer([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])
-to equal Buffer([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])
+expected Buffer.from([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])
+to equal Buffer.from([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])
 
  00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74  │...Here is the t│
 -68 69 6E 67 20 49 20 77 61 73 20 74 61 6C 6B 69  │hing I was talki│
diff --git a/lib/types.js b/lib/types.js
index e2fa9736..6979ab75 100644
--- a/lib/types.js
+++ b/lib/types.js
@@ -1273,7 +1273,10 @@ module.exports = function(expect) {
     expect.addType({
       name: 'Buffer',
       base: 'binaryArray',
-      identify: Buffer.isBuffer
+      identify: Buffer.isBuffer,
+      prefix(output) {
+        return output.code(`Buffer.from([`, 'javascript');
+      }
     });
   }
 


Test patch:
diff --git a/test/assertions/to-equal.spec.js b/test/assertions/to-equal.spec.js
index 2c8eb07c..7bd556ae 100644
--- a/test/assertions/to-equal.spec.js
+++ b/test/assertions/to-equal.spec.js
@@ -359,8 +359,8 @@ describe('to equal assertion', () => {
           );
         },
         'to throw',
-        'expected Buffer([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])\n' +
-          'to equal Buffer([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])\n' +
+        'expected Buffer.from([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])\n' +
+          'to equal Buffer.from([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])\n' +
           '\n' +
           ' 00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74  │...Here is the t│\n' +
           '-68 69 6E 67 20 49 20 77 61 73 20 74 61 6C 6B 69  │hing I was talki│\n' +
diff --git a/test/assertions/to-satisfy.spec.js b/test/assertions/to-satisfy.spec.js
index f345d22a..ff0cf3e7 100644
--- a/test/assertions/to-satisfy.spec.js
+++ b/test/assertions/to-satisfy.spec.js
@@ -1299,7 +1299,7 @@ describe('to satisfy assertion', () => {
             );
           },
           'to throw',
-          'expected Buffer([0x01, 0x02, 0x03]) to equal Buffer([0x01, 0x02, 0x04])\n' +
+          'expected Buffer.from([0x01, 0x02, 0x03]) to equal Buffer.from([0x01, 0x02, 0x04])\n' +
             '\n' +
             '-01 02 03                                         │...│\n' +
             '+01 02 04                                         │...│'
@@ -1325,10 +1325,10 @@ describe('to satisfy assertion', () => {
               );
             },
             'to throw',
-            'expected Buffer([0x62, 0x61, 0x72])\n' +
-              "to satisfy expect.it('to equal', Buffer([0x66, 0x6F, 0x6F]))\n" +
+            'expected Buffer.from([0x62, 0x61, 0x72])\n' +
+              "to satisfy expect.it('to equal', Buffer.from([0x66, 0x6F, 0x6F]))\n" +
               '\n' +
-              'expected Buffer([0x62, 0x61, 0x72]) to equal Buffer([0x66, 0x6F, 0x6F])\n' +
+              'expected Buffer.from([0x62, 0x61, 0x72]) to equal Buffer.from([0x66, 0x6F, 0x6F])\n' +
               '\n' +
               '-62 61 72                                         │bar│\n' +
               '+66 6F 6F                                         │foo│'
@@ -1371,11 +1371,11 @@ describe('to satisfy assertion', () => {
               )
             ),
             'to be rejected with',
-            'expected Buffer([0x00, 0x01, 0x02])\n' +
-              "to satisfy expect.it('when delayed a little bit', 'to equal', Buffer([0x02, 0x01, 0x00]))\n" +
+            'expected Buffer.from([0x00, 0x01, 0x02])\n' +
+              "to satisfy expect.it('when delayed a little bit', 'to equal', Buffer.from([0x02, 0x01, 0x00]))\n" +
               '\n' +
-              'expected Buffer([0x00, 0x01, 0x02])\n' +
-              'when delayed a little bit to equal Buffer([0x02, 0x01, 0x00])\n' +
+              'expected Buffer.from([0x00, 0x01, 0x02])\n' +
+              'when delayed a little bit to equal Buffer.from([0x02, 0x01, 0x00])\n' +
               '\n' +
               '-00 01 02                                         │...│\n' +
               '+02 01 00                                         │...│'