-
Notifications
You must be signed in to change notification settings - Fork 21
Migrate test runner to vitest browser #1300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
c47a76f
8598b67
3e94b54
92a25ce
fa7b3f5
43934b6
de9d086
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import { fixture, expect, aTimeout, elementUpdated } from '@open-wc/testing'; | ||
| import { html } from 'lit/static-html.js'; | ||
| import { sendKeys } from '@web/test-runner-commands'; | ||
|
|
||
| import type { PharosImageCard } from './pharos-image-card'; | ||
| import type { PharosButton } from '../button/pharos-button'; | ||
|
|
@@ -812,7 +811,6 @@ describe('pharos-image-card', () => { | |
| ); | ||
|
|
||
| component.focus(); | ||
| sendKeys({ press: 'Tab' }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was never actually doing anything meaningful, I removed just this line on |
||
| const checkboxElement = component.renderRoot.querySelector( | ||
| '[data-pharos-component="PharosCheckbox"]' | ||
| ); | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests passed intermittently as they were when I was running them locally, extending the timeout made them less flaky |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "exclude": [ | ||
| "src/pages/**", | ||
| "**/storyArgs.ts", | ||
| "src/**/*.stories.ts", | ||
| "src/**/*.stories.tsx", | ||
| "src/react-components/**", | ||
| "src/utils/_storybook/**" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { defineConfig } from 'vitest/config'; | ||
| import { playwright } from '@vitest/browser-playwright'; | ||
|
|
||
| const silencedLogs = ['Lit is in dev mode.', 'Multiple versions of Lit loaded.']; | ||
|
|
||
| // Pre-existing unhandled rejections that wtr ignored, which we can now see and are silencing until resolved. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add a follow-up issue to address these |
||
| const ignoredUnhandledErrors = [ | ||
| 'is not a valid preset', // image-card | ||
| 'Could not get icon named', // icon | ||
| 'Table must have an accessible name', // table | ||
| 'Axe is already running', // coach-mark | ||
| ]; | ||
|
|
||
| // Stub unused web-test-runner-commands so @open-wc/testing's static import of it doesn't break the tests | ||
| const testRunnerCommandsStub = fileURLToPath( | ||
| new URL('./vitest.stubs/web-test-runner-commands.ts', import.meta.url) | ||
| ); | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| // Icons load via dynamic import, by pre-transforming them, they are available immediately and don't cause random test timeouts. | ||
| warmup: { | ||
| clientFiles: ['./src/styles/icons/*.ts'], | ||
| }, | ||
| }, | ||
| resolve: { | ||
| alias: { | ||
| '@web/test-runner-commands': testRunnerCommandsStub, | ||
| }, | ||
| }, | ||
| test: { | ||
| include: ['src/**/*.test.ts'], | ||
| globals: true, | ||
| setupFiles: ['./vitest.setup.ts'], | ||
| onConsoleLog(log) { | ||
| if (silencedLogs.some((l) => log.includes(l))) return false; | ||
| }, | ||
| onUnhandledError(error) { | ||
| if (ignoredUnhandledErrors.some((m) => error.message?.includes(m))) return false; | ||
| }, | ||
| coverage: { | ||
| // istanbul is the only provider that supports coverage while running against multiple browsers. | ||
| provider: 'istanbul', | ||
| include: ['src/**/*.ts'], | ||
| exclude: [ | ||
| 'src/**/*.test.ts', | ||
| 'src/test/**', | ||
| 'src/**/*.stories.*', | ||
| 'src/**/storyArgs.ts', | ||
| 'src/styles/**', | ||
| 'src/react-components/**', | ||
| 'src/pages/**', | ||
| '**/*.css.ts', | ||
| '**/*.tsx', | ||
| 'src/utils/_storybook/**', | ||
| ], | ||
| thresholds: { | ||
| statements: 94, | ||
| branches: 85, | ||
| functions: 96, | ||
| lines: 94, | ||
| }, | ||
| }, | ||
| browser: { | ||
| enabled: true, | ||
| headless: true, | ||
| provider: playwright(), | ||
| screenshotFailures: false, | ||
| viewport: { width: 1280, height: 720 }, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match wtr's full-page size, Vitest iframe default is 414x896 |
||
| instances: [{ browser: 'chromium' }, { browser: 'firefox' }, { browser: 'webkit' }], | ||
| }, | ||
| }, | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // Polyfill for custom-element definitions. | ||
| import '@webcomponents/scoped-custom-element-registry'; | ||
|
|
||
| // Register every Pharos component under the `test-` prefix | ||
| import './src/test/initComponents'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Stub for `@web/test-runner-commands` needed under Vitest browser mode. | ||
| // | ||
| // `@open-wc/semantic-dom-diff` (pulled in transitively by `@open-wc/testing`'s | ||
| // `expect`) statically imports `@web/test-runner-commands`, which itself imports | ||
| // a wtr-only websocket module (`__web-dev-server__web-socket.js`) that does not | ||
| // exist under Vite — breaking the whole `@open-wc/testing` import chain even | ||
| // though our tests never uses snapshot/dom-diff/command assertions. | ||
| // | ||
| // These exports mirror `@web/test-runner-commands` so the import resolves; each | ||
| // throws only if actually invoked. Aliased in `vitest.config.ts` so Vite's | ||
| // dep optimizer (which pre-bundles `@open-wc/testing`) picks it up. | ||
|
|
||
| const unavailable = | ||
| (name: string) => | ||
| (..._args: unknown[]): never => { | ||
| throw new Error(`@web/test-runner-commands.${name} is not available under Vitest browser mode`); | ||
| }; | ||
|
|
||
| export const executeServerCommand = unavailable('executeServerCommand'); | ||
| export const setViewport = unavailable('setViewport'); | ||
| export const emulateMedia = unavailable('emulateMedia'); | ||
| export const setUserAgent = unavailable('setUserAgent'); | ||
| export const sendKeys = unavailable('sendKeys'); | ||
| export const selectOption = unavailable('selectOption'); | ||
| export const sendMouse = unavailable('sendMouse'); | ||
| export const resetMouse = unavailable('resetMouse'); | ||
| export const a11ySnapshot = unavailable('a11ySnapshot'); | ||
| export const writeFile = unavailable('writeFile'); | ||
| export const readFile = unavailable('readFile'); | ||
| export const removeFile = unavailable('removeFile'); | ||
| export const findAccessibilityNode = unavailable('findAccessibilityNode'); | ||
| export const getSnapshotConfig = unavailable('getSnapshotConfig'); | ||
| export const getSnapshots = unavailable('getSnapshots'); | ||
| export const getSnapshot = unavailable('getSnapshot'); | ||
| export const saveSnapshot = unavailable('saveSnapshot'); | ||
| export const removeSnapshot = unavailable('removeSnapshot'); | ||
| export const compareSnapshot = unavailable('compareSnapshot'); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the difference in how vitest compiles the component for testing, there were some whitespace changes, which resulted in errors like this
I just added
.trim()where this was happening