Skip to content

Instantly share code, notes, and snippets.

View Farenheith's full-sized avatar

Thiago Oliveira Santos Farenheith

  • Grupo Boticário
  • São Paulo, Brazil
View GitHub Profile
@Farenheith
Farenheith / runnable.spec.ts
Created February 20, 2020 02:01
Example of test of a code without functions
import { stub } from 'sinon';
import { expect } from 'chai';
describe('runnable.ts', () => {
let target = '../src/runnable.ts';
beforeEach(() => {
stub(console, 'log');
});
@Farenheith
Farenheith / runnable.ts
Last active February 20, 2020 02:48
Example of code without functions
if (process.env.IS_GOODBYE === 'true') {
console.log('goodbye world!');
} else {
console.log('hello world');
}
@Farenheith
Farenheith / my-function.spec.ts
Created February 20, 2020 00:23
Example to mock an exported function
import * as functionToMock from '../src/other-function';
import { myFunction } from '../src/my-function';
import { stub } from 'sinon';
describe('myFunction()', () => {
beforeEach(() => {
stub(functionToMock, 'otherFunction').returns('expected result' as any);
});
it('should return otherFunction result', () => {
@Farenheith
Farenheith / my-function.ts
Created February 20, 2020 00:15
Example to mock an exported function
import { otherFunction } from './other-function';
export function myFunction() {
return otherFunction(123);
}
@Farenheith
Farenheith / get-instance.spec.ts
Last active February 20, 2020 00:21
Example of how to mock a class constructor
import * as classToMock from '../src/my-class';
import { getInstance } from '../src/get-instance';
import { stub } from 'sinon';
describe('getInstance()', () => {
const mockedInstance = {
info: 'MyClass instance',
};
beforeEach(() => {
@Farenheith
Farenheith / get-instance.ts
Created February 19, 2020 23:59
Example of code to use in class stubbing
import { MyClass } from './my-class';
export function getInstance() {
return new MyClass(123);
}
@Farenheith
Farenheith / index.spec.ts
Created January 12, 2020 19:22
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');
@Farenheith
Farenheith / testsuiteexample.spec.ts
Last active January 12, 2020 19:09
An example of test suite
import { stub, SinonStub } from 'sinon';
// First describe, class name
describe('MyClass', () => {
let service: MyService;
let target: MyClass;
// Class describe beforeEach, class bootstrap
beforeEach(() => {
service = {} as any;
@Farenheith
Farenheith / setup.spec.ts
Created January 12, 2020 14:50
Exmaple of setup for unit tests
import 'reflect-metadata';
import { use } from 'chai';
import { restore } from 'sinon';
import sinonChai = require('sinon-chai');
use(sinonChai);
afterEach(() => {
restore();
});
@Farenheith
Farenheith / gulpfile-clear.js
Last active January 2, 2020 23:24
gulpfile-clear.js example
var gulp = require('gulp');
var del = require('del');
gulp.task('default', function () {
return del([
'build/'
]);
});