From 8a2a76cfb4cc742bbe10794b31742d24b7d037a9 Mon Sep 17 00:00:00 2001 From: Shai Reznik Date: Thu, 1 Feb 2024 12:23:47 +0200 Subject: [PATCH] updated the code to TS --- package.json | 1 + packages/playwright-ct-qwik/project.json | 1 + packages/playwright-ct-qwik/src/cli.ts | 7 ++- packages/playwright-ct-qwik/src/hooks.d.ts | 28 ------------ packages/playwright-ct-qwik/src/hooks.ts | 23 +++++++--- packages/playwright-ct-qwik/src/index.ts | 45 +++++-------------- .../playwright-ct-qwik/src/register-source.ts | 15 ++++--- packages/playwright-ct-qwik/src/register.ts | 22 --------- packages/playwright-ct-qwik/tsconfig.lib.json | 2 +- 9 files changed, 46 insertions(+), 98 deletions(-) delete mode 100644 packages/playwright-ct-qwik/src/hooks.d.ts delete mode 100644 packages/playwright-ct-qwik/src/register.ts diff --git a/package.json b/package.json index 2339df2..c6bd7e4 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ }, "private": false, "scripts": { + "build": "nx build playwright-ct-qwik", "commit": "git-cz", "format:fix": "pretty-quick --staged", "prepare": "husky install" diff --git a/packages/playwright-ct-qwik/project.json b/packages/playwright-ct-qwik/project.json index a82cb3a..3869e33 100644 --- a/packages/playwright-ct-qwik/project.json +++ b/packages/playwright-ct-qwik/project.json @@ -6,6 +6,7 @@ "targets": { "build-mount": { "executor": "@nx/js:tsc", + "inputs": ["{projectRoot}/src/**/*"], "outputs": ["{options.outputPath}"], "options": { "outputPath": "dist/packages/playwright-ct-qwik", diff --git a/packages/playwright-ct-qwik/src/cli.ts b/packages/playwright-ct-qwik/src/cli.ts index db414c5..e0059cf 100644 --- a/packages/playwright-ct-qwik/src/cli.ts +++ b/packages/playwright-ct-qwik/src/cli.ts @@ -15,5 +15,8 @@ * limitations under the License. */ -const { program } = require('@playwright/experimental-ct-core/lib/program'); -program.parse(process.argv); +// import { program } from '@playwright/experimental-ct-core/lib/program'; + +// program.parse(process.argv); + +module.exports = require('@playwright/experimental-ct-core/cli'); diff --git a/packages/playwright-ct-qwik/src/hooks.d.ts b/packages/playwright-ct-qwik/src/hooks.d.ts deleted file mode 100644 index 1f92fdb..0000000 --- a/packages/playwright-ct-qwik/src/hooks.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; -import { JSXElement } from 'solid-js'; - -export declare function beforeMount( - callback: (params: { - hooksConfig?: HooksConfig; - App: () => JSXElement; - }) => Promise -): void; -export declare function afterMount( - callback: (params: { hooksConfig?: HooksConfig }) => Promise -): void; diff --git a/packages/playwright-ct-qwik/src/hooks.ts b/packages/playwright-ct-qwik/src/hooks.ts index 1037f6c..4747d5f 100644 --- a/packages/playwright-ct-qwik/src/hooks.ts +++ b/packages/playwright-ct-qwik/src/hooks.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Copyright (c) Microsoft Corporation. * @@ -14,16 +15,26 @@ * limitations under the License. */ -const __pw_hooks_before_mount = []; -const __pw_hooks_after_mount = []; +import type { JSXOutput } from '@builder.io/qwik'; +import type { JsonObject } from '@playwright/experimental-ct-core/types/component'; + +const __pw_hooks_before_mount: any[] = []; +const __pw_hooks_after_mount: any[] = []; window.__pw_hooks_before_mount = __pw_hooks_before_mount; window.__pw_hooks_after_mount = __pw_hooks_after_mount; -export const beforeMount = (callback) => { +export function beforeMount( + callback: (params: { + hooksConfig?: HooksConfig; + App: () => JSXOutput; + }) => Promise, +): void { __pw_hooks_before_mount.push(callback); -}; +} -export const afterMount = (callback) => { +export function afterMount( + callback: (params: { hooksConfig?: HooksConfig }) => Promise, +): void { __pw_hooks_after_mount.push(callback); -}; +} diff --git a/packages/playwright-ct-qwik/src/index.ts b/packages/playwright-ct-qwik/src/index.ts index 272adbd..5b54ba2 100644 --- a/packages/playwright-ct-qwik/src/index.ts +++ b/packages/playwright-ct-qwik/src/index.ts @@ -1,33 +1,4 @@ /* eslint-disable @typescript-eslint/no-var-requires */ -// /* eslint-disable @typescript-eslint/no-var-requires */ -// import { -// test as baseTest, -// devices, -// expect, -// defineConfig as originalDefineConfig, -// } from '@playwright/test'; -// import path from 'path'; - -// const { fixtures } = require('@playwright/experimental-ct-core/lib/mount'); - -// process.env['NODE_ENV'] = 'test'; - -// function plugin() { -// const { -// createPlugin, -// } = require('@playwright/experimental-ct-core/lib/vitePlugin'); -// return createPlugin(path.join(__dirname, 'registerSource.mjs'), () => { -// // import('').then((m) => m.default) -// }); -// } - -// const test = baseTest.extend(fixtures); -// const defineConfig = (config: any) => -// originalDefineConfig({ ...config, _plugins: [plugin] }); - -// export { test, expect, devices, defineConfig }; - -// ---------- NEW STUFF --------- /** * Copyright (c) Microsoft Corporation. @@ -45,17 +16,23 @@ * limitations under the License. */ -const { test, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/experimental-ct-core'); +const { + test, + expect, + devices, + defineConfig: originalDefineConfig, +} = require('@playwright/experimental-ct-core'); import path from 'path'; const plugin = async () => { // Only fetch upon request to avoid resolution in workers. const { createPlugin } = require('@playwright/experimental-ct-core/plugin'); - return createPlugin( - path.join(__dirname, 'register-source.mjs'), - () => import('@builder.io/qwik/optimizer').then(plugin => plugin.qwikVite())); + return createPlugin(path.join(__dirname, 'register-source.mjs'), () => + import('@builder.io/qwik/optimizer').then((plugin) => plugin.qwikVite()), + ); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any -const defineConfig = (config: any, ...configs: any[]) => originalDefineConfig({ ...config, _plugins: [plugin] }, ...configs); +const defineConfig = (config: any, ...configs: any[]) => + originalDefineConfig({ ...config, _plugins: [plugin] }, ...configs); module.exports = { test, expect, devices, defineConfig }; diff --git a/packages/playwright-ct-qwik/src/register-source.ts b/packages/playwright-ct-qwik/src/register-source.ts index c4a176b..6933d6e 100644 --- a/packages/playwright-ct-qwik/src/register-source.ts +++ b/packages/playwright-ct-qwik/src/register-source.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Copyright (c) Microsoft Corporation. * @@ -19,6 +20,7 @@ import { JSXOutput, render } from '@builder.io/qwik'; import { QWIK_LOADER } from '@builder.io/qwik/loader'; +import { Component } from '@playwright/experimental-ct-core/types/component'; /** * @param {any} component * @returns {component is JsxComponent} @@ -39,14 +41,14 @@ addQwikLoader(); const __pwUnmountKey = Symbol('unmountKey'); window.playwrightMount = async ( - component: JSXOutput, + component: Component, rootElement: HTMLElement, hooksConfig: unknown, ) => { // if (!isJsxComponent(component)) // throw new Error('Object mount notation is not supported'); - let componentToRender = component; + let componentToRender = component as JSXOutput; for (const hook of window['__pw_hooks_before_mount'] || []) { const wrapper = await hook({ component, hooksConfig }); if (wrapper) { @@ -56,20 +58,23 @@ window.playwrightMount = async ( const { cleanup } = await render(rootElement, componentToRender); - rootElement[__pwUnmountKey] = cleanup; + (rootElement as any)[__pwUnmountKey] = cleanup; for (const hook of window['__pw_hooks_after_mount'] || []) await hook({ hooksConfig }); }; window.playwrightUnmount = async (rootElement) => { - const cleanup = rootElement[__pwcleanupKey]; + const cleanup = (rootElement as any)[__pwUnmountKey]; if (!cleanup) throw new Error('Component was not mounted'); cleanup(); }; -window.playwrightUpdate = async (rootElement, component) => { +window.playwrightUpdate = async ( + rootElement: HTMLElement, + component: Component, +) => { // if (!isJsxComponent(component)) // throw new Error('Object mount notation is not supported'); diff --git a/packages/playwright-ct-qwik/src/register.ts b/packages/playwright-ct-qwik/src/register.ts deleted file mode 100644 index 19dd060..0000000 --- a/packages/playwright-ct-qwik/src/register.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { pwRegister } from './register-source'; - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export default (components: Record): void => { - pwRegister(components); -}; diff --git a/packages/playwright-ct-qwik/tsconfig.lib.json b/packages/playwright-ct-qwik/tsconfig.lib.json index 5b66fae..bfb25a0 100644 --- a/packages/playwright-ct-qwik/tsconfig.lib.json +++ b/packages/playwright-ct-qwik/tsconfig.lib.json @@ -9,6 +9,6 @@ "types": ["node"], "importHelpers": false }, - "include": ["src/index.ts"], + "include": ["src/*.ts"], "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"] }