From dcb26a8ef5c62bf6acbf8130895c37f56e8a9d3f Mon Sep 17 00:00:00 2001 From: Cashew <52880648+Scrub1492@users.noreply.github.com> Date: Sun, 14 Jan 2024 10:51:00 +0700 Subject: more test updates (#510) * test updates * remove duplicate * fix indentation * fix --- test/fixtures/dom-test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/fixtures') diff --git a/test/fixtures/dom-test.js b/test/fixtures/dom-test.js index 8cfe80a9..e0e7d647 100644 --- a/test/fixtures/dom-test.js +++ b/test/fixtures/dom-test.js @@ -35,6 +35,20 @@ function prepareWindow(window) { document.caretRangeFromPoint = () => null; } +/** + * + * @param {string} [htmlFilePath] + * @returns {Promise<{window: import('jsdom').DOMWindow; teardown: (global: unknown) => import('vitest').Awaitable}>} + */ +export async function setupDomTest(htmlFilePath) { + const html = typeof htmlFilePath === 'string' ? fs.readFileSync(htmlFilePath, {encoding: 'utf8'}) : ''; + const env = builtinEnvironments.jsdom; + const {teardown} = await env.setup(global, {jsdom: {html}}); + const window = /** @type {import('jsdom').DOMWindow} */ (/** @type {unknown} */ (global.window)); + prepareWindow(window); + return {window, teardown}; +} + /** * @param {string} [htmlFilePath] * @returns {import('vitest').TestAPI<{window: import('jsdom').DOMWindow}>} -- cgit v1.2.3