protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
351 stars 67 forks source link

fail to load google-protobuf in node environnement (unit test with jest) #160

Closed xNok closed 1 year ago

xNok commented 1 year ago

I am trying to have a Go application and Node backend communicated via gRPC. But I am facing some issues with the google-protobuf lib when trying to unit-test the code. The issue seems that when running jest is not able to import google-protobuf for some reason (the app is working fine just no unit test can pass anymore).

node: v16.19.0 google-protobuf: v3.21.2

The error:

   ReferenceError: self is not defined
      at Object.self (../../../node_modules/google-protobuf/google-protobuf.js:12:314)

The generated protos looks like that:

var jspb = require('google-protobuf');
var goog = jspb;
var global = Function('return this')();

var google_api_annotations_pb = require('../../../../google/api/annotations_pb.js');
goog.object.extend(proto, google_api_annotations_pb);
var google_protobuf_field_mask_pb = require('google-protobuf/google/protobuf/field_mask_pb.js');
goog.object.extend(proto, google_protobuf_field_mask_pb);
var google_protobuf_timestamp_pb = require('google-protobuf/google/protobuf/timestamp_pb.js');

It calls var jspb = require('google-protobuf'); as expected by the import, seems to get stuck at this point.

goog.global=this||self

where this and self are not defined

dibenede commented 1 year ago

Thanks for reporting this. We are aware of this issue; closing as a dupe of #149 .