Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx committed Oct 12, 2023
1 parent c8eb684 commit 55d4da6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 83 deletions.
4 changes: 2 additions & 2 deletions src/document/presence/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export enum PresenceChangeType {
* `PresenceChange` represents the change of presence.
*/
export type PresenceChange<P extends Indexable> =
| { 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.
Expand Down
164 changes: 83 additions & 81 deletions test/vitest/env/index.ts
Original file line number Diff line number Diff line change
@@ -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<typeof addEventListener>) {
if (args[0] === 'error')
userErrorListenerCount++
return addEventListener.apply(this, args)
}
window.removeEventListener = function (...args: Parameters<typeof removeEventListener>) {
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<typeof addEventListener>
) {
if (args[0] === 'error') userErrorListenerCount++;
return addEventListener.apply(this, args);
};
window.removeEventListener = function (
...args: Parameters<typeof removeEventListener>
) {
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 <Environment>({
export default <Environment>{
name: 'jsdom',
transformMode: 'web',
async setupVM({ jsdom = {} }) {
Expand All @@ -46,26 +46,28 @@ export default <Environment>({
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
Expand All @@ -77,33 +79,29 @@ export default <Environment>({
'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 = '<!DOCTYPE html>',
userAgent,
Expand All @@ -116,39 +114,43 @@ export default <Environment>({
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));
},
}
};
},
})
};

0 comments on commit 55d4da6

Please sign in to comment.