Created
January 12, 2020 19:22
-
-
Save Farenheith/5a6472c62958aa7cadf2bd6b6221ab07 to your computer and use it in GitHub Desktop.
An example of unit test for a simple main file
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 { stub, SinonStub } from 'sinon'; | |
import * as server from '../src/server'; | |
// First describe, class name | |
describe('index.ts', () => { | |
let start: SinonStub; | |
// Before each to bootstrap stubs, where all the methods that could be called are stubbeds | |
beforeEach(() => { | |
start = sinon.stub(server, 'start'); | |
// here we clean the require cache, to make sure index will not be loaded before our test | |
delete require.cache[require.resolve('../src/index')]; | |
}); | |
// A single test case | |
it('should do something when something happens', () => { | |
const result = require('../src/index'); | |
expect(start).to.have.been.calledOnceWithExatly(); | |
expect(result).to.be.undefined; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment