Skip to content

Instantly share code, notes, and snippets.

@Romakita
Created May 14, 2018 06:28
Show Gist options
  • Save Romakita/a95fe9d491f453d038b5bec0cbe72e8f to your computer and use it in GitHub Desktop.
Save Romakita/a95fe9d491f453d038b5bec0cbe72e8f to your computer and use it in GitHub Desktop.
Utils for unit test in Typescript with mocha + chai + sinon
import * as Chai from "chai";
import * as ChaiAsPromised from "chai-as-promised";
import * as SinonLib from "sinon";
import {SinonStub} from "sinon";
import * as SinonChai from "sinon-chai";
import {$log} from "ts-log-debug"; // your logger
Chai.should();
Chai.use(SinonChai);
Chai.use(ChaiAsPromised);
const expect = Chai.expect;
const assert = Chai.assert;
// tslint:disable-next-line: variable-name
const Sinon = SinonLib;
export interface LogStub {
$log: any;
stub: any;
restore: any;
reset: any;
info: SinonStub;
debug: SinonStub;
error: SinonStub;
warn: LogStub;
trace: LogStub;
}
const $logStub: LogStub = {
$log: $log as any,
info: Sinon.stub(),
debug: Sinon.stub(),
error: Sinon.stub(),
warn: Sinon.stub(),
trace: Sinon.stub(),
// tslint:disable-next-line
stub: function() {
this.info = Sinon.stub($log, "info");
this.debug = Sinon.stub($log, "debug");
this.error = Sinon.stub($log, "error");
this.warn = Sinon.stub($log, "warn");
this.trace = Sinon.stub($log, "trace");
},
// tslint:disable-next-line
restore: function() {
this.info.restore();
this.debug.restore();
this.error.restore();
this.warn.restore();
this.trace.restore();
},
// tslint:disable-next-line
reset: function() {
this.info.reset();
this.debug.reset();
this.error.reset();
this.warn.reset();
this.trace.reset();
}
} as any;
$logStub.stub();
const stub = (t: any): SinonStub => t;
const restore = (t: any) => t.restore();
export {expect, assert, Sinon, SinonChai, $logStub, stub, restore};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment