Skip to content

Instantly share code, notes, and snippets.

View epreston's full-sized avatar

Ed Preston epreston

View GitHub Profile
@epreston
epreston / resolveWhenIdle.js
Last active April 28, 2023 22:01
wait for idle promise - runs async right before paint - safari fallback
const idleOptions = { timeout: 500 };
const request = window.requestIdleCallback || window.requestAnimationFrame;
const cancel = window.cancelIdleCallback || window.cancelAnimationFrame;
const resolveWhenIdle = {
request,
cancel,
promise: (num) => new Promise((resolve) => request(resolve, Object.assign({}, idleOptions, num))),
};
@epreston
epreston / addAppMountLifecycleHooks.js
Created April 28, 2023 19:58
vue - add app.mount lifecycle hooks - onBeforeMount and onAfterMount
// add app mount lifecycle hooks to the app instance for plugins to use when registering.
function addAppMountLifecycleHooks(app) {
const beforeHooks = [];
const afterHooks = [];
const boundMount = app.mount.bind(app);
app.mount = (async (rootContainer, isHydrate, isSVG) => {
for (const preHook of beforeHooks) await preHook(app);
boundMount(rootContainer, isHydrate, isSVG);
@epreston
epreston / basic.robots.txt
Created April 28, 2023 19:37
robots.txt - basic robots.txt with no configuration
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
@epreston
epreston / vitest.setup.js
Created April 28, 2023 19:30
vitest - example setup file
// setup.js - vitest
// This contains code that should be made available to every test and
// code that should be run before any mock or import in a test file. This
// file will execute just after the test file's environment has been loaded.
// if (typeof window === 'undefined') {
// global.window = {};
// }
@epreston
epreston / vue.unit.test.js
Last active April 28, 2023 19:16
vitest - vue test file structure example
// @vitest-environment jsdom
import { config, mount, shallowMount } from '@vue/test-utils';
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
// ---------------------------------------------------------
// Imports used by options API or compiled component
import {
createBlock,
@epreston
epreston / sanity.test.js
Created April 28, 2023 18:57
vitest - example sanity test for test environment
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
// ---------------------------------------------------------
describe('test environment', () => {
it('is working as expected', () => {
const obj = {};
expect(obj).toBeDefined();
expect(true).toBeTruthy();
});
@epreston
epreston / jsdom.test.js
Created April 28, 2023 18:54
vitest - example test file structure for browser context testing with jsdom
// @vitest-environment jsdom
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
// ---------------------------------------------------------
// import { itemsToMock } from '../src/testDependency.js';
// ---------------------------------------------------------
@epreston
epreston / example.test.js
Created April 28, 2023 18:48
vitest - generic test file format example
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
// ---------------------------------------------------------
// import { itemsToMock } from '../src/testDependency.js';
// ---------------------------------------------------------
// vi.mock('../src/testDependency.js');
@epreston
epreston / test-templates.js
Created April 28, 2023 18:44
vitest - generic test structures and examples
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
// ---------------------------------------------------------
// template unit test functions
// ---------------------------------------------------------
const isFn = (a) => typeof a === 'function';
const isString = (a) => typeof a === 'string';
@epreston
epreston / typechecks.js
Created April 28, 2023 18:40
javascript - type comparison functions to cleanup code
const isFn = (a) => typeof a === 'function';
const isString = (a) => typeof a === 'string';
const isNumber = (a) => typeof a === 'number';
const isArray = Array.isArray;
const isObject = (a) => a !== null && typeof a === 'object';
const isActualObject = (a) => isObject(a) && !isArray(a);