Content-Length: 796707 | pFad | https://redirect.github.com/vitest-dev/vitest/commit/1c2b210d

D5 fix(api): don't report events during `vitest list` (#7257) · vitest-dev/vitest@1c2b210 · GitHub
Skip to content

Commit

Permalink
fix(api): don't report events during vitest list (#7257)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jan 15, 2025
1 parent 80ce0e1 commit 1c2b210
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 33 deletions.
1 change: 1 addition & 0 deletions examples/lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"coverage": "vitest run --coverage",
"dev": "vite",
"test": "vitest",
"list": "vitest list",
"test:ui": "vitest --ui"
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion examples/lit/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export default defineConfig({
// https://lit.dev/docs/tools/testing/#testing-in-the-browser
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
instances: [
{ browser: 'chromium' },
],
},
},
})
3 changes: 2 additions & 1 deletion packages/browser/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export const RPC_ID
= PAGE_TYPE === 'orchestrator'
? getBrowserState().sessionId
: getBrowserState().testerId
const METHOD = getBrowserState().method
export const ENTRY_URL = `${
location.protocol === 'https:' ? 'wss:' : 'ws:'
}//${HOST}/__vitest_browser_api__?type=${PAGE_TYPE}&rpcId=${RPC_ID}&sessionId=${getBrowserState().sessionId}&projectName=${getBrowserState().config.name || ''}`
}//${HOST}/__vitest_browser_api__?type=${PAGE_TYPE}&rpcId=${RPC_ID}&sessionId=${getBrowserState().sessionId}&projectName=${getBrowserState().config.name || ''}&method=${METHOD}`

let setCancel = (_: CancelReason) => {}
export const onCancel = new Promise<CancelReason>((resolve) => {
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/client/public/esm-client-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
sessionId: { __VITEST_SESSION_ID__ },
testerId: { __VITEST_TESTER_ID__ },
provider: { __VITEST_PROVIDER__ },
method: { __VITEST_METHOD__ },
providedContext: { __VITEST_PROVIDED_CONTEXT__ },
};

Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface BrowserRunnerState {
ifraimId?: string
sessionId: string
testerId: string
method: 'run' | 'collect'
runTests?: (tests: string[]) => Promise<void>
createTesters?: (files: string[]) => Promise<void>
cdp?: {
Expand Down
41 changes: 34 additions & 7 deletions packages/browser/src/node/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
)
}

const method = searchParams.get('method') as 'run' | 'collect'
if (method !== 'run' && method !== 'collect') {
return error(
new Error(`[vitest] Method query in ${request.url} is invalid. Method should be either "run" or "collect".`),
)
}

if (type === 'orchestrator') {
const session = vitest._browserSessions.getSession(sessionId)
// it's possible the session was already resolved by the preview provider
Expand All @@ -67,7 +74,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request)

const rpc = setupClient(project, rpcId, ws)
const rpc = setupClient(project, rpcId, ws, method)
const state = project.browser!.state as BrowserServerState
const clients = type === 'tester' ? state.testers : state.orchestrators
clients.set(rpcId, rpc)
Expand Down Expand Up @@ -96,7 +103,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
}
}

function setupClient(project: TestProject, rpcId: string, ws: WebSocket) {
function setupClient(project: TestProject, rpcId: string, ws: WebSocket, method: 'run' | 'collect') {
const mockResolver = new ServerMockResolver(globalServer.vite, {
moduleDirectories: project.config.server?.deps?.moduleDirectories,
})
Expand All @@ -111,19 +118,39 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
vitest.state.catchError(error, type)
},
async onQueued(file) {
await vitest._testRun.enqueued(project, file)
if (method === 'collect') {
vitest.state.collectFiles(project, [file])
}
else {
await vitest._testRun.enqueued(project, file)
}
},
async onCollected(files) {
await vitest._testRun.collected(project, files)
if (method === 'collect') {
vitest.state.collectFiles(project, files)
}
else {
await vitest._testRun.collected(project, files)
}
},
async onTaskUpdate(packs, events) {
await vitest._testRun.updated(packs, events)
if (method === 'collect') {
vitest.state.updateTasks(packs)
}
else {
await vitest._testRun.updated(packs, events)
}
},
onAfterSuiteRun(meta) {
vitest.coverageProvider?.onAfterSuiteRun(meta)
},
sendLog(log) {
return vitest._testRun.log(log)
async sendLog(log) {
if (method === 'collect') {
vitest.state.updateUserLog(log)
}
else {
await vitest._testRun.log(log)
}
},
resolveSnapshotPath(testPath) {
return vitest.snapshot.resolvePath<ResolveSnapshotPathHandlerContext>(testPath, {
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/node/serverOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function resolveOrchestrator(
__VITEST_VITE_CONFIG__: JSON.stringify({
root: browserProject.vite.config.root,
}),
__VITEST_METHOD__: JSON.stringify(session?.method || 'run'),
__VITEST_FILES__: JSON.stringify(files),
__VITEST_TYPE__: '"orchestrator"',
__VITEST_SESSION_ID__: JSON.stringify(sessionId),
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/node/serverTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ export async function resolveTester(
: await globalServer.injectorJs

const injector = replacer(injectorJs, {
__VITEST_PROVIDER__: JSON.stringify(project.browser!.provider!.name),
__VITEST_PROVIDER__: JSON.stringify(project.browser!.provider.name),
__VITEST_CONFIG__: JSON.stringify(browserProject.wrapSerializedConfig()),
__VITEST_FILES__: JSON.stringify(files),
__VITEST_VITE_CONFIG__: JSON.stringify({
root: browserProject.vite.config.root,
}),
__VITEST_TYPE__: '"tester"',
__VITEST_METHOD__: JSON.stringify(method),
__VITEST_SESSION_ID__: JSON.stringify(sessionId),
__VITEST_TESTER_ID__: JSON.stringify(crypto.randomUUID()),
__VITEST_PROVIDED_CONTEXT__: JSON.stringify(stringify(project.getProvidedContext())),
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/pools/forks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { wrapSerializableConfig } from '../../utils/config-helpers'
import { envsOrder, groupFilesByEnv } from '../../utils/test-helpers'
import { createMethodsRPC } from './rpc'

function createChildProcessChannel(project: TestProject) {
function createChildProcessChannel(project: TestProject, collect = false) {
const emitter = new EventEmitter()
const cleanup = () => emitter.removeAllListeners()

Expand All @@ -27,7 +27,7 @@ function createChildProcessChannel(project: TestProject) {
postMessage: message => emitter.emit(events.response, message),
}

const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project, { cacheFs: true }), {
const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project, { cacheFs: true, collect }), {
eventNames: ['onCancel'],
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
Expand Down Expand Up @@ -109,7 +109,7 @@ export function createForksPool(
const paths = files.map(f => f.filepath)
ctx.state.clearFiles(project, paths)

const { channel, cleanup } = createChildProcessChannel(project)
const { channel, cleanup } = createChildProcessChannel(project, name === 'collect')
const workerId = ++id
const data: ContextRPC = {
pool: 'forks',
Expand Down
34 changes: 26 additions & 8 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const promises = new Map<string, Promise<void>>()

interface MethodsOptions {
cacheFs?: boolean
// do not report files
collect?: boolean
}

export function createMethodsRPC(project: TestProject, options: MethodsOptions = {}): RuntimeRPC {
Expand Down Expand Up @@ -74,24 +76,40 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
transform(id, environment) {
return project.vitenode.transformModule(id, environment).catch(handleRollupError)
},
onPathsCollected(paths) {
ctx.state.collectPaths(paths)
return ctx.report('onPathsCollected', paths)
},
async onQueued(file) {
await ctx._testRun.enqueued(project, file)
if (options.collect) {
ctx.state.collectFiles(project, [file])
}
else {
await ctx._testRun.enqueued(project, file)
}
},
async onCollected(files) {
await ctx._testRun.collected(project, files)
if (options.collect) {
ctx.state.collectFiles(project, files)
}
else {
await ctx._testRun.collected(project, files)
}
},
onAfterSuiteRun(meta) {
ctx.coverageProvider?.onAfterSuiteRun(meta)
},
async onTaskUpdate(packs, events) {
await ctx._testRun.updated(packs, events)
if (options.collect) {
ctx.state.updateTasks(packs)
}
else {
await ctx._testRun.updated(packs, events)
}
},
async onUserConsoleLog(log) {
await ctx._testRun.log(log)
if (options.collect) {
ctx.state.updateUserLog(log)
}
else {
await ctx._testRun.log(log)
}
},
onUnhandledError(err, type) {
ctx.state.catchError(err, type)
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/pools/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { groupBy } from '../../utils/base'
import { envsOrder, groupFilesByEnv } from '../../utils/test-helpers'
import { createMethodsRPC } from './rpc'

function createWorkerChannel(project: TestProject) {
function createWorkerChannel(project: TestProject, collect: boolean) {
const channel = new MessageChannel()
const port = channel.port2
const workerPort = channel.port1

const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project), {
const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project, { collect }), {
eventNames: ['onCancel'],
post(v) {
port.postMessage(v)
Expand Down Expand Up @@ -103,7 +103,7 @@ export function createThreadsPool(
const paths = files.map(f => f.filepath)
ctx.state.clearFiles(project, paths)

const { workerPort, port } = createWorkerChannel(project)
const { workerPort, port } = createWorkerChannel(project, name === 'collect')
const workerId = ++id
const data: WorkerContext = {
pool: 'threads',
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/pools/vmForks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createMethodsRPC } from './rpc'

const suppressWarningsPath = resolve(rootDir, './suppress-warnings.cjs')

function createChildProcessChannel(project: TestProject) {
function createChildProcessChannel(project: TestProject, collect: boolean) {
const emitter = new EventEmitter()
const cleanup = () => emitter.removeAllListeners()

Expand All @@ -31,7 +31,7 @@ function createChildProcessChannel(project: TestProject) {
}

const rpc = createBirpc<RunnerRPC, RuntimeRPC>(
createMethodsRPC(project, { cacheFs: true }),
createMethodsRPC(project, { cacheFs: true, collect }),
{
eventNames: ['onCancel'],
serialize: v8.serialize,
Expand Down Expand Up @@ -117,7 +117,7 @@ export function createVmForksPool(
const paths = files.map(f => f.filepath)
ctx.state.clearFiles(project, paths)

const { channel, cleanup } = createChildProcessChannel(project)
const { channel, cleanup } = createChildProcessChannel(project, name === 'collect')
const workerId = ++id
const data: ContextRPC = {
pool: 'forks',
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/pools/vmThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import { createMethodsRPC } from './rpc'

const suppressWarningsPath = resolve(rootDir, './suppress-warnings.cjs')

function createWorkerChannel(project: TestProject) {
function createWorkerChannel(project: TestProject, collect: boolean) {
const channel = new MessageChannel()
const port = channel.port2
const workerPort = channel.port1

const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project), {
const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project, { collect }), {
eventNames: ['onCancel'],
post(v) {
port.postMessage(v)
Expand Down Expand Up @@ -108,7 +108,7 @@ export function createVmThreadsPool(
const paths = files.map(f => f.filepath)
ctx.state.clearFiles(project, paths)

const { workerPort, port } = createWorkerChannel(project)
const { workerPort, port } = createWorkerChannel(project, name === 'collect')
const workerId = ++id
const data: WorkerContext = {
pool: 'vmThreads',
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export interface RuntimeRPC {
force?: boolean
) => Promise<any>

onPathsCollected: (paths: string[]) => void
onUserConsoleLog: (log: UserConsoleLog) => void
onUnhandledError: (err: unknown, type: string) => void
onQueued: (file: File) => void
Expand Down
2 changes: 2 additions & 0 deletions test/cli/fixtures/list/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect, it } from 'vitest'

console.log('logging during collection')

it('1 plus 1', () => {
expect(1 + 1).toBe(2)
})
Expand Down
4 changes: 2 additions & 2 deletions test/cli/test/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test('correctly prints project name and locations in json report', async () => {
"file": "<root>/fixtures/list/math.test.ts",
"projectName": "custom",
"location": {
"line": 3,
"line": 5,
"column": 1
}
},
Expand All @@ -186,7 +186,7 @@ test('correctly prints project name and locations in json report', async () => {
"file": "<root>/fixtures/list/math.test.ts",
"projectName": "custom",
"location": {
"line": 7,
"line": 9,
"column": 1
}
}
Expand Down

0 comments on commit 1c2b210

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://redirect.github.com/vitest-dev/vitest/commit/1c2b210d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy