Best way to ignore certain imported functions in jest tests

I'm working with Jest to test vue components. I don't want to test the axios calls inside of some of my functions, as I want the calls to be tested in a different place.

The way my files are set up, I import a function that builds an axios call into my component, like this:

My Component:

import { RepositoryFactroy } from '../services/RepositoryFactory';
const ModelRepo = RepositoryFactory.get('model');

export default {
  methods: {
    async getModelItems() {
      ModelRepo.get(); //calls an axios get for all matching records, needs to be mocked
      OtherCodeThatNeedsToBeRun(); //this needs to run
    }
  }
}

The Repository Factory gets a matching repository for whatever you are looking for, in this case "model", and checks the corresponding repository for example ModelRepository.js for a list of functions that have whatever info is needed for an axios call, then add it onto the axios instance declared in Repository.js. Not sure if that's necessary info but just want to be thorough.

Say I wanted to test something on Component.vue in getModelItems() method without running my imported axios call. How do I make Jest ignore the ModelRepo.get() call? Do I somehow have to create a jest mock for this?

edit

To be clear, I need getModelItems() to run for other code inside it, and only to replace the functionality of ModelRepo.get() within that method



Read more here: https://stackoverflow.com/questions/66335314/best-way-to-ignore-certain-imported-functions-in-jest-tests

Content Attribution

This content was originally published by movac at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: