Created
March 29, 2018 20:12
-
-
Save bryanjknight/5e6304c61b73c8ef6eb7023cd0adac22 to your computer and use it in GitHub Desktop.
Stubbing fs using sinonjs in typescript
This file contains hidden or 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
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'); | |
}); | |
}); |
This file contains hidden or 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
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