Skip to content

Instantly share code, notes, and snippets.

@bryanjknight
Created March 29, 2018 20:12
Show Gist options
  • Save bryanjknight/5e6304c61b73c8ef6eb7023cd0adac22 to your computer and use it in GitHub Desktop.
Save bryanjknight/5e6304c61b73c8ef6eb7023cd0adac22 to your computer and use it in GitHub Desktop.
Stubbing fs using sinonjs in typescript
import testModule from '../src/test';
import { expect } from 'chai';
import sinon from 'sinon';
import fs from 'fs';
describe('testModule', () => {
let readFileSyncStub: sinon.SinonStub;
let sandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
readFileSyncStub = sandbox.stub(fs, 'readFileSync')
});
afterEach(() => {
sandbox.restore();
});
it('should return blah', () => {
readFileSyncStub.returns('blah blah');
const result = testModule('./some/file.txt');
expect(result).to.equal('blah blah');
});
});
import fs from "fs";
export default function(pathToFile: string) {
return fs.readFileSync(pathToFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment