Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream' into chore/action-checkout/up…
Browse files Browse the repository at this point in the history
…grade
  • Loading branch information
Dunqing committed Sep 8, 2023
2 parents a5b2f3c + b5bf329 commit 070bc9e
Show file tree
Hide file tree
Showing 9 changed files with 2,755 additions and 530 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.34.3",
"private": true,
"packageManager": "pnpm@8.6.6",
"packageManager": "pnpm@8.7.4",
"description": "A blazing fast unit test framework powered by Vite",
"scripts": {
"ci": "ni && nr typecheck && nr lint && nr build && nr test:all",
Expand Down Expand Up @@ -75,7 +75,7 @@
},
"pnpm": {
"overrides": {
"vite": "^4.3.9",
"vite": "^4.4.9",
"vitest": "workspace:*"
},
"peerDependencyRules": {
Expand Down
3 changes: 2 additions & 1 deletion packages/runner/src/utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export function interpretTaskModes(suite: Suite, namePattern?: string | RegExp,
}

function getTaskFullName(task: TaskBase): string {
return `${task.suite ? `${getTaskFullName(task.suite)} ` : ''}${task.name}`
const fullName = task.suite ? getTaskFullName(task.suite) : null
return fullName ? `${fullName} ${task.name}` : task.name
}

export function someTasksAreOnly(suite: Suite): boolean {
Expand Down
30 changes: 16 additions & 14 deletions packages/vite-node/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ export function withInlineSourcemap(result: TransformResult, options: {
if (!map || code.includes(VITE_NODE_SOURCEMAPPING_SOURCE))
return result

map.sources = map.sources?.map((source) => {
if (!source)
if ('sources' in map) {
map.sources = map.sources?.map((source) => {
if (!source)
return source
// sometimes files here are absolute,
// but they are considered absolute to the server url, not the file system
// this is a bug in Vite
// all files should be either absolute to the file system or relative to the source map file
if (isAbsolute(source)) {
const actualPath = (!source.startsWith(withTrailingSlash(options.root)) && source.startsWith('/'))
? resolve(options.root, source.slice(1))
: source
return relative(dirname(options.filepath), actualPath)
}
return source
// sometimes files here are absolute,
// but they are considered absolute to the server url, not the file system
// this is a bug in Vite
// all files should be either absolute to the file system or relative to the source map file
if (isAbsolute(source)) {
const actualPath = (!source.startsWith(withTrailingSlash(options.root)) && source.startsWith('/'))
? resolve(options.root, source.slice(1))
: source
return relative(dirname(options.filepath), actualPath)
}
return source
})
})
}

// to reduce the payload size, we only inline vite node source map, because it's also the only one we use
const OTHER_SOURCE_MAP_REGEXP = new RegExp(`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`, 'gm')
Expand Down
13 changes: 3 additions & 10 deletions packages/vitest/src/integrations/browser/server.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { createServer } from 'vite'
import { resolve } from 'pathe'
import { findUp } from 'find-up'
import { configFiles, defaultBrowserPort } from '../../constants'
import type { UserConfig } from '../../types/config'
import { defaultBrowserPort } from '../../constants'
import { ensurePackageInstalled } from '../../node/pkg'
import { resolveApiServerConfig } from '../../node/config'
import { CoverageTransform } from '../../node/plugins/coverageTransform'
import type { WorkspaceProject } from '../../node/workspace'
import { MocksPlugin } from '../../node/plugins/mocks'
import { resolveFsAllow } from '../../node/plugins/utils'

export async function createBrowserServer(project: WorkspaceProject, options: UserConfig) {
export async function createBrowserServer(project: WorkspaceProject, configFile: string | undefined) {
const root = project.config.root

await ensurePackageInstalled('@vitest/browser', root)

const configPath = options.config === false
? false
: options.config
? resolve(root, options.config)
: await findUp(configFiles, { cwd: root } as any)
const configPath = typeof configFile === 'string' ? configFile : false

const server = await createServer({
logLevel: 'error',
Expand Down
21 changes: 8 additions & 13 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,15 @@ export class Vitest {

await Promise.all(this._onSetServer.map(fn => fn()))

this.projects = await this.resolveWorkspace(options, cliOptions)
this.projects = await this.resolveWorkspace(cliOptions)

if (this.config.testNamePattern)
this.configOverride.testNamePattern = this.config.testNamePattern
}

private async createCoreWorkspace(options: UserConfig) {
const coreWorkspace = new WorkspaceProject(this.config.root, this)
await coreWorkspace.setServer(options, this.server, {
runner: this.runner,
server: this.vitenode,
})
this.coreWorkspaceProject = coreWorkspace
return coreWorkspace
private async createCoreProject() {
this.coreWorkspaceProject = await WorkspaceProject.createCoreProject(this)
return this.coreWorkspaceProject
}

public getCoreWorkspaceProject(): WorkspaceProject | null {
Expand All @@ -161,7 +156,7 @@ export class Vitest {
|| this.projects[0]
}

private async resolveWorkspace(options: UserConfig, cliOptions: UserConfig) {
private async resolveWorkspace(cliOptions: UserConfig) {
const configDir = this.server.config.configFile
? dirname(this.server.config.configFile)
: this.config.root
Expand All @@ -171,7 +166,7 @@ export class Vitest {
})

if (!workspaceConfigName)
return [await this.createCoreWorkspace(options)]
return [await this.createCoreProject()]

const workspaceConfigPath = join(configDir, workspaceConfigName)

Expand Down Expand Up @@ -259,7 +254,7 @@ export class Vitest {
if (
this.server.config.configFile === workspacePath
)
return this.createCoreWorkspace(options)
return this.createCoreProject()
return initializeProject(workspacePath, this, { workspaceConfigPath, test: cliOverrides })
})

Expand All @@ -268,7 +263,7 @@ export class Vitest {
})

if (!projects.length)
return [await this.createCoreWorkspace(options)]
return [await this.createCoreProject()]

const resolvedProjects = await Promise.all(projects)
const names = new Set<string>()
Expand Down
5 changes: 2 additions & 3 deletions packages/vitest/src/node/plugins/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export function VitestOptimizer(): Plugin {

viteConfig.cacheDir = webOptimizer.cacheDir || ssrOptimizer.cacheDir || viteConfig.cacheDir
viteConfig.optimizeDeps = webOptimizer.optimizeDeps
viteConfig.ssr = {
optimizeDeps: ssrOptimizer.optimizeDeps,
}
viteConfig.ssr ??= {}
viteConfig.ssr.optimizeDeps = ssrOptimizer.optimizeDeps
},
},
}
Expand Down
27 changes: 16 additions & 11 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import { getBrowserProvider } from '../integrations/browser'
import { isBrowserEnabled, resolveConfig } from './config'
import { WorkspaceVitestPlugin } from './plugins/workspace'

interface InitializeServerOptions {
server?: ViteNodeServer
runner?: ViteNodeRunner
}

interface InitializeProjectOptions extends UserWorkspaceConfig {
workspaceConfigPath: string
extends?: string
Expand Down Expand Up @@ -187,20 +182,30 @@ export class WorkspaceProject {
return testFiles
}

async initBrowserServer(options: UserConfig) {
async initBrowserServer(configFile: string | undefined) {
if (!this.isBrowserEnabled())
return
await this.browser?.close()
this.browser = await createBrowserServer(this, options)
this.browser = await createBrowserServer(this, configFile)
}

static async createCoreProject(ctx: Vitest) {
const project = new WorkspaceProject(ctx.config.name || ctx.config.root, ctx)
project.vitenode = ctx.vitenode
project.server = ctx.server
project.runner = ctx.runner
project.config = ctx.config
await project.initBrowserServer(ctx.server.config.configFile)
return project
}

async setServer(options: UserConfig, server: ViteDevServer, params: InitializeServerOptions = {}) {
async setServer(options: UserConfig, server: ViteDevServer) {
this.config = resolveConfig(this.ctx.mode, options, server.config)
this.server = server

this.vitenode = params.server ?? new ViteNodeServer(server, this.config)
this.vitenode = new ViteNodeServer(server, this.config)
const node = this.vitenode
this.runner = params.runner ?? new ViteNodeRunner({
this.runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fetchModule(id: string) {
Expand All @@ -211,7 +216,7 @@ export class WorkspaceProject {
},
})

await this.initBrowserServer(options)
await this.initBrowserServer(this.server.config.configFile)
}

async report<T extends keyof Reporter>(name: T, ...args: ArgumentsType<Reporter[T]>) {
Expand Down
Loading

0 comments on commit 070bc9e

Please sign in to comment.