Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Typescript Jest mock FS correctly
jest.mock('fs')
import fs from 'fs'
const mockedFs = fs as jest.Mocked<typeof fs>
describe('fetch', () => {
describe('file exists', () => {
beforeEach(() => {
mockedFs.existsSync.mockImplementation(() => true)
// function to test
myClass.fetch(DUMMY_PATH)
})
afterEach(() => {
jest.clearAllMocks()
jest.resetAllMocks()
})
it('should check if file exists', () => {
expect(mockedFs.existsSync).toHaveBeenCalledTimes(1)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment