GithubHelp home page GithubHelp logo

Comments (6)

rcaceiro avatar rcaceiro commented on June 2, 2024

How can I mock twillo module for testing?

The same here

from nestjs-twilio.

lkaric avatar lkaric commented on June 2, 2024

@rcaceiro @qamalyanaren We had the same question about the testing here #10, if you need a more concrete example, please provide me with context so that I can help you

from nestjs-twilio.

qamalyanaren avatar qamalyanaren commented on June 2, 2024

@rejvban I want to mock this function in the service
https://user-images.githubusercontent.com/18355408/128837763-cdcc6a55-6aef-4ebf-91f4-f1a4ac0c920e.png

from nestjs-twilio.

lkaric avatar lkaric commented on June 2, 2024

@qamalyanaren Since this has become a rather reacurring question, I'm going to try to explain how it's tested in the library itself. But I think that this is more of a general testing question. File for reference!

Since I don't see the whole context, I'm going to go through the example with the assumption that you only want to test the twilio module https://github.com/rejvban/nestjs-twilio/blob/e846f88fd330b0531e2ed0414b77717ed4af6fbc/lib/__tests__/twilio.decorators.test.ts#L27-L37

First I created the mock injectable service containing the injected twilio client and then provided it to the testing module which is properly initializing the TwilioModule in order for the service to work. You should also be able to use your written service, just be cautious when initializing the module.

Then, from the test module, you can access the appropriate service, in this case, its client (TwilioClient) and a whole bunch of methods that that client provides.

https://github.com/rejvban/nestjs-twilio/blob/e846f88fd330b0531e2ed0414b77717ed4af6fbc/lib/__tests__/twilio.decorators.test.ts#L47-L57

I can vouch that this approach works, considering that we have automated tests that send an SMS to my personal phone each time the test passes each time that there's a change on the master branch.

Also sorry for the late reply, been really busy at work, hope I could help!

from nestjs-twilio.

stale avatar stale commented on June 2, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from nestjs-twilio.

rained23 avatar rained23 commented on June 2, 2024

hi, can I know how can I mock the injected service ?, I don't want it to call the real client, I want to mock the injected dependency, with the above approach I cant override the function from the client. When I execute my test, I am getting this error

The requested resource /2010-04-01/Accounts/ACSid/Messages.json was not found

I tried to mock the client using the following

{ 
  provide: InjectableService, 
  useValue: { 
    client: { 
      messages: { create:  jest.fn().mockResolvedValue(true) } 
    } 
}

But still receiving the above error.

from nestjs-twilio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.