SolidJS
SolidJSは、シンプルで高性能なリアクティブ性を使用してユーザーインターフェースを構築するためのフレームワークです。WebdriverIOとそのブラウザーランナーを使用することで、SolidJSコンポーネントを実際のブラウザで直接テストできます。
設定
SolidJSプロジェクトにWebdriverIOを設定するには、コンポーネントテストドキュメントの手順に従ってください。ランナーオプションでプリセットとしてsolid
を選択してください。例:
// wdio.conf.js
export const config = {
// ...
runner: ['browser', {
preset: 'solid'
}],
// ...
}
情報
SolidJSプリセットには、vite-plugin-solid
のインストールが必要です。
- npm
- Yarn
- pnpm
npm install --save-dev vite-plugin-solid
yarn add --dev vite-plugin-solid
pnpm add --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/web
のrender
メソッドを使用して、コンポーネントをテストページにアタッチします。コンポーネントと対話するには、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コンポーネントテストスイートの完全な例は、サンプルリポジトリにあります。