Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Sep 8, 2023
1 parent 3635486 commit ccf618d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
16 changes: 3 additions & 13 deletions packages/vitest/src/node/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { resolve } from 'pathe'
import { createServer, mergeConfig } from 'vite'
import { mergeConfig } from 'vite'
import type { InlineConfig as ViteInlineConfig, UserConfig as ViteUserConfig } from 'vite'
import { findUp } from 'find-up'
import type { UserConfig, VitestRunMode } from '../types'
import { configFiles } from '../constants'
import { Vitest } from './core'
import { VitestPlugin } from './plugins'
import { createViteServer } from './vite'

export async function createVitest(mode: VitestRunMode, options: UserConfig, viteOverrides: ViteUserConfig = {}) {
const ctx = new Vitest(mode)
Expand All @@ -27,21 +28,10 @@ export async function createVitest(mode: VitestRunMode, options: UserConfig, vit
plugins: await VitestPlugin(options, ctx),
}

// Vite prints an error (https://github.com/vitejs/vite/issues/14328)
// But Vitest works correctly either way
const error = console.error
console.error = (...args: any[]) => {
if (typeof args[0] === 'string' && args[0].includes('WebSocket server error:'))
return
error(...args)
}

const server = await createServer(mergeConfig(config, mergeConfig(viteOverrides, { root: options.root })))
const server = await createViteServer(mergeConfig(config, mergeConfig(viteOverrides, { root: options.root })))

if (ctx.config.api?.port)
await server.listen()

console.error = error

return ctx
}
21 changes: 21 additions & 0 deletions packages/vitest/src/node/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { InlineConfig } from 'vite'
import { createServer } from 'vite'

export async function createViteServer(inlineConfig: InlineConfig) {
// Vite prints an error (https://github.com/vitejs/vite/issues/14328)
// But Vitest works correctly either way
const error = console.error
console.error = (...args: any[]) => {
if (typeof args[0] === 'string' && args[0].includes('WebSocket server error:'))
return
error(...args)
}

const server = await createServer({
logLevel: 'error',
...inlineConfig,
})

console.error = error
return server
}
4 changes: 2 additions & 2 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { promises as fs } from 'node:fs'
import fg from 'fast-glob'
import mm from 'micromatch'
import { dirname, relative, resolve, toNamespacedPath } from 'pathe'
import { createServer } from 'vite'
import type { ViteDevServer, InlineConfig as ViteInlineConfig } from 'vite'
import { ViteNodeRunner } from 'vite-node/client'
import { ViteNodeServer } from 'vite-node/server'
Expand All @@ -14,6 +13,7 @@ import type { BrowserProvider } from '../types/browser'
import { getBrowserProvider } from '../integrations/browser'
import { isBrowserEnabled, resolveConfig } from './config'
import { WorkspaceVitestPlugin } from './plugins/workspace'
import { createViteServer } from './vite'

interface InitializeProjectOptions extends UserWorkspaceConfig {
workspaceConfigPath: string
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function initializeProject(workspacePath: string | number, ctx: Vit
],
}

await createServer(config)
await createViteServer(config)

return project
}
Expand Down

0 comments on commit ccf618d

Please sign in to comment.