Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protobuf.js 6.10.0 is incompatible with jest #1443

Closed
starkwang opened this issue Jul 14, 2020 · 6 comments · Fixed by #1445
Closed

protobuf.js 6.10.0 is incompatible with jest #1443

starkwang opened this issue Jul 14, 2020 · 6 comments · Fixed by #1445

Comments

@starkwang
Copy link

starkwang commented Jul 14, 2020

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:

protobuf.js/src/root.js

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);
};

@qazwsx9006
Copy link

+1

1 similar comment
@seki2020
Copy link

+1

@alincode
Copy link

alincode commented Jul 15, 2020

+1

google pubsub module also depends on protobuf.js, it will effect a lot project, I think. 😅

https://github.com/googleapis/nodejs-pubsub/blob/master/package.json#L63

@amammay
Copy link

amammay commented Jul 15, 2020

+1

2 similar comments
@JonMontgo
Copy link

+1

@jhowr
Copy link

jhowr commented Jul 15, 2020

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants