Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to mock a function/method in Vue 3 with Vue Test Utils 2?

I am in the process of converting my project from Vue 2 to 3 and also testing library to Vue test utils 2. Before, mocking methods works like magic:

export default {
  methods: {
    greet(text) {
      sayHello('hello world')
    },
    sayHello(text) {
      console.log(text)
    }
  }
}
test('should sample', async () => {
  const spy = jest.spyOn(wrapper.vm, 'sayHello')
  wrapper.vm.greet('hello world')
  expect(spy).toHaveBeenCalledWith('hello world')
})

I tried converting it to composition api:

export default defineComponent({
  setup() {
    function greet(text) {
      sayHello(text)
    }

    function sayHello(text) {
      console.log(text)
    }

    return {
      greet,
      sayHello
    }
  }
})

and now testing is not working at all

expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: "hello world"

Number of calls: 0

How do I mock functions that call another functions in Vue 3 + Vue test utils 2?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you should create your spy functions before the wrapper instance is created (where you use mount or shallowMount). Creating spies before creating instance and using

beforeEach(() => {
  jest.resetAllMocks();
})

in order to reset mocks for following cases should do the trick.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!