Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Created October 17, 2022 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DWboutin/c0be29d370921fc878ebfc80a3f090eb to your computer and use it in GitHub Desktop.
Save DWboutin/c0be29d370921fc878ebfc80a3f090eb to your computer and use it in GitHub Desktop.
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