From 55d4da66530820dc188f9c7d3bc744cfe9eb77c5 Mon Sep 17 00:00:00 2001 From: blurfx Date: Thu, 12 Oct 2023 16:22:32 +0000 Subject: [PATCH] fix: lint --- src/document/presence/presence.ts | 4 +- test/vitest/env/index.ts | 164 +++++++++++++++--------------- 2 files changed, 85 insertions(+), 83 deletions(-) diff --git a/src/document/presence/presence.ts b/src/document/presence/presence.ts index e1c8a5d85..468de2b0a 100644 --- a/src/document/presence/presence.ts +++ b/src/document/presence/presence.ts @@ -30,8 +30,8 @@ export enum PresenceChangeType { * `PresenceChange` represents the change of presence. */ export type PresenceChange

= - | { type: PresenceChangeType.Put; presence: P; } - | { type: PresenceChangeType.Clear; }; + | { type: PresenceChangeType.Put; presence: P } + | { type: PresenceChangeType.Clear }; /** * `Presence` represents a proxy for the Presence to be manipulated from the outside. diff --git a/test/vitest/env/index.ts b/test/vitest/env/index.ts index 372fc8646..b11e93e5e 100644 --- a/test/vitest/env/index.ts +++ b/test/vitest/env/index.ts @@ -1,36 +1,36 @@ import { CookieJar, JSDOM, ResourceLoader, VirtualConsole } from 'jsdom'; -import { Environment } from 'vitest' -import { populateGlobal } from 'vitest/environments' +import { Environment } from 'vitest'; +import { populateGlobal } from 'vitest/environments'; function catchWindowErrors(window: Window) { - let userErrorListenerCount = 0 + let userErrorListenerCount = 0; function throwUnhandlerError(e: ErrorEvent) { if (userErrorListenerCount === 0 && e.error != null) - process.emit('uncaughtException', e.error) - } - const addEventListener = window.addEventListener.bind(window) - const removeEventListener = window.removeEventListener.bind(window) - window.addEventListener('error', throwUnhandlerError) - window.addEventListener = function (...args: Parameters) { - if (args[0] === 'error') - userErrorListenerCount++ - return addEventListener.apply(this, args) - } - window.removeEventListener = function (...args: Parameters) { - if (args[0] === 'error' && userErrorListenerCount) - userErrorListenerCount-- - return removeEventListener.apply(this, args) + process.emit('uncaughtException', e.error); } + const addEventListener = window.addEventListener.bind(window); + const removeEventListener = window.removeEventListener.bind(window); + window.addEventListener('error', throwUnhandlerError); + window.addEventListener = function ( + ...args: Parameters + ) { + if (args[0] === 'error') userErrorListenerCount++; + return addEventListener.apply(this, args); + }; + window.removeEventListener = function ( + ...args: Parameters + ) { + if (args[0] === 'error' && userErrorListenerCount) userErrorListenerCount--; + return removeEventListener.apply(this, args); + }; return function clearErrorHandlers() { - window.removeEventListener('error', throwUnhandlerError) - } + window.removeEventListener('error', throwUnhandlerError); + }; } -const ALLOWED_KEYS = [ - 'Uint8Array' -] +const AllowedKeys = ['Uint8Array']; -export default ({ +export default { name: 'jsdom', transformMode: 'web', async setupVM({ jsdom = {} }) { @@ -46,26 +46,28 @@ export default ({ console = false, cookieJar = false, ...restOptions - } = jsdom as any - const dom = new JSDOM( - html, - { - pretendToBeVisual, - resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : undefined), - runScripts, - url, - virtualConsole: (console && globalThis.console) ? new VirtualConsole().sendTo(globalThis.console) : undefined, - cookieJar: cookieJar ? new CookieJar() : undefined, - includeNodeLocations, - contentType, - userAgent, - ...restOptions, - }, - ) - const clearWindowErrors = catchWindowErrors(dom.window as any) + } = jsdom as any; + const dom = new JSDOM(html, { + pretendToBeVisual, + resources: + resources ?? + (userAgent ? new ResourceLoader({ userAgent }) : undefined), + runScripts, + url, + virtualConsole: + console && globalThis.console + ? new VirtualConsole().sendTo(globalThis.console) + : undefined, + cookieJar: cookieJar ? new CookieJar() : undefined, + includeNodeLocations, + contentType, + userAgent, + ...restOptions, + }); + const clearWindowErrors = catchWindowErrors(dom.window as any); // TODO: browser doesn't expose Buffer, but a lot of dependencies use it - dom.window.Buffer = Buffer + dom.window.Buffer = Buffer; // inject web globals if they missing in JSDOM but otherwise available in Nodejs // https://nodejs.org/dist/latest/docs/api/globals.html @@ -77,33 +79,29 @@ export default ({ 'BroadcastChannel', 'MessageChannel', 'MessagePort', - ] as const + ] as const; for (const name of globalNames) { - const value = globalThis[name] + const value = globalThis[name]; if ( - typeof value !== 'undefined' - && typeof dom.window[name] === 'undefined' + typeof value !== 'undefined' && + typeof dom.window[name] === 'undefined' ) - dom.window[name] = value + dom.window[name] = value; } return { getVmContext() { - return dom.getInternalVMContext() + return dom.getInternalVMContext(); }, teardown() { - clearWindowErrors() - dom.window.close() + clearWindowErrors(); + dom.window.close(); }, - } + }; }, async setup(global, { jsdom = {} }) { - const { - CookieJar, - JSDOM, - ResourceLoader, - VirtualConsole, - } = await require('jsdom') as typeof import('jsdom') + const { CookieJar, JSDOM, ResourceLoader, VirtualConsole } = + (await require('jsdom')) as typeof import('jsdom'); const { html = '', userAgent, @@ -116,39 +114,43 @@ export default ({ console = false, cookieJar = false, ...restOptions - } = jsdom as any - const dom = new JSDOM( - html, - { - pretendToBeVisual, - resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : undefined), - runScripts, - url, - virtualConsole: (console && global.console) ? new VirtualConsole().sendTo(global.console) : undefined, - cookieJar: cookieJar ? new CookieJar() : undefined, - includeNodeLocations, - contentType, - userAgent, - ...restOptions, - }, - ) + } = jsdom as any; + const dom = new JSDOM(html, { + pretendToBeVisual, + resources: + resources ?? + (userAgent ? new ResourceLoader({ userAgent }) : undefined), + runScripts, + url, + virtualConsole: + console && global.console + ? new VirtualConsole().sendTo(global.console) + : undefined, + cookieJar: cookieJar ? new CookieJar() : undefined, + includeNodeLocations, + contentType, + userAgent, + ...restOptions, + }); - const { keys, originals } = populateGlobal(global, dom.window, { bindFunctions: true }); + const { keys, originals } = populateGlobal(global, dom.window, { + bindFunctions: true, + }); - ALLOWED_KEYS.forEach((key) => { + AllowedKeys.forEach((key) => { delete global[key]; global[key] = originals.get(key); }); - const clearWindowErrors = catchWindowErrors(global) + const clearWindowErrors = catchWindowErrors(global); return { teardown(global) { - clearWindowErrors() - dom.window.close() - keys.forEach(key => delete global[key]) - originals.forEach((v, k) => global[k] = v) + clearWindowErrors(); + dom.window.close(); + keys.forEach((key) => delete global[key]); + originals.forEach((v, k) => (global[k] = v)); }, - } + }; }, -}) \ No newline at end of file +};