0

We are using gRPC-Web client in our project, but when we try to run unit tests on code that imports generated gRPC classes we get an error

TypeError: cannot read property 'navigator' of undefined.

I guess the problem is somehow related to the way generated modules are exposed, however I cannot figure out how to make it work with Jest.

I tried to mock global.navigator object manually using setupTests.js file, but it didn't help. It looks like the gRPC code is being executed before Jest.

Our web project is created using Create React App library with TypeScript without ejecting and we use default Jest configuration to run unit tests.

Please advise.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • Provide the code you want to test – Lin Du Dec 03 '19 at 04:42
  • I am not sure where this "navigator" property comes from. Not from our grpc-web library. Can you provide more info? – Stanley Cheung Dec 04 '19 at 21:00
  • After a couple of days struggling with this, I found what the problem was. By default Jest has `--transformIgnorePatterns /node_modules/` option which was overridden in our project. As a result our grpc api package wasn't correctly wrapped with Jest. After restoring this option to include all the contents of node_modules folder the problem disappeared. – Alexey Protasov Dec 06 '19 at 08:36

0 Answers0