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 / 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 / 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 / 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 / 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 / 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 / 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 / untestable-code.ts
Created February 20, 2020 02:12
A example of untestable code
import untestableOldFunction = require('untestable-old-function');
export function doSomethingUntestable() {
return untestableOldFunction(123);
}
@Farenheith
Farenheith / untestable-wrapper.ts
Created February 20, 2020 02:17
Example of wrapper making untestable function testable
import untestableOldFunction = require('untestable-old-function');
export const nowTestableOldFunction = untestableOldFunction as (p: number) => string;
@Farenheith
Farenheith / untestable-wrapper.spec.ts
Created February 20, 2020 02:20
Test for unstestable-wrapper
import { expect } from 'chai';
import * as wrapper from '../src/untestable-wrapper';
import untestableOldFunction = require('untestable-old-function');
describe('untestable-wrapper', () => {
it('should export untestableOldFunction as nowTestableOldFunction', () => {
expect(wrapper.nowTestableOldFunction).to.be.eq(untestableOldFunction);
});
});
@Farenheith
Farenheith / untestable-code.ts
Created February 20, 2020 02:26
Example of untestable code made testable
import { nowTestableOldFunction } from './untestable-wrapper';
export function doSomethingUntestable() {
return nowTestableOldFunction(123);
}