-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Description
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:
protobuf.js/src/util/minimal.js
Lines 29 to 32 in 07e8185
| 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:
protobuf.js/src/util/minimal.js
Line 55 in 07e8185
| util.isNode = Boolean(util.global.process && util.global.process.versions && util.global.process.versions.node); |
And then the loadSync() will throw an error:
Lines 244 to 248 in 07e8185
| Root.prototype.loadSync = function loadSync(filename, options) { | |
| if (!util.isNode) | |
| throw Error("not supported"); | |
| return this.load(filename, options, SYNC); | |
| }; |
initial-wu, smilebuz, qazwsx9006, robrichard, mad-it and 7 morerogerkjoy and amammay
Metadata
Metadata
Assignees
Labels
No labels