Unit Test with Jest gives TypeError: File.test.js: Cannot read property 'toString' of undefined

1.1k views Asked by At

I have been working in React Native using Expo CLI and recently started to face issue with my Unit tests that got failed because of one common reason. Stack trace is below

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)

My node version is node:12.18.4.I wonder what caused these errors since everything was working perfectly. On my local system they are working fine, occasionally but CI process tends to fail them randomly which hinders the overall code coverage figures.

Unit test I am trying to run is very simple as written below

it('Renders Strings as expected', () => {
  expect(received).toStrictEqual(expected)
})
1

There are 1 answers

0
Mehroze Yaqoob On BEST ANSWER

For those of you who are still wandering around to find answer to above question. Issue was in the library itself convert-source-map which needed to handle this exception.

I forked the actual repository and handled that exception in line 64 toBase64 method. Now the method looks like something

Converter.prototype.toBase64 = function () {
  var json = this.toJSON();
  return (SafeBuffer.Buffer.from(json, 'utf8') || "").toString('base64');
};

Now everything is working fine.

Original method was something like this

Converter.prototype.toBase64 = function () {
  var json = this.toJSON();
  return SafeBuffer.Buffer.from(json, 'utf8').toString('base64');
};