Skip to content
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

feat(browser): support "none" provider and update lit example to use it #4427

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/lit/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@vitest/example-lit",
"type": "module",
"private": true,
"files": [
"dist",
Expand All @@ -12,13 +13,15 @@
"test:ui": "vitest --ui"
},
"dependencies": {
"lit": "^2.2.5"
"lit": "^3.0.1"
},
"devDependencies": {
"@vitest/browser": "latest",
"@vitest/ui": "latest",
"jsdom": "latest",
"vite": "latest",
"vitest": "latest"
"vitest": "latest",
"webdriverio": "^8.21.0"
},
"stackblitz": {
"startCommand": "npm run test:ui"
Expand Down
12 changes: 2 additions & 10 deletions examples/lit/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

import '../src/my-button'
import '../src/my-button.js'

describe('Button with increment', async () => {
function getInsideButton(): HTMLElement | null | undefined {
return document.body.querySelector('my-button')?.shadowRoot?.querySelector('button')
}

beforeEach(async () => {
beforeEach(() => {
document.body.innerHTML = '<my-button name="World"></my-button>'
await new Promise<void>((resolve) => {
const interval = setInterval(() => {
if (getInsideButton()) {
clearInterval(interval)
resolve()
}
})
})
})

it('should increment the count on each click', () => {
Expand Down
5 changes: 4 additions & 1 deletion examples/lit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"compilerOptions": {
"module": "node16",
"target": "es2020",
"moduleResolution": "Node16",
"experimentalDecorators": true,
"useDefineForClassFields": false,
"importsNotUsedAsValues": "preserve"
"verbatimModuleSyntax": true
}
}
6 changes: 4 additions & 2 deletions examples/lit/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { defineConfig } from 'vite'
// https://vitejs.dev/config/
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
browser: {
enabled: true,
name: 'chrome',
},
},
})
3 changes: 2 additions & 1 deletion packages/browser/providers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import type { BrowserProvider } from 'vitest/nide'

declare var webdriverio: BrowserProvider
declare var playwright: BrowserProvider
declare var none: BrowserProvider

export { webdriverio, playwright }
export { webdriverio, playwright, none }
2 changes: 2 additions & 0 deletions packages/browser/src/node/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PlaywrightBrowserProvider } from './playwright'
import { WebdriverBrowserProvider } from './webdriver'
import { NoneBrowserProvider } from './none'

export const webdriverio = WebdriverBrowserProvider
export const playwright = PlaywrightBrowserProvider
export const none = NoneBrowserProvider
40 changes: 40 additions & 0 deletions packages/browser/src/node/providers/none.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Awaitable } from 'vitest'
import type { BrowserProvider, WorkspaceProject } from 'vitest/node'

export class NoneBrowserProvider implements BrowserProvider {
public name = 'none'
private ctx!: WorkspaceProject
private open = false

getSupportedBrowsers() {
// `none` is not restricted to certain browsers.
return []
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
}

isOpen() {
return this.open
}

async initialize(ctx: WorkspaceProject) {
this.ctx = ctx
this.open = false
}

catchError(_cb: (error: Error) => Awaitable<void>) {
return () => {}
}

async openPage(_url: string) {
this.open = true
if (!this.ctx.browser)
throw new Error('Browser is not initialized')
const options = this.ctx.browser.config.server
const _open = options.open
options.open = _url
this.ctx.browser.openBrowser()
options.open = _open
}

async close() {
}
}
7 changes: 5 additions & 2 deletions packages/vitest/src/integrations/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ interface Loader {
executeId: (id: string) => any
}

const builtinProviders = ['webdriverio', 'playwright', 'none']

export async function getBrowserProvider(options: ResolvedBrowserOptions, loader: Loader): Promise<BrowserProviderModule> {
if (options.provider == null || options.provider === 'webdriverio' || options.provider === 'playwright') {
if (options.provider == null || builtinProviders.includes(options.provider)) {
await ensurePackageInstalled('@vitest/browser', loader.root)
const providers = await loader.executeId('@vitest/browser/providers') as {
webdriverio: BrowserProviderModule
playwright: BrowserProviderModule
none: BrowserProviderModule
}
const provider = (options.provider || 'webdriverio') as 'webdriverio' | 'playwright'
const provider = (options.provider || 'webdriverio') as 'webdriverio' | 'playwright' | 'none'
return providers[provider]
}

Expand Down
5 changes: 4 additions & 1 deletion packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ResolvedConfig as ResolvedViteConfig } from 'vite'
import type { ApiConfig, ResolvedConfig, UserConfig, VitestRunMode } from '../types'
import { defaultBrowserPort, defaultPort } from '../constants'
import { benchmarkConfigDefaults, configDefaults } from '../defaults'
import { isCI, toArray } from '../utils'
import { isCI, stdProvider, toArray } from '../utils'
import { VitestCache } from './cache'
import { BaseSequencer } from './sequencers/BaseSequencer'
import { RandomSequencer } from './sequencers/RandomSequencer'
Expand Down Expand Up @@ -380,6 +380,9 @@ export function resolveConfig(
resolved.browser.slowHijackESM ??= true
resolved.browser.isolate ??= true

if (resolved.browser.enabled && stdProvider === 'stackblitz')
resolved.browser.provider = 'none'
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

resolved.browser.api = resolveApiServerConfig(resolved.browser) || {
port: defaultBrowserPort,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class WorkspaceProject {
const supportedBrowsers = this.browserProvider.getSupportedBrowsers()
if (!browser)
throw new Error(`[${this.getName()}] Browser name is required. Please, set \`test.browser.name\` option manually.`)
if (!supportedBrowsers.includes(browser))
if (supportedBrowsers.length && !supportedBrowsers.includes(browser))
throw new Error(`[${this.getName()}] Browser "${browser}" is not supported by the browser provider "${this.browserProvider.name}". Supported browsers: ${supportedBrowsers.join(', ')}.`)
const providerOptions = this.config.browser.providerOptions
await this.browserProvider.initialize(this, { browser, options: providerOptions })
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface BrowserConfigOptions {
*
* @default 'webdriverio'
*/
provider?: 'webdriverio' | 'playwright' | (string & {})
provider?: 'webdriverio' | 'playwright' | 'none' | (string & {})

/**
* Options that are passed down to a browser provider.
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const isNode: boolean = typeof process < 'u' && typeof process.stdout < 'u' && !process.versions?.deno && !globalThis.window
export const isBrowser: boolean = typeof window !== 'undefined'
export { isCI } from 'std-env'
export { isCI, provider as stdProvider } from 'std-env'
Loading
Loading