本文へ移動

SolidJS

SolidJSは、シンプルで高性能なリアクティブ性を使用してユーザーインターフェースを構築するためのフレームワークです。WebdriverIOとそのブラウザーランナーを使用することで、SolidJSコンポーネントを実際のブラウザで直接テストできます。

設定

SolidJSプロジェクトにWebdriverIOを設定するには、コンポーネントテストドキュメントの手順に従ってください。ランナーオプションでプリセットとしてsolidを選択してください。例:

// wdio.conf.js
export const config = {
// ...
runner: ['browser', {
preset: 'solid'
}],
// ...
}
情報

開発サーバーとしてViteを既に使用している場合は、WebdriverIOの設定でvite.config.ts内の設定を再利用することもできます。詳細については、ランナーオプションviteConfigを参照してください。

SolidJSプリセットには、vite-plugin-solidのインストールが必要です。

npm install --save-dev vite-plugin-solid

テストを開始するには、以下を実行します。

npx wdio run ./wdio.conf.js

テストの記述

次のSolidJSコンポーネントがあるとします。

./components/Component.tsx
import { createSignal } from 'solid-js'

function App() {
const [theme, setTheme] = createSignal('light')

const toggleTheme = () => {
const nextTheme = theme() === 'light' ? 'dark' : 'light'
setTheme(nextTheme)
}

return <button onClick={toggleTheme}>
Current theme: {theme()}
</button>
}

export default App

テストでは、solid-js/webrenderメソッドを使用して、コンポーネントをテストページにアタッチします。コンポーネントと対話するには、WebdriverIOコマンドの使用をお勧めします。これは、実際のユーザー操作により近い動作をするためです。例:

app.test.tsx
import { expect } from '@wdio/globals'
import { render } from 'solid-js/web'

import App from './components/Component.jsx'

describe('Solid Component Testing', () => {
/**
* ensure we render the component for every test in a
* new root container
*/
let root: Element
beforeEach(() => {
if (root) {
root.remove()
}

root = document.createElement('div')
document.body.appendChild(root)
})

it('Test theme button toggle', async () => {
render(<App />, root)
const buttonEl = await $('button')

await buttonEl.click()
expect(buttonEl).toContainHTML('dark')
})
})

SolidJS用のWebdriverIOコンポーネントテストスイートの完全な例は、サンプルリポジトリにあります。

ようこそ!お手伝いできることはありますか?

WebdriverIO AI Copilot