Skip to content

Instantly share code, notes, and snippets.

View JoshuaKGoldberg's full-sized avatar

Josh Goldberg ✨ JoshuaKGoldberg

View GitHub Profile
@JoshuaKGoldberg
JoshuaKGoldberg / sinon-stub-return.ts
Created January 31, 2017 20:16
Sinon stub with a return value
attempt_CallsOnFail_IfMyApiFails() {
// Arrange
const onFail = sinon.stub();
const onSucceed = sinon.stub();
const externalApi = sinon.stub().returns(false);
const actor = new Actor({ externalApi, onFail, onSucceed });
// Act
actor.attempt(); // internally calls externalApi
@JoshuaKGoldberg
JoshuaKGoldberg / lolex-clock.ts
Created January 31, 2017 20:17
Lolex clock for simulating delays
actAfterDelay_CallsAction_AfterDelay() {
// Arrange
const action = sinon.stub();
const clock = lolex.createClock<lolex.BrowserClock>();
const delay = 100;
const actor = new Actor({ action, clock, delay });
// Act
actor.actAfterDelay();
clock.tick(delay);
@JoshuaKGoldberg
JoshuaKGoldberg / component.ts
Created December 30, 2017 21:41
Ridiculously tiny IoC with almost no features
import { IComponentListing } from "./listings";
import { getFunctionName } from "../utils";
export const component = (componentFunction: any, initializer?: Function) => {
const componentFunctionName = getFunctionName(componentFunction);
return initializer === undefined
? createComponentClass(componentFunction, componentFunctionName)
: createComponentInitializer(componentFunction, componentFunctionName, initializer);
};
@JoshuaKGoldberg
JoshuaKGoldberg / npm-debug.log
Created January 17, 2018 21:38
Errors from npm i with an empty package-lock.json
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\jogol\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'i' ]
2 info using [email protected]
3 info using [email protected]
4 verbose npm-session c1cc1bbe74c6487e
5 silly install runPreinstallTopLevelLifecycles
6 silly preinstall [email protected]
7 info lifecycle [email protected]~preinstall: [email protected]
@JoshuaKGoldberg
JoshuaKGoldberg / CreateRouteDetails.ts
Last active May 25, 2018 14:02
🃏 Unit Tests: SOLID, Modern, Electric Boogaloo 🃏
export const createRouteDetails = (lookupId?: string): IRouteDetails => {
if (lookupId === undefined) {
return {
pageMode: PageMode.MySways,
};
}
return {
lookupId,
pageMode: PageMode.Document,
@JoshuaKGoldberg
JoshuaKGoldberg / download-selenium-drivers.ps1
Last active April 19, 2025 08:14
PowerShell script to download major browser WebDriver drivers for Selenium.
[Net.ServicePointManager]::SecurityProtocol = "Ssl3, Tls, Tls11, Tls12";
$currentDir = (Get-Item -Path "./").FullName;
$driversDirName = Join-Path $currentDir ".drivers";
$isWindows = [System.Boolean](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue);
$webClient = New-Object System.Net.WebClient;
function Ensure-Driver-Exists($browserName, $exeName, $download, $zipName) {
$localExeName = Join-Path $driversDirName $exeName;
if (Test-Path $localExeName) {
@JoshuaKGoldberg
JoshuaKGoldberg / test-event-emitting.ts
Created June 1, 2018 03:10
Sample of event listening with selenium-webdriver and squee
import { createEventEmitter, IEventReceiver } from "squee";
import * as Selenium from "selenium-webdriver";
// Put whatever you'd like in this...
interface IPostMessageData {
identifier: string;
}
declare const window: {
__MY_IS_EVENT_DATA_VALID__: (data: any) => data is IPostMessageData;
@JoshuaKGoldberg
JoshuaKGoldberg / LogicMathLol.ts
Last active May 3, 2023 18:25
Silly Binary Arithmetic in TypeScript's Type System
type Bit = 0 | 1;
type BitOr<A extends Bit, B extends Bit> =
[A, B] extends [0, 0] ? 0 :
[A, B] extends [0, 1] | [1, 0] | [1, 1] ? 1 :
Bit
;
type BitAnd<A extends Bit, B extends Bit> =
[A, B] extends [0, 0] | [1, 0] | [0, 1] ? 0 :
@JoshuaKGoldberg
JoshuaKGoldberg / clipboard-polyfill.d.ts
Last active May 2, 2019 12:56
Removing an incorrect @types definition
// webpack/assets/javascripts/clipboard-polyfill.d.ts
// This file should be in that path 👆 but Gists don't allow subpaths...
// Overrides incorrect types in node_modules/clipboard-polyfill/build/clipboard-polyfill.d.ts
// See https://github.com/lgarron/clipboard-polyfill/issues/106
@JoshuaKGoldberg
JoshuaKGoldberg / esri.d.ts
Last active May 2, 2019 12:59
Replacing an incorrect @types definition
// webpack/assets/javascripts/esri.d.ts
// This file should be in that path 👆 but Gists don't allow subpaths...
declare module "esri" {
const wat: string;
export wat;
}