• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

235
Views
How to mock functions of a function?

I'm using simple-git in my script like this:

import simpleGit from 'simple-git'
const git = simpleGit()

const foo = async (): Promise<void> => {
  const tags: string = await git.tag({
    '--list': null,
    '--format': '%(objectname:short)'
  })
}

In my tests I need to mock out the git calls, which I'm doing this way:

jest.mock('simple-git', () => ({
  tag: () => jest.fn()
}))

But this is failing. I guess, I have to take care of const git = simpleGit()

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

0

If simple-git is installed via node_module (which I assume it is) you only need to create a directory called __mocks__ at the root of your project anything placed there is automatically mocked in your case you need to create a file called simple-git.js

.
├── __mocks__
│   └── simple-git.js
├── node_modules

Reference: https://jestjs.io/docs/manual-mocks#mocking-node-modules

about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error