Created
October 17, 2022 15:58
-
-
Save DWboutin/c0be29d370921fc878ebfc80a3f090eb to your computer and use it in GitHub Desktop.
Typescript Jest mock FS correctly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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