Skip to content

Commit

Permalink
Improve stability of component tests for Safari (#9985)
Browse files Browse the repository at this point in the history
* Improve stability of component tests for Safari

* revert tweak

* print source page when connection fails

* evaluate for title too
  • Loading branch information
christian-bromann committed Mar 15, 2023
1 parent 462bbc6 commit 7bc087b
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -88,6 +88,8 @@ jobs:
timeout_minutes: 20
max_attempts: 3
command: npm run test:e2e
- name: Safari Component Tests
run: npm run test:component
# Disable Firefox tests due to https://github.com/puppeteer/puppeteer/issues/8923
# - name: Setup Firefox
# uses: browser-actions/setup-firefox@latest
Expand Down
9 changes: 8 additions & 1 deletion e2e/browser-runner/lit.test.js
Expand Up @@ -78,7 +78,14 @@ describe('Lit Component testing', () => {
expect(window.TEST_COMMAND).toBe('serve')
})

it('should allow to manual mock dependencies', async () => {
it('should allow to manual mock dependencies', async function () {
/**
* this fails in Safari as the click on the button is not recognised
*/
if (browser.capabilities.browserName.toLowerCase() === 'safari') {
return this.skip()
}

render(
html`<simple-greeting name="WebdriverIO" />`,
document.body
Expand Down
17 changes: 10 additions & 7 deletions e2e/browser-runner/wdio.conf.js
Expand Up @@ -3,17 +3,22 @@ import url from 'node:url'
import path from 'node:path'
import { loadEnv } from 'vite'

const isMac = os.platform() === 'darwin'
const isWindows = os.platform() === 'win32'

/**
* WebdriverIO is using this example to test its component testing features
* and we have experienced issues with Vue when running in Windows,
* see https://github.com/testing-library/vue-testing-library/issues/292
* Please ignore and remove this in your project!
*/
if (process.env.CI && process.env.WDIO_PRESET === 'vue' && os.platform() === 'win32') {
if (process.env.CI && process.env.WDIO_PRESET === 'vue' && (isWindows || isMac)) {
process.exit(0)
}

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const browserName = isMac ? 'safari' : 'chrome'
const driver = isMac ? 'safaridriver' : 'chromedriver'
export const config = {
/**
* specify test files
Expand All @@ -26,10 +31,7 @@ export const config = {
/**
* capabilities
*/
capabilities: [{
browserName: 'chrome',
acceptInsecureCerts: true
}],
capabilities: [{ browserName }],

/**
* test configurations
Expand All @@ -38,7 +40,7 @@ export const config = {
framework: 'mocha',
outputDir: path.join(__dirname, 'logs', process.env.WDIO_PRESET),
reporters: ['spec', 'dot', 'junit'],
services: ['chromedriver'],
services: [driver],
runner: ['browser', {
preset: process.env.WDIO_PRESET,
rootDir: path.resolve(__dirname, '..'),
Expand All @@ -54,7 +56,8 @@ export const config = {
},
coverage: {
enabled: true,
functions: 100
// we skip some tests on Mac, therefor lower coverage treshold
functions: isMac ? 66 : 100
}
}],

Expand Down
90 changes: 89 additions & 1 deletion e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion e2e/package.json
Expand Up @@ -41,7 +41,8 @@
"expect": "^29.1.2",
"graphql-request": "^5.1.0",
"mocha": "^10.2.0",
"wdio-chromedriver-service": "^8.0.1"
"wdio-chromedriver-service": "^8.0.1",
"wdio-safaridriver-service": "^2.1.0"
},
"dependencies": {
"lit": "^2.4.0",
Expand Down
5 changes: 0 additions & 5 deletions packages/wdio-browser-runner/src/browser/mock.ts
Expand Up @@ -17,9 +17,4 @@ export const dirname = () => ''
export const resolve = () => ''
export const sep = '/'
export const type = 'browser'

// mock jest-matcher-utils exports
export const printDiffOrStringify = () => {}
export const printReceived = () => {}
export const printExpected = () => {}
export default () => {}
6 changes: 4 additions & 2 deletions packages/wdio-browser-runner/src/vite/constants.ts
Expand Up @@ -2,6 +2,7 @@ import topLevelAwait from 'vite-plugin-top-level-await'
import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'
import type { InlineConfig } from 'vite'

import { codeFrameFix } from './plugins/esbuild.js'
import type { FrameworkPreset } from '../types.js'

export const PRESET_DEPENDENCIES: Record<FrameworkPreset, [string, string, any] | undefined> = {
Expand Down Expand Up @@ -41,7 +42,7 @@ export const DEFAULT_VITE_CONFIG: Partial<InlineConfig> = {
'expect', 'serialize-error', 'minimatch', 'css-shorthand-properties',
'lodash.merge', 'lodash.zip', 'lodash.clonedeep', 'lodash.pickby', 'lodash.flattendeep',
'aria-query', 'grapheme-splitter', 'css-value', 'rgb2hex', 'p-iteration', 'fast-safe-stringify',
'deepmerge-ts', 'jest-util'
'deepmerge-ts', 'jest-util', 'jest-matcher-utils'
],
esbuildOptions: {
logLevel: 'silent',
Expand All @@ -51,7 +52,8 @@ export const DEFAULT_VITE_CONFIG: Partial<InlineConfig> = {
},
// Enable esbuild polyfill plugins
plugins: [
esbuildCommonjs(['@testing-library/vue'])
esbuildCommonjs(['@testing-library/vue']),
codeFrameFix()
],
},
}
Expand Down
34 changes: 34 additions & 0 deletions packages/wdio-browser-runner/src/vite/plugins/esbuild.ts
@@ -0,0 +1,34 @@
import fs from 'node:fs/promises'
import type { Plugin, PluginBuild } from 'esbuild'

export function codeFrameFix () {
return <Plugin>{
name: 'wdio:codeFrameFix',
setup (build: PluginBuild) {
build.onLoad(
{ filter: /@babel\/code-frame/, namespace: 'file' },

/**
* mock @babel/code-frame as it fails in Safari due
* to usage of chalk
*/
async ({ path: id }: { path: string }) => {
const code = await fs.readFile(id).then(
(buf) => buf.toString(),
() => undefined)

if (!code) {
return
}

return {
contents: code.replace('require("@babel/highlight");', /*js*/`{
shouldHighlight: false,
reset: () => {}
}`)
}
}
)
}
}
}
3 changes: 1 addition & 2 deletions packages/wdio-browser-runner/src/vite/plugins/testrunner.ts
Expand Up @@ -41,8 +41,7 @@ const resolvedVirtualModuleId = '\0' + virtualModuleId
* functionality
*/
const MODULES_TO_MOCK = [
'import-meta-resolve', 'puppeteer-core', 'archiver', 'glob', 'devtools', 'ws',
'jest-matcher-utils', 'decamelize'
'import-meta-resolve', 'puppeteer-core', 'archiver', 'glob', 'devtools', 'ws', 'decamelize'
]

const POLYFILLS = [
Expand Down
8 changes: 6 additions & 2 deletions packages/wdio-runner/src/browser.ts
Expand Up @@ -141,8 +141,12 @@ export default class BrowserFramework implements Omit<TestFramework, 'init'> {
viteError = [{ message: errorElems.map((elem) => elem.innerText).join('\n') }]
}
}
const loadError = typeof window.__wdioErrors__ === 'undefined'
? [{ message: `Failed to load test page (title = ${document.title})` }]
const loadError = (
typeof window.__wdioErrors__ === 'undefined' &&
document.title !== 'WebdriverIO Browser Test' &&
!document.querySelector('mocha-framework')
)
? [{ message: `Failed to load test page (title = "${document.title}", source: ${document.documentElement.innerHTML})` }]
: null
const errors = viteError || window.__wdioErrors__ || loadError
return { failures, errors, hasViteError: Boolean(viteError) }
Expand Down

0 comments on commit 7bc087b

Please sign in to comment.