I just want to mock a function which is imported in the function, which I want to test:
content.ts
import { getFileContent } from './files' // Mock this one
export const example = async (argv) => { // This function should be tested
let result = {}
const { nodes } = await getFileContent(argv) // Mock this one
console.log(nodes)
result = nodes.type
return result
}
file.ts
export const getFileContent = async (argv) => {
try {
return reader(argv.path) // reader is a function inside this file which reads files using `fs`
} catch (error) {
console.error(error)
}
}
content.spec.ts
import { example } from './content'
const mockGetContent = jest.fn(() =>
Promise.resolve({ nodes: { type: 'app' } })
)
jest.mock('./files', () => ({
getFileContent: () => mockGetContent
}))
test('should call mock function', async () => {
const argv = { path: './file.json'}
await example(argv)
expect(mockGetContent).toHaveBeenCalled()
})
But the expect fails, as mockGetContent
has not been called. Also the console.log
prints undefined
.
What am I missing?