y-a-v-a / node-gd

🎨 GD graphics library (libgd) C++ bindings for Node.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Cannot redefine property: saveJpeg

scheepers opened this issue · comments

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)
+    });
+  }
 });
 
 /**

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

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.

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