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 / 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;
import { Readable } from 'stream';
import { createGzip } from 'zlib';
export function streamExample(stream: Readable) {
return stream
.pipe(createGzip());
}
@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) => {
@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.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 / 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 / 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);
}
@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-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-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);
}