Skip to content

Instantly share code, notes, and snippets.

View LeeCheneler's full-sized avatar

Lee Cheneler LeeCheneler

  • Enzyme Software
  • UK
  • 10:06 (UTC +01:00)
View GitHub Profile
@LeeCheneler
LeeCheneler / find-keys-with-numeric-value.js
Created December 4, 2020 13:45
find key paths in object with numeric value
const findKeysWithNumericValue = (obj, keyPath = "") => {
const keys = Object.keys(obj)
const numericKeys = []
const preKey = keyPath.length > 0 ? `${keyPath}.` : ""
for (let k of keys) {
const value = obj[k]
if (typeof value === "number") {
numericKeys.push(`${preKey}${k}`)
} else if (typeof value === "object") {
import "@testing-library/jest-dom/extend-expect";
import { cleanup } from "@testing-library/react";
import { axe, toHaveNoViolations } from "jest-axe";
// stop RTL from cleaning up after itself so we can assert on DOM after tests with aXe
process.env.RTL_SKIP_AUTO_CLEANUP = "true";
// configure aXe
expect.extend(toHaveNoViolations);
@LeeCheneler
LeeCheneler / render-with-next-router.tsx
Last active December 2, 2020 12:22
render with mock nextjs router
import React from "react";
import { render as RTLRender } from "@testing-library/react";
import { NextRouter } from "next/router";
import { RouterContext } from "next/dist/next-server/lib/router-context";
interface RenderOptions {
router: Partial<NextRouter>;
}
export const render = (Component: JSX.Element, options?: RenderOptions) => {
@LeeCheneler
LeeCheneler / apollo-test-client.ts
Created August 5, 2020 22:34
apollo server test client with support for context
import { createTestClient } from "apollo-server-testing";
import { ApolloServer } from "apollo-server";
import {
createResolvers,
typeDefs,
createContext,
Context,
CreateContextOptions,
} from "./server";
import { DocumentNode } from "graphql";
@LeeCheneler
LeeCheneler / error-boundary-example-app.tsx
Last active June 3, 2020 09:18
Reporting error boundary with fallback
import React from "react";
import { ErrorBoundary } from "./error-boundary";
export const App = () => {
return (
<React.StrictMode>
<ErrorBoundary report={console.error}>
<h1>Example</h1>
</ErrorBoundary>
</React.StrictMode>
@LeeCheneler
LeeCheneler / package-json.ts
Created May 4, 2020 14:59
typescript type for package.json
export interface Package {
name: string;
version: string;
description?: string;
keywords?: string[];
homepage?: string;
bugs?: {
url: string;
email: string;
@LeeCheneler
LeeCheneler / useful-commands.sh
Created April 29, 2020 10:56
useful commands
# remove all node_modules directories
find . -name node_modules | xargs rm -rf
# remove ll coverage directories
find . -name coverage | xargs rm -rf
# you get the gist... heh
@LeeCheneler
LeeCheneler / run-cli-command.ts
Created April 13, 2020 22:10
run cli command for tests
import { spawn } from "child_process";
import terminate from "terminate";
export interface RunCliCommandOptions {
cwd?: string;
}
export const runCliCommand = (
command: string,
options: RunCliCommandOptions = { cwd: process.cwd() }
@LeeCheneler
LeeCheneler / extendExpect.ts
Last active April 14, 2020 11:38
expect.toContainInOrder
import chalk from "chalk";
const containsInOrder = (received: string[], expected: string[]) => {
let lastIndex = 0;
const lastOccuranceMap: { [key: string]: number } = {};
for (let e of expected) {
const index = received.indexOf(e);
// Not present at all
import fs from "fs";
import path from "path";
import { prettyDOM } from "@testing-library/react";
const writeDomToFile = (filename = "test-dom.html") => {
const dom = prettyDOM(document.body, 9999999, { highlight: false });
fs.writeFileSync(path.resolve(__dirname, filename), dom);
}