问题描述
我一直在使用Expo CLI在React Native中工作,最近开始遇到由于一个常见原因而导致我的单元测试失败的问题。堆栈跟踪在下面
Cannot read property 'toString' of undefined
at Converter.toBase64 (node_modules/convert-source-map/index.js:61:46)
at Converter.toComment (node_modules/convert-source-map/index.js:65:21)
at generateCode (node_modules/@babel/core/lib/transformation/file/generate.js:78:76)
at run (node_modules/@babel/core/lib/transformation/index.js:55:33)
at run.next (<anonymous>)
at transform (node_modules/@babel/core/lib/transform.js:27:41)
at transform.next (<anonymous>)
at evaluateSync (node_modules/gensync/index.js:244:28)
at sync (node_modules/gensync/index.js:84:14)
我的节点版本是node:12.18.4。我想知道是什么原因导致这些错误的,因为一切都运行良好。在我的本地系统上,它们有时工作正常,但CI流程往往会使其随机失效,从而阻碍了总体代码覆盖范围。
我要运行的单元测试非常简单,如下所示
it('Renders Strings as expected',() => {
expect(received).toStrictEqual(expected)
})
解决方法
对于那些仍在四处寻找上述问题答案的人。 问题出在库本身的convert-source-map中,该库需要处理此异常。
我分叉了实际的存储库,并在第64行toBase64方法中处理了该异常。现在该方法看起来像
Converter.prototype.toBase64 = function () {
var json = this.toJSON();
return (SafeBuffer.Buffer.from(json,'utf8') || "").toString('base64');
};
现在一切正常。
原始方法是这样的
Converter.prototype.toBase64 = function () {
var json = this.toJSON();
return SafeBuffer.Buffer.from(json,'utf8').toString('base64');
};