Last active
October 23, 2023 20:24
-
-
Save x47188/daedfb2f4f8c7f81acd0ae8a9318aa0b to your computer and use it in GitHub Desktop.
Jest + Typescript + mock + fs
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
// __mocks__/fs.ts | |
const promises = { | |
readFile: jest.fn() | |
}; | |
export { // or whatever you've to mock. | |
promises | |
} |
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
// class.spec.ts | |
import { mocked } from 'ts-jest/utils'; | |
import { promises } from 'fs'; | |
jest.mock('fs'); | |
describe('Class', () => { | |
describe('method', () => { | |
beforeEach(() => { | |
mocked(promises.readFile as jest.Mock).mockImplementation(() => { | |
throw new Error(); | |
}); | |
}); | |
beforeEach(() => { | |
mocked(promises.readFile as jest.Mock).mockReturnValueOnce(content); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment