#How to mock the twilioClient in my Authentication Service with jest

16 messages · Page 1 of 1 (latest)

wanton sleet
#

How to mock instance variables in classes, which is my twilioClient in this case

I have attached images of my service and spec file

#

when i run the test, i get the following error.. I want to mock this twilioClient and all its functions. Any help appreciated been stuck on this for a while!

#

How to mock the twilioClient in my Authentication Service with jest

civic narwhal
#

Because you use new Twilio() you'd have to mock the twilio package using jest.mock. I'd rather suggest making that a provider so you can mock the provided object rather than the constructor

wanton sleet
#

thanks @civic narwhal

#

to make it a provider i would have to setup a twilioModule right?

civic narwhal
#

Not necessarily. If you plan to use it in multiple locations I would though

wanton sleet
#

what would be the other way, if i dont make it a module?

civic narwhal
#

Just add it to the current module's providers

wanton sleet
#

Like new TwilioClient()

civic narwhal
#
{
  provide: 'TWILIO_CLIENT',
  inject: [TWILIO_CLIENT_OPTIONS_TOKEN],
  useFactory: (options) => new Twilio(options)m
}
#

Assuming that the options have a dedicated token

wanton sleet
#

what does dedicated token mean?

#

I will try this out @civic narwhal

civic narwhal
wanton sleet
#

ok makes sense.. this would be easier to test then!