Skip to content

Instantly share code, notes, and snippets.

View graffhyrum's full-sized avatar

Joshua Pendragon graffhyrum

View GitHub Profile
/**
* Creates a typed key-value pair object to work around TypeScript issue #13948.
* This function preserves the literal type of the key in the resulting object type.
*
* @see [source](https://github.com/microsoft/TypeScript/issues/13948#issuecomment-1333159066)
*
* @example
* const obj = createTypedKeyValue('userId', 123);
* // obj has type: { userId: number } with literal key preservation
*

This script is a vetting tool designed to automate the process of running tests, retrying failures if necessary, and saving the results of each test cycle to a session-specific directory. Here's a summary of its functionality:

Key Features and Workflow:

  1. Session Setup:

    • Creates a session-specific directory based on the current timestamp and Git branch name.
    • Ensures the base directory (vetting-results) exists and creates the session directory recursively.
  2. User-Specified Test Parsing:

    • Parses command-line arguments to extract user-specified tests to run.
@graffhyrum
graffhyrum / LoggingWrapper.ts
Created February 20, 2025 19:11
A proxy wrapper for services to allow logging functionality for requests, responses, and errors.
import { file } from 'bun'
import { mkdirSync, existsSync } from 'node:fs'
import { join } from 'node:path'
const logFolder = 'LogFolder'
const logFilePath = join(process.cwd(), logFolder, 'proxy.log')
// Ensure the log folder exists
if (!existsSync(logFolder)) {
mkdirSync(logFolder)
/**
* ProcessEnvFacade is an object that provides methods to interact with environment variables.
* It provides methods to get, set, and validate environment variables.
*/
const ProcessEnvFacade = {
getValueOrThrow: (key: keyof NodeJS.ProcessEnv): string => {
const maybeKey = process.env[key];
if (!maybeKey) {
throw new Error(`Environment variable ${key} is not set`);
}
@graffhyrum
graffhyrum / find-my-file.yaml
Created November 1, 2024 16:46
A Github Action that provides debug information about file paths.
name: 'Find My File'
description: 'Action that provides debug information about file paths'
inputs:
runnerOs:
description: 'The runner operating system'
required: true
filename:
description: 'The filename to search for'
required: true
@graffhyrum
graffhyrum / modify.md
Created October 9, 2024 20:05
Swaps the type of a parameter to something else
/**
 * Modify a type `T` by replacing properties with the properties of type `R`.
 *
 * @template T - The original type.
 * @template R - The type with properties to replace in `T`.
 */
type Modify<T, R> = Omit<T, keyof R> & R;
@graffhyrum
graffhyrum / browserWrapper.ts
Last active July 26, 2024 20:18
Wrap some callback action in a new browser instance with setup and teardown handled
import {
BrowserContextOptions,
BrowserType,
LaunchOptions,
Page,
} from '@playwright/test';
function doWithNewBrowser(
callback: (page: Page, ...args: any[]) => Promise<any>,
browser: BrowserType,
@graffhyrum
graffhyrum / playwright.yaml
Created July 10, 2024 21:55
A good github action template for running playwright tests using Bun
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
@graffhyrum
graffhyrum / PlaywrightShowcase.md
Last active June 4, 2024 23:09
Micro Playwright config example

These files are an illustration of the benefits available in playwright test projects. It leverages parallelization and composed fixtures to create a matrix of tests for different combinations of test environments, browsers, and resolutions. The following is a brief description of the files in their execution order:

  1. playwright.config.ts: This is the entry point for the Playwright tests. It defines the configuration for the tests and uses the getProjects function to generate a matrix of tests for different combinations of environments, browsers, and resolutions.

  2. getProjects.ts: This file defines a function getProjects that generates a matrix of tests for different combinations of environments, browsers, and resolutions. The function can be customized with specific environments, browsers, and resolutions to test.

  3. params.ts: This file defines several constants and types related to the testable environments, browsers, and resolutions. It also defines the types TestableEnvironment, `Testabl

Summary

The sendTabUntilFocused function is an asynchronous function in TypeScript that simulates the pressing of the 'Tab' key until a specific element on the webpage is focused. This function is useful in automated testing scenarios where you need to simulate user interactions with a webpage.

Signature

async function sendTabUntilFocused(page: Page, selector: ReturnType<typeof page.locator>): Promise<void>

Parameters

  • page: An instance of the Page class from the Playwright library, representing a single tab in a browser.