protobuf.js version: 6.10.0
How to reproduce
See: https://github.com/starkwang/jest-protobufjs-incompatible
const protobuf = require('protobufjs')
test('adds 1 + 2 to equal 3', (done) => {
protobuf.loadSync("awesome.proto");
expect(1 + 2).toBe(3);
});
Reason
Jest has a mock global for each test. It is not a [object global] but a [object Object]
So the mock global will break the util.global in protobuf.js:
|
util.global = typeof global !== "undefined" && Object.prototype.toString.call(global) === "[object global]" && global |
|
|| typeof window !== "undefined" && window |
|
|| typeof self !== "undefined" && self |
|
|| this; // eslint-disable-line no-invalid-this |
And util.isNode will return false:
|
util.isNode = Boolean(util.global.process && util.global.process.versions && util.global.process.versions.node); |
And then the loadSync() will throw an error:
|
Root.prototype.loadSync = function loadSync(filename, options) { |
|
if (!util.isNode) |
|
throw Error("not supported"); |
|
return this.load(filename, options, SYNC); |
|
}; |
protobuf.js version: 6.10.0
How to reproduce
See: https://github.com/starkwang/jest-protobufjs-incompatible
Reason
Jest has a mock
globalfor each test. It is not a[object global]but a[object Object]So the mock
globalwill break theutil.globalin protobuf.js:protobuf.js/src/util/minimal.js
Lines 29 to 32 in 07e8185
And
util.isNodewill returnfalse:protobuf.js/src/util/minimal.js
Line 55 in 07e8185
And then the
loadSync()will throw an error:protobuf.js/src/root.js
Lines 244 to 248 in 07e8185