Skip to content

Instantly share code, notes, and snippets.

@Farenheith
Created January 12, 2020 19:22
Show Gist options
  • Save Farenheith/5a6472c62958aa7cadf2bd6b6221ab07 to your computer and use it in GitHub Desktop.
Save Farenheith/5a6472c62958aa7cadf2bd6b6221ab07 to your computer and use it in GitHub Desktop.
An example of unit test for a simple main file
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