Skip to content

Commit

Permalink
fix(browser): set server.open to false and move error handling after …
Browse files Browse the repository at this point in the history
…init (#5845)
  • Loading branch information
sheremet-va authored Jun 5, 2024
1 parent 11a1636 commit 4700367
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
12 changes: 8 additions & 4 deletions packages/browser/src/client/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { getBrowserState } from './utils'

const now = Date.now

interface SpyModule {
spyOn: typeof import('vitest').vi['spyOn']
}

export class VitestBrowserClientMocker {
private queue = new Set<Promise<void>>()
private mocks: Record<string, any> = {}
private factories: Record<string, () => any> = {}

private spyModule!: typeof import('vitest')
private spyModule!: SpyModule

public setSpyModule(mod: typeof import('vitest')) {
public setSpyModule(mod: SpyModule) {
this.spyModule = mod
}

Expand Down Expand Up @@ -187,7 +191,7 @@ export class VitestBrowserClientMocker {
if (isFunction) {
// mock and delegate calls to original prototype method, which should be also mocked already
const original = this[key]
const mock = spyModule.vi.spyOn(this, key as string).mockImplementation(original)
const mock = spyModule.spyOn(this, key as string).mockImplementation(original)
mock.mockRestore = () => {
mock.mockReset()
mock.mockImplementation(original)
Expand All @@ -197,7 +201,7 @@ export class VitestBrowserClientMocker {
}
}
}
const mock = spyModule.vi.spyOn(newContainer, property).mockImplementation(mockFunction)
const mock = spyModule.spyOn(newContainer, property).mockImplementation(mockFunction)
mock.mockRestore = () => {
mock.mockReset()
mock.mockImplementation(mockFunction)
Expand Down
10 changes: 5 additions & 5 deletions packages/browser/src/client/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ async function prepareTestEnvironment(files: string[]) {

const rpc: any = await loadSafeRpc(client)

stopErrorHandler()
registerUnexpectedErrors(rpc)

const providedContext = await client.rpc.getProvidedContext()

const state: WorkerGlobalState = {
Expand Down Expand Up @@ -119,17 +116,20 @@ async function prepareTestEnvironment(files: string[]) {
browserHashMap.set(filename, [true, version])
})

const [runner, { startTests, setupCommonEnv, Vitest }] = await Promise.all([
const [runner, { startTests, setupCommonEnv, Spy }] = await Promise.all([
initiateRunner(config),
importId('vitest/browser') as Promise<typeof import('vitest/browser')>,
])

mocker.setSpyModule(Vitest)
mocker.setSpyModule(Spy)

onCancel.then((reason) => {
runner.onCancel?.(reason)
})

stopErrorHandler()
registerUnexpectedErrors(rpc)

return {
runner,
config,
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export {
getCoverageProvider,
startCoverageInsideWorker,
} from './integrations/coverage'
export * as Vitest from './index'
export * as Spy from './integrations/spy'
3 changes: 2 additions & 1 deletion packages/vitest/src/integrations/browser/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function createBrowserServer(project: WorkspaceProject, configFile:
enforce: 'post',
name: 'vitest:browser:config',
async config(config) {
const server = resolveApiServerConfig(config.test?.browser || {}) || {
const server = resolveApiServerConfig(config.test?.browser || {}, defaultBrowserPort) || {
port: defaultBrowserPort,
}

Expand All @@ -45,6 +45,7 @@ export async function createBrowserServer(project: WorkspaceProject, configFile:
config.server = {
...config.server,
...server,
open: false,
}
config.server.fs ??= {}
config.server.fs.allow = config.server.fs.allow || []
Expand Down
8 changes: 5 additions & 3 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function parseInspector(inspect: string | undefined | boolean | number) {

export function resolveApiServerConfig<Options extends ApiConfig & UserConfig>(
options: Options,
defaultPort: number,
): ApiConfig | undefined {
let api: ApiConfig | undefined

Expand Down Expand Up @@ -407,7 +408,7 @@ export function resolveConfig(
}

// the server has been created, we don't need to override vite.server options
resolved.api = resolveApiServerConfig(options)
resolved.api = resolveApiServerConfig(options, defaultPort)

if (options.related)
resolved.related = toArray(options.related).map(file => resolve(resolved.root, file))
Expand Down Expand Up @@ -535,7 +536,8 @@ export function resolveConfig(
resolved.browser.enabled ??= false
resolved.browser.headless ??= isCI
resolved.browser.isolate ??= true
resolved.browser.ui ??= !isCI
// disable in headless mode by default, and if CI is detected
resolved.browser.ui ??= resolved.browser.headless === false ? true : !isCI

resolved.browser.viewport ??= {} as any
resolved.browser.viewport.width ??= 414
Expand All @@ -544,7 +546,7 @@ export function resolveConfig(
if (resolved.browser.enabled && stdProvider === 'stackblitz')
resolved.browser.provider = 'preview'

resolved.browser.api = resolveApiServerConfig(resolved.browser) || {
resolved.browser.api = resolveApiServerConfig(resolved.browser, defaultBrowserPort) || {
port: defaultBrowserPort,
}

Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { deepMerge, notNullish, removeUndefinedValues, toArray } from '../../uti
import { resolveApiServerConfig } from '../config'
import { Vitest } from '../core'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { defaultPort } from '../../constants'
import { SsrReplacerPlugin } from './ssrReplacer'
import { CSSEnablerPlugin } from './cssEnabler'
import { CoverageTransform } from './coverageTransform'
Expand Down Expand Up @@ -48,7 +49,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
removeUndefinedValues(viteConfig.test ?? {}),
options,
)
testConfig.api = resolveApiServerConfig(testConfig)
testConfig.api = resolveApiServerConfig(testConfig, defaultPort)

// store defines for globalThis to make them
// reassignable when running in worker in src/runtime/setup.ts
Expand Down Expand Up @@ -166,7 +167,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
viteConfigTest,
options,
)
options.api = resolveApiServerConfig(options)
options.api = resolveApiServerConfig(options, defaultPort)

// we replace every "import.meta.env" with "process.env"
// to allow reassigning, so we need to put all envs on process.env
Expand Down

0 comments on commit 4700367

Please sign in to comment.