node-gd icon indicating copy to clipboard operation
node-gd copied to clipboard

TypeError: Cannot redefine property: saveJpeg

Open scheepers opened this issue 5 years ago • 3 comments
trafficstars

When running test suites that include async test cases for modules using:

gd = require ('node-gd')

The following error occurs:

    TypeError: Cannot redefine property: saveJpeg
        at Function.defineProperty (<anonymous>)

       7 | 
       8 | 
    >  9 | const gd =  require('node-gd')
         |             ^
      10 | 
      11 | 
      12 | class Tile {

      at node_modules/node-gd/lib/node-gd.js:89:12
          at Array.forEach (<anonymous>)
      at Object.<anonymous> (node_modules/node-gd/lib/node-gd.js:82:9)
      at Object.<anonymous> (node_modules/node-gd/index.js:10:18) 

Which can be fixed by this patch:

--- lib/node-gd.js	2020-09-11 21:24:59.945903226 +0200
+++ lib/node-gd-new.js	2020-09-11 21:44:00.075234552 +0200
@@ -85,10 +85,11 @@
     return;
   }
 
-  if (!`save${format}`) Object.defineProperty(
-    bindings.Image.prototype, `save${format}`, {
-    value: saveFormatFn(format)
-  });
+  if (!bindings.Image.prototype[`save${format}`]){
+    Object.defineProperty(bindings.Image.prototype, `save${format}`, {
+      value: saveFormatFn(format)
+    });
+  }
 });
 
 /**

scheepers avatar Sep 11 '20 19:09 scheepers

HI @scheepers thanks for reaching out. Could you elaborate on test suites that use async test cases? What does your code setup look like in those cases? I'm wondering if your proposed solution is enough, in the sense that either we would need to make a "deeper" fix, or you might need to change the way you load node-gd. Could you supply a test case in order to investigate it more thorough? Thanks in advance. Regards, Vincent

y-a-v-a avatar Sep 13 '20 12:09 y-a-v-a

Hi Vince, thanks for responding. My test cases are written in Jest for the following use case: an image wrapper to encapsulate image loading and a few calculation functions. Many of those are contained in a dictionary loaded from a directory of images. I've now found if I run the test suites for these two objects seperately they both pass all tests, but running the whole suite produces the error result.

scheepers avatar Sep 13 '20 20:09 scheepers

Hi @y-a-v-a , I noticed the same behavior. My case is close to the one above.

maximshe avatar Dec 17 '21 09:12 maximshe