Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Migrate tests from Jest to Bun #411

Merged
merged 38 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cdf7e2a
Configure bun to run the tests
titouanmathis Feb 16, 2024
71b55d7
Migrate first batch of tests to bun
titouanmathis Feb 16, 2024
55d6ced
Migrate more tests to bun
titouanmathis Feb 16, 2024
d0ebc4c
Remove the legacy autoBind function
titouanmathis Feb 16, 2024
cbd4e20
Migrate some more tests to bun
titouanmathis Feb 16, 2024
90f1c68
Migrate more tests
titouanmathis Feb 16, 2024
a5afc9a
Add `raf` and `cancelRaf` utils export
titouanmathis Feb 16, 2024
9f8a9f5
Add an `isEmptyString` utility function
titouanmathis Feb 16, 2024
3bea8aa
Improve types
titouanmathis Feb 16, 2024
3e3754d
Migrate tests to bun
titouanmathis Feb 16, 2024
f2ddfbb
Migrate helpers test to bun
titouanmathis Feb 16, 2024
928e7d2
Migrate decorators tests to bun
titouanmathis Feb 16, 2024
b552a94
Migrate some more tests
titouanmathis Feb 16, 2024
5297750
Update script
titouanmathis Feb 16, 2024
0d6c2ed
Add tests
titouanmathis Feb 19, 2024
764083d
Improve types
titouanmathis Feb 19, 2024
56bf1f5
Remove JS tests
titouanmathis Feb 19, 2024
a3682ea
Improve types
titouanmathis Feb 19, 2024
6b70fa0
Improve tests
titouanmathis Feb 19, 2024
352f509
Improve raf service usage
titouanmathis Feb 19, 2024
5151fba
Fix tests
titouanmathis Feb 19, 2024
fb9d7e2
Fix GitHub action test command
titouanmathis Feb 19, 2024
5e9b9a3
Fix tests
titouanmathis Feb 19, 2024
dd0e065
Fix build
titouanmathis Feb 19, 2024
d3e3d7a
Fix lint
titouanmathis Feb 19, 2024
c5a5d35
Lint .md files
titouanmathis Feb 19, 2024
da10b0a
Lint .js files
titouanmathis Feb 19, 2024
4d7ea75
Fix tests
titouanmathis Feb 19, 2024
34bb2c5
Refactor the requestAnimationFrame polyfill
titouanmathis Feb 19, 2024
e25b7d1
Improve nextFrame doc
titouanmathis Feb 19, 2024
2929414
Update VitePress
titouanmathis Feb 19, 2024
3751fd7
Remove doc from unexported function
titouanmathis Feb 19, 2024
dfd62b6
Improve types
titouanmathis Feb 19, 2024
f6aed05
Fix tests
titouanmathis Feb 19, 2024
d099358
Fix tests
titouanmathis Feb 19, 2024
9d78714
Update changelog
titouanmathis Feb 19, 2024
054f3a9
Fix docs build
titouanmathis Feb 19, 2024
c8b6798
Fix demo build
titouanmathis Feb 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: npm run build

- name: Test
run: npm run test -- -- --coverage || npm run test -- -- --coverage
run: npm run test -- -- --coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install modules
run: npm install
- name: Run tests
run: npm run test -- -- --coverage --runInBand --ci || npm run test -- -- --coverage --runInBand --ci
run: npm run test -- -- --coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ All notable changes to this project will be documented in this file. The format

- ⚠️ Refactor definition of breakpoints for the resize service ([#323](https://github.com/studiometa/js-toolkit/pull/323))
- ⚠️ Refactor `focusTrap` for simpler exports ([#406](https://github.com/studiometa/js-toolkit/pull/406))
- Migrate tests from Jest to Bun ([#411](https://github.com/studiometa/js-toolkit/pull/411))
- Refactor the requestAnimationFrame polyfill ([#411](https://github.com/studiometa/js-toolkit/pull/411), [dfd62b6](https://github.com/studiometa/js-toolkit/commit/dfd62b6), [34bb2c5](https://github.com/studiometa/js-toolkit/commit/34bb2c5))
- Improve raf service usage ([#411](https://github.com/studiometa/js-toolkit/pull/411), [352f509](https://github.com/studiometa/js-toolkit/commit/352f509))

### Removed

Expand Down
821 changes: 629 additions & 192 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"packages/*"
],
"scripts": {
"postinstall": "patch-package",
"demo:dev": "npm run dev --workspace=@studiometa/js-toolkit-demo",
"demo:watch": "npm run watch --workspace=@studiometa/js-toolkit-demo",
"demo:build": "npm run build --workspace=@studiometa/js-toolkit-demo",
"docs:dev": "npm run dev --workspace=@studiometa/js-toolkit-docs",
"docs:build": "npm run build --workspace=@studiometa/js-toolkit-docs",
"docs:preview": "npm run preview --workspace=@studiometa/js-toolkit-docs",
"test": "npm run test --workspace=@studiometa/js-toolkit-tests",
"lint": "npm run lint:eslint && npm run lint:types && npm run lint:docs",
"lint:eslint": "eslint packages/js-toolkit --ext=.js,.ts --cache",
Expand Down Expand Up @@ -40,6 +42,7 @@
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-import-resolver-exports": "^1.0.0-beta.5",
"eslint-plugin-jest": "^27.6.1",
"patch-package": "^8.0.0",
"prettier": "^2.8.8",
"typescript": "^5.3.3"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export default defineConfig({
{
text: `v${pkg.version}`,
items: [
{ text: 'Release Notes', link: 'https://github.com/studiometa/js-toolkit/blob/master/CHANGELOG.md' },
{
text: 'Release Notes',
link: 'https://github.com/studiometa/js-toolkit/blob/master/CHANGELOG.md',
},
{ text: 'Demo', link: 'https://studiometa-js-toolkit-demo.netlify.app/' },
],
},
Expand Down
5 changes: 3 additions & 2 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"private": true,
"scripts": {
"dev": "vitepress dev .",
"build": "vitepress build ."
"build": "vitepress build .",
"preview": "vitepress preview ."
},
"dependencies": {
"@studiometa/tailwind-config": "^2.1.0",
"tailwindcss": "^3.4.1",
"vitepress": "^1.0.0-rc.36"
"vitepress": "^1.0.0-rc.44"
}
}
6 changes: 3 additions & 3 deletions packages/docs/utils/nextFrame.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nextFrame

Execute a given function in the next window frame.
Execute a given function in the next window frame. This function is a promisified version of the [`requestAnimationFrame` function](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame).

## Usage

Expand All @@ -19,10 +19,10 @@ console.log('I will be executed in the next frame!');

### Parameters

- `fn` (`Function`): the function to execute
- `callback` (`(time:DOMHighResTimeStamp) => unknown`): the function to execute

[Source](https://github.com/studiometa/js-toolkit/blob/master/src/utils/nextFrame.js)

### Return value

This function returns a `Promise` resolving on the next frame.
This function returns a `Promise` resolving on the next frame with the return value of the provided callback.
1 change: 0 additions & 1 deletion packages/docs/utils/trapFocus.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ The `untrapFocus` function does not need any argument.
## Return value

Both functions return nothing.

7 changes: 7 additions & 0 deletions packages/js-toolkit/Base/managers/ActionManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AbstractManager } from './AbstractManager.js';

export class ActionManager extends AbstractManager {
registerAll() {
this.__element.querySelectorAll('[data-on]');
}
}
20 changes: 19 additions & 1 deletion packages/js-toolkit/decorators/withScrolledInView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,28 @@ export function getEdgeWithOffset(start: number, size: number, offset: string |
return start;
}

type HorizontalOnlyRect = {
x: number;
width: number;
};

type VerticalOnlyRect = {
y: number;
height: number;
};

type HorizontalRect = HorizontalOnlyRect & Partial<VerticalOnlyRect>;
type VerticalRect = Partial<HorizontalOnlyRect> & VerticalOnlyRect;

/**
* Get starting and ending edges for a given axis, a target sizings, a container sizings and their offset.
*/
export function getEdges(axis: 'x' | 'y', targetSizes, containerSizes, offset): [number, number] {
export function getEdges<T extends 'x' | 'y'>(
axis: T,
targetSizes: T extends 'x' ? HorizontalRect : VerticalRect,
containerSizes: T extends 'x' ? HorizontalRect : VerticalRect,
offset: NormalizedOffset,
): [number, number] {
const sizeKey = axis === 'x' ? 'width' : 'height';
const targetStart = getEdgeWithOffset(targetSizes[axis], targetSizes[sizeKey], offset[0][0]);
const containerStart = getEdgeWithOffset(0, containerSizes[sizeKey], offset[0][1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export function withScrolledInView<S extends Base = Base>(
render();
},
scrolled: (props) => {
if ((!this.$services.has('ticked') && props.changed.y) || props.changed.x) {
if (props.changed.y || props.changed.x) {
this.$services.enable('ticked');
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/helpers/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isDefined } from '../utils/index.js';

export type CreateAppOptions = {
root: HTMLElement;
features: Features;
features: Partial<Features>;
breakpoints?: Record<string, string>;
};

Expand Down
8 changes: 4 additions & 4 deletions packages/js-toolkit/services/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ const instances = new Map<HTMLElement | Window, PointerService>();
/**
* Use the pointer service.
*/
export default function usePointer(target: HTMLElement | undefined): PointerService {
if (!instances.has(target)) {
instances.set(target, createPointerService(target));
export default function usePointer(target?: HTMLElement): PointerService {
if (!instances.has(target ?? window)) {
instances.set(target ?? window, createPointerService(target));
}
return instances.get(target);
return instances.get(target ?? window) as PointerService;
}
6 changes: 2 additions & 4 deletions packages/js-toolkit/services/raf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */
import { useService } from './service.js';
import { getRaf as getRequestAnimationFrame } from '../utils/nextFrame.js';
import { useScheduler } from '../utils/scheduler.js';
import { isFunction } from '../utils/is.js';
import type { ServiceInterface } from './index.js';
Expand All @@ -18,7 +17,6 @@ export interface RafServiceProps {
*/
function createRafService(): RafService {
let isTicking = false;
const RAF = getRequestAnimationFrame();

/**
* Trigger callbacks.
Expand Down Expand Up @@ -49,7 +47,7 @@ function createRafService(): RafService {
return;
}

RAF(loop);
requestAnimationFrame(loop);
}

const { add, remove, has, props, callbacks } = useService({
Expand All @@ -58,7 +56,7 @@ function createRafService(): RafService {
} as RafServiceProps,
init() {
isTicking = true;
RAF(loop);
requestAnimationFrame(loop);
},
kill() {
isTicking = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/utils/SmartQueue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Queue } from './Queue.js';
import nextTick from './nextTick.js';
import { nextTick } from './nextTick.js';

/**
* A task is considered long if it blocks the main thread for more thant 50ms.
Expand Down
20 changes: 13 additions & 7 deletions packages/js-toolkit/utils/css/animate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lerp, map } from '../math/index.js';
import { isDefined, isFunction, isNumber } from '../is.js';
import transform, { TRANSFORM_PROPS } from './transform.js';
import { transform, TRANSFORM_PROPS } from './transform.js';
import { domScheduler as scheduler } from '../scheduler.js';
import { tween, normalizeEase } from '../tween.js';
// eslint-disable-next-line import/extensions
Expand All @@ -12,14 +12,20 @@ import type { BezierCurve, TweenOptions } from '../tween.js';

export type CSSCustomPropertyName = `--${string}`;

export type Keyframe = TransformProps & {
opacity?: number;
transformOrigin?: string;
easing?: EasingFunction | BezierCurve;
offset?: number;
[key: CSSCustomPropertyName]: number;
type KeyframeTransforms<Type> = {
[Property in keyof Type]: number | [number, string];
};

export type Keyframe = Partial<
KeyframeTransforms<TransformProps> & {
opacity: number;
transformOrigin: string;
easing: EasingFunction | BezierCurve;
offset: number;
[key: CSSCustomPropertyName]: number;
}
>;

export type NormalizedKeyframe = Keyframe & {
easing: EasingFunction;
offset: number;
Expand Down
3 changes: 2 additions & 1 deletion packages/js-toolkit/utils/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { add as addClass, remove as removeClass, toggle as toggleClass } from '.
export { add as addStyle, remove as removeStyle } from './styles.js';
export { default as getOffsetSizes } from './getOffsetSizes.js';
export { default as matrix } from './matrix.js';
export { default as transform } from './transform.js';
export { transform } from './transform.js';
export { default as transition } from './transition.js';
export type { TransformProps } from './transform.js';
2 changes: 1 addition & 1 deletion packages/js-toolkit/utils/css/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MatrixTransform = {
* // matrix(0.5, 0, 0, 0.5, 0, 0)
* ```
*/
export default function matrix(transform: MatrixTransform): string {
export default function matrix(transform?: MatrixTransform): string {
// eslint-disable-next-line no-param-reassign
transform = transform || {};
return `matrix(${transform.scaleX ?? 1}, ${transform.skewY ?? 0}, ${transform.skewX ?? 0}, ${
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/utils/css/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const TRANSFORM_PROPS = [
/**
* Generate a CSS transform.
*/
export default function transform(
export function transform(
elementOrElements: HTMLElement | HTMLElement[] | NodeListOf<HTMLElement>,
props: TransformProps,
): string {
Expand Down
5 changes: 2 additions & 3 deletions packages/js-toolkit/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ export * from './trapFocus.js';
export { default as keyCodes } from './keyCodes.js';
export { default as memoize } from './memoize.js';
export { nextFrame } from './nextFrame.js';
export { default as nextTick } from './nextTick.js';
export { default as nextMicrotask } from './nextMicrotask.js';
export { nextTick } from './nextTick.js';
export { nextMicrotask } from './nextMicrotask.js';
export { default as throttle } from './throttle.js';
export { default as scrollTo } from './scrollTo.js';
export { default as getComponentResolver } from './getComponentResolver.js';
export * from './is.js';
export * from './has.js';
// eslint-disable-next-line import/extensions
export * from './css/index.js';
export {
objectToURLSearchParams,
Expand Down
5 changes: 5 additions & 0 deletions packages/js-toolkit/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ export const isBoolean = (value: unknown): value is boolean => typeof value ===

// eslint-disable-next-line prefer-destructuring
export const isArray = Array.isArray;

/**
* Test if a given value is an empty string.
*/
export const isEmptyString = (value: unknown): boolean => isString(value) && value.length > 0;
43 changes: 7 additions & 36 deletions packages/js-toolkit/utils/nextFrame.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,17 @@
import { isFunction } from './is.js';
import { hasWindow } from './has.js';

/**
* RequestAnimation frame polyfill.
*
* @see https://github.com/vuejs/vue/blob/ec78fc8b6d03e59da669be1adf4b4b5abf670a34/dist/vue.runtime.esm.js#L7355
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function getRaf(): (handler: Function) => number {
return hasWindow() && window.requestAnimationFrame
? window.requestAnimationFrame.bind(window)
: setTimeout;
}

/**
* Get a function to cancel the method returned by `getRaf()`.
*/
export function getCancelRaf(): (id: number) => void {
return hasWindow() && window.cancelAnimationFrame
? window.cancelAnimationFrame.bind(window)
: clearTimeout;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Callback = (time?: DOMHighResTimeStamp) => any;

/**
* Wait for the next frame to execute a function.
*
* @template {() => any} T
* @param {T} [fn] The callback function to execute.
* @returns {Promise<T extends Function ? ReturnType<T> : undefined>} A Promise resolving when the next frame is reached.
* @example
* ```js
* nextFrame(() => console.log('hello world'));
*
* await nextFrame();
* console.log('hello world');
* ```
*/
export function nextFrame<T extends () => unknown>(
fn?: T,
): Promise<T extends () => unknown ? ReturnType<T> : void> {
export function nextFrame(): Promise<DOMHighResTimeStamp>;
export function nextFrame<T extends Callback>(callback?: T): Promise<ReturnType<T>>;
export function nextFrame<T extends Callback>(callback?: T): Promise<ReturnType<T>> {
const fn = hasWindow() ? window?.requestAnimationFrame ?? setTimeout : setTimeout;
return new Promise((resolve) => {
// @ts-ignore
getRaf()(() => resolve(isFunction(fn) && fn()));
fn((time) => resolve(isFunction(callback) ? callback(time) : time));
});
}
12 changes: 7 additions & 5 deletions packages/js-toolkit/utils/nextMicrotask.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { isFunction } from './is.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Callback = (...args: any[]) => any;

/**
* Wait for the next microtask.
*/
export default async function nextMicrotask<T extends () => unknown>(
fn?: T,
): Promise<T extends () => unknown ? ReturnType<T> : void> {
// @ts-ignore
return Promise.resolve().then(() => isFunction(fn) && fn());
export async function nextMicrotask(): Promise<void>;
export async function nextMicrotask<T extends Callback>(callback?: T): Promise<ReturnType<T>>;
export async function nextMicrotask<T extends Callback>(callback?: T): Promise<ReturnType<T>> {
return Promise.resolve().then(() => isFunction(callback) && callback());
}
12 changes: 7 additions & 5 deletions packages/js-toolkit/utils/nextTick.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { isFunction } from './is.js';
import { wait } from './wait.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Callback = (...args: any[]) => any;

/**
* Wait for the next tick.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default async function nextTick<T extends () => any>(
fn?: T,
): Promise<T extends void ? void : ReturnType<T>> {
return wait().then(isFunction(fn) && (fn as ReturnType<T>));
export async function nextTick(): Promise<void>;
export async function nextTick<T extends Callback>(callback?: T): Promise<ReturnType<T>>;
export async function nextTick<T extends Callback>(callback?: T): Promise<ReturnType<T>> {
return wait().then(isFunction(callback) && (callback as ReturnType<T>));
}
Loading
Loading