Skip to content

Instantly share code, notes, and snippets.

@x47188
Last active October 23, 2023 20:24
Show Gist options
  • Save x47188/daedfb2f4f8c7f81acd0ae8a9318aa0b to your computer and use it in GitHub Desktop.
Save x47188/daedfb2f4f8c7f81acd0ae8a9318aa0b to your computer and use it in GitHub Desktop.
Jest + Typescript + mock + fs
// __mocks__/fs.ts
const promises = {
readFile: jest.fn()
};
export { // or whatever you've to mock.
promises
}
// 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