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 / launch.json
Last active September 2, 2024 00:51
Launch.json example for vscode using nvm to get node version directly from .nvmrc
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch program",
"skipFiles": ["<node_internals>/**"],
"cwd": "${workspaceRoot}",
"envFile": "${workspaceFolder}/.env",
"runtimeExecutable": "${env:NVM_DIR}/nvm-exec",
import { expect } from 'chai';
import { stub, match } from 'sinon';
import { anonymousExample } from '../src/anonymous-example';
describe('anonymousExample()', () => {
beforeEach(() => {
stub(console, 'log');
});
it('should log first v when v is 1', () => {
export function anonymousExample(callbacker: (x: (v: number) => void) => void) {
callbacker((v) => {
if (v === 1) {
console.log('first v!');
} else {
console.log('another v!');
}
});
}
import { expect } from 'chai';
import { stub, match, SinonStub } from 'sinon';
import request = require('superagent');
import { superagentExample } from '../src/superagent-example';
describe('superAgentExample()', () => {
let send: SinonStub;
let set1: SinonStub;
let set2: SinonStub;
@Farenheith
Farenheith / superagent-example.spec.ts
Created February 29, 2020 06:22
Example of test to the first example of code in https://visionmedia.github.io/superagent/
import { expect } from 'chai';
import { stub, match } from 'sinon';
import request = require('superagent');
import { superagentExample } from '../src/superagent-example';
describe('superAgentExample()', () => {
let requestResult: request.SuperAgentRequest;
beforeEach(() => {
requestResult = Promise.resolve({ body: 'expected body' }) as any;
import { EventEmitter } from 'events';
import { expect } from 'chai';
import { stub } from 'sinon';
import { eventEmitterExample } from '../src/event-emitter-example';
describe.only('eventEmitterExample()', () => {
beforeEach(() => {
stub(console, 'info');
stub(console, 'error');
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}`);
});
@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>;
@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 / 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);
});