Skip to content

protobuf.js 6.10.0 is incompatible with jest #1443

@starkwang

Description

@starkwang

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions