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 / untestable-class-wrapper.ts
Created February 20, 2020 02:30
Example of wrapper to a untestable class
import UntestableClass from 'untestable-class';
export interface NowTestableClassType {
someMethod(p: number): string;
}
export const NowTestableClass = UntestableClass as new (constructorParam: string) => NowTestableClassType;
@Farenheith
Farenheith / double-console.ts
Last active February 21, 2020 10:49
Example of testable code using setTimeout
export function doubleConsole() {
console.log('Now you see me');
setTimeout(() => console.log("Now you don't"), 100);
setImmediate(() => console.log("Now still"));
}
@Farenheith
Farenheith / double-console.spec.ts
Last active February 21, 2020 09:01
Unit test for global timing functions
import { expect } from 'chai';
import { SinonFakeTimers, useFakeTimers, stub, match } from 'sinon';
import { doubleConsole } from '../src/double-console';
describe.only('doubleConsole()', () => {
let clock: SinonFakeTimers;
beforeEach(() => {
clock = useFakeTimers();
stub(global, 'setImmediate').callThrough();
@Farenheith
Farenheith / double-console.spec.ts
Created February 21, 2020 08:58
Another way to test global functions
import { expect } from 'chai';
import { match, stub } from 'sinon';
import { doubleConsole } from '../src/double-console';
describe.only('doubleConsole()', () => {
let callbackSetImmediate: any;
let callbackSetTimeout: any;
beforeEach(() => {
stub(global, 'setImmediate').callsFake((x) => {
import { Readable } from 'stream';
import { createGzip } from 'zlib';
export function streamExample(stream: Readable) {
return stream
.pipe(createGzip());
}
@Farenheith
Farenheith / example-stream.spec.ts
Last active February 22, 2020 15:10
An example of a nasty stream test
import { expect } from 'chai';
import { stub } from 'sinon';
import * as zlib from 'zlib';
import { streamExample } from '../src/stream-example';
import { Readable, Writable } from 'stream';
describe('streamExample()', () => {
let data: any[];
let writable: Writable;
@Farenheith
Farenheith / example-stream.spec.ts
Last active February 22, 2020 15:49
Simple test for example-stream.ts
import { expect } from 'chai';
import { stub } from 'sinon';
import * as zlib from 'zlib';
import { streamExample } from '../src/stream-example';
describe.only('streamExample()', () => {
beforeEach(() => {
stub(zlib, 'createGzip').returns('createGZip result' as any);
});
@Farenheith
Farenheith / not-so-simple-stream-example.ts
Last active February 22, 2020 15:45
An eventier stream handler
import { Readable } from 'stream';
import { gzip } from 'zlib';
export async function notSoSimpleStreamExample(stream: Readable) {
return new Promise((resolve, reject) => {
const data: Array<Promise<Buffer>> = [];
stream.on('data', info => {
data.push(new Promise((resolve, reject) => {
gzip(info, (error, result) => {
if (error) {
@Farenheith
Farenheith / not-so-simple-stream-example.spec.ts
Last active February 22, 2020 16:38
An example of tests of a function with stream and promises
import { expect } from 'chai';
import { stub, SinonStub, match } from 'sinon';
import * as zlib from 'zlib';
import { notSoSimpleStreamExample } from '../src/not-so-simple-stream-example';
import { ObjectReadableMock } from 'stream-mock';
describe('notSoSimpleStreamExample()', () => {
let data: any[] = [];
let gzip: SinonStub<any, any>;
import { EventEmitter } from 'events';
export function eventEmitterExample(emitter: EventEmitter) {
emitter.on('somethingHappen', (x) => {
console.info(`Something happened! ${x}`);
});
emitter.on('sometingGoesWrong', (x) => {
console.error(`Something went wrong! ${x}`);
});