Description
When transit-js
gets imported into jest
I get the following error:
TypeError: Cannot read property 'Buffer' of undefined
1 | import React from "react"
> 2 | import { Builder, Query, Utils } from "react-awesome-query-builder"
| ^
3 |
4 | import configFn from "./config"
5 | import "./styles.less"
at Object.defaultHandlers (node_modules/transit-js/transit.js:3222:37)
at new Object.<anonymous>.com.cognitect.transit.handlers.Handlers (node_modules/transit-js/transit.js:3228:34)
at new Object.<anonymous>.com.cognitect.transit.impl.writer.JSONMarshaller (node_modules/transit-js/transit.js:3482:19)
at Object.writer (node_modules/transit-js/transit.js:3770:13)
at createWriter (node_modules/transit-immutable-js/index.js:63:18)
at createInstanceFromHandlers (node_modules/transit-immutable-js/index.js:240:16)
at Object.<anonymous> (node_modules/transit-immutable-js/index.js:303:18)
at Object.<anonymous> (node_modules/react-awesome-query-builder/lib/import/tree.js:22:15)
at Object.<anonymous> (node_modules/react-awesome-query-builder/lib/import/index.js:7:13)
at Object.<anonymous> (node_modules/react-awesome-query-builder/lib/index.js:36:38)
at Object.<anonymous> (packages/shared-query-builder/src/DemoQueryBuilder.tsx:2:1)
at Object.<anonymous> (packages/shared-query-builder/src/index.js:1:1)
at Object.<anonymous> (src/domains/scorecards/LayerForm/LayerForm.jsx:2:1)
at Object.<anonymous> (src/domains/scorecards/LayerForm/__tests__/LayerForm.spec.jsx:4:1)
The specific function failing is:
com.cognitect.transit.handlers.defaultHandlers = function(a) {
a.set(null, new com.cognitect.transit.handlers.NilHandler);
a.set(String, new com.cognitect.transit.handlers.StringHandler);
a.set(Number, new com.cognitect.transit.handlers.NumberHandler);
a.set(goog.math.Long, new com.cognitect.transit.handlers.IntegerHandler);
a.set(Boolean, new com.cognitect.transit.handlers.BooleanHandler);
a.set(Array, new com.cognitect.transit.handlers.ArrayHandler);
a.set(Object, new com.cognitect.transit.handlers.MapHandler);
a.set(Date, new com.cognitect.transit.handlers.DateHandler);
a.set(com.cognitect.transit.types.UUID, new com.cognitect.transit.handlers.UUIDHandler);
a.set(com.cognitect.transit.types.Keyword, new com.cognitect.transit.handlers.KeywordHandler);
a.set(com.cognitect.transit.types.Symbol, new com.cognitect.transit.handlers.SymbolHandler);
a.set(com.cognitect.transit.types.TaggedValue, new com.cognitect.transit.handlers.TaggedHandler);
a.set(com.cognitect.transit.types.TransitSet, new com.cognitect.transit.handlers.TransitSetHandler);
a.set(com.cognitect.transit.types.TransitArrayMap, new com.cognitect.transit.handlers.TransitArrayMapHandler);
a.set(com.cognitect.transit.types.TransitMap, new com.cognitect.transit.handlers.TransitMapHandler);
"undefined" != typeof goog.global.Buffer && a.set(goog.global.Buffer, new com.cognitect.transit.handlers.BufferHandler);
"undefined" != typeof Uint8Array && a.set(Uint8Array, new com.cognitect.transit.handlers.Uint8ArrayHandler);
return a;
};
specifically the clause:
"undefined" != typeof goog.global.Buffer
Seems that goog.global
isn't an object, and there was no working way for me to modify window/globals to get this to have some inject value (no window.Buffer
or window.goog = window.goog || {}
or globals.Buffer
etc).
I tried a whole bunch of jest config settings and require vs import and all had similar issues.
The only thing I did note was that the "react-awesome-query-builder" from which this is a dependency includes node: {Buffer: false}
in their webpack config. I tried setting this as well but to no avail.
The only other thing of note was that all other modules were imported but "transit-immutable-js" was required?
// react-awesome-query-builder/modules/import/tree.js
const transit = require("transit-immutable-js");
Thoughts?
Reference issue: ukrbublik/react-awesome-query-builder#254