Skip to content

Instantly share code, notes, and snippets.

View traviskaufman's full-sized avatar

Travis Kaufman traviskaufman

View GitHub Profile
@traviskaufman
traviskaufman / play_framework_testing_async_responses.md
Last active June 8, 2023 09:09
Testing Asynchronous HTTP Responses in Play Framework

Testing Asynchronous Responses in Play! Framework

TL;DR

The Background

I came across this issue while I was working on a functional test for JAMLive!, a Play! application I'm currently working on obsessing over in my free time. I have a controller with a connect method that looks like this:

@traviskaufman
traviskaufman / logback_disable_in_unit_tests.md
Last active March 20, 2023 08:38
Logback: Disable all logging in unit tests

After scouring the internet and piece-mealing together the correct way to do this, here is a step-by-step, all-in-one-place guide to making logback STFU when running your unit tests.

Here's how to do it

Save the following as logback-test.xml under src/test/resources:

<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%msg%n</pattern>
@traviskaufman
traviskaufman / jasmine-this-vars.md
Last active September 19, 2022 14:35
Better Jasmine Tests With `this`

Better Jasmine Tests With this

On the Refinery29 Mobile Web Team, codenamed "Bicycle", all of our unit tests are written using Jasmine, an awesome BDD library written by Pivotal Labs. We recently switched how we set up data for tests from declaring and assigning to closures, to assigning properties to each test case's this object, and we've seen some awesome benefits from doing such.

The old way

Up until recently, a typical unit test for us looked something like this:

describe('views.Card', function() {
@traviskaufman
traviskaufman / merge-intervals.js
Last active May 14, 2020 17:06
Merge Intervals - O(n) average case time rather than THETA(nlogn)
// See: http://www.programcreek.com/2012/12/leetcode-merge-intervals/
console.log(mergeIntervals([[1,3],[2,6],[8,10],[15,18]]));
console.log(mergeIntervals([[1,8], [2,10], [3,6]]));
console.log(mergeIntervals([[1,8], [2, 8], [3, 6]]));
function mergeIntervals(intervals) {
if (intervals.length < 2) {
return intervals;
}
@traviskaufman
traviskaufman / 02-07-demystifying-rxjs-concatMap.ts
Last active November 27, 2019 22:26
Demystifying RxJS, Part II: concatMap
function concatMap<T, R>(
project: (value: T, index: number) => Observable<R>
): OperatorFunction<T, R> {
let currentIndex = 0;
const buffer: Array<Observable<R>> = [];
const subscribeTo = (
projected: Observable<R>,
obs: Observer<R>,
subscriptions: Set<Subscription>
@traviskaufman
traviskaufman / 02-05-demystifying-rxjs-mergemap.ts
Last active November 26, 2019 17:31
Demystifying RxJS, Part II: MergeMap
function mergeMap<T, R>(
project: (value: T, index: number) => Observable<R>
): OperatorFunction<T, R> {
let currentIndex = 0;
return source =>
new Observable(obs => {
const subscriptions = new Set<Subscription>();
const sub = source.subscribe(
x => {
const projected = project(x, currentIndex++);
@traviskaufman
traviskaufman / 04-demystifying-rxjs-from.ts
Last active November 21, 2019 18:57
Demystifying RxJS, Part I: from()
function from<T>(convertible: Promise<T> | ArrayLike<T>): Observable<T> {
if (typeof (convertible as any).length === "number") {
const arrayLike = convertible as ArrayLike<T>;
return new Observable(obs => {
for (let i = 0; i < arrayLike.length; i++) {
obs.next(arrayLike[i]);
}
obs.complete();
});
}
@traviskaufman
traviskaufman / scrape-timestamps.js
Created October 31, 2019 01:50
Timestamp scraping script used for r/dataisbeautiful October 2019 Challenge
const fs = require('fs');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
main().catch(err => console.error(err));
async function main() {
const SCARE_RE = /^((\d{1,2}):(\d{1,2})(?::(\d{1,2}))?).*– (.+)$/;
const $ = cheerio.load(fs.readFileSync('./notebooks/movielist.html', 'utf8'));
const $movieNames = $('td.column-1 > a');
@traviskaufman
traviskaufman / 03-02-demystifying-rxjs-schedulers.ts
Created October 28, 2019 21:40
Demystifying RxJS, Part III: Concrete Schedulers
const syncScheduler: Scheduler = {
schedule(work) {
work();
}
};
const asyncScheduler: Scheduler = {
schedule(work) {
setTimeout(work, 0);
}
@traviskaufman
traviskaufman / 03-01-demystifying-rxjs-scheduler-interface.ts
Created October 28, 2019 21:37
Demystifying RxJS, Part III: Scheduler Interface
interface Scheduler {
schedule(work: Function): void;
}