diff --git a/test/browser/README.md b/test/browser/README.md index e2896e9bfc0a..f4217a00fb50 100644 --- a/test/browser/README.md +++ b/test/browser/README.md @@ -26,5 +26,5 @@ Some test suites don't support running it remotely (`fixtures/inspect` and `fixt pnpm docker up -d # Run tests with BROWSER_WS_ENDPOINT -BROWSER_WS_ENDPOINT=ws://127.0.0.1:6677/ pnpm run test:playwright +BROWSER_WS_ENDPOINT=true pnpm run test:playwright ``` diff --git a/test/browser/settings.ts b/test/browser/settings.ts index fc6493cd5cca..c6bb019a38f9 100644 --- a/test/browser/settings.ts +++ b/test/browser/settings.ts @@ -5,12 +5,14 @@ import { webdriverio } from '@vitest/browser-webdriverio' const providerName = (process.env.PROVIDER || 'playwright') as 'playwright' | 'webdriverio' | 'preview' +const wsEndpoint = process.env.BROWSER_WS_ENDPOINT === 'true' ? 'ws://127.0.0.1:6677/' : process.env.BROWSER_WS_ENDPOINT + export const providers = { - playwright: (options?: Parameters[0]) => playwright(process.env.BROWSER_WS_ENDPOINT + playwright: (options?: Parameters[0]) => playwright(wsEndpoint ? { ...options, connectOptions: { - wsEndpoint: process.env.BROWSER_WS_ENDPOINT, + wsEndpoint, exposeNetwork: '', }, } diff --git a/test/browser/specs/assertion-helper.test.ts b/test/browser/specs/assertion-helper.test.ts index ed9decb5d490..8cc463c2be95 100644 --- a/test/browser/specs/assertion-helper.test.ts +++ b/test/browser/specs/assertion-helper.test.ts @@ -1,23 +1,12 @@ -import path from 'node:path' import { expect, test } from 'vitest' -import { buildTestProjectTree } from '../../test-utils' import { instances, runBrowserTests } from './utils' test('vi.defineHelper hides internal stack traces', async () => { - const { results, ctx } = await runBrowserTests({ + const { errorTree } = await runBrowserTests({ root: './fixtures/assertion-helper', }) - const projectTree = buildTestProjectTree(results, (testCase) => { - const result = testCase.result() - return result.errors.map((e) => { - const stacks = e.stacks.map(s => ({ - ...s, - file: path.relative(ctx.config.root, s.file), - })) - return ({ message: e.message, stacks }) - }) - }) + const projectTree = errorTree({ project: true, stackTrace: true }) expect(Object.keys(projectTree).sort()).toEqual(instances.map(i => i.browser).sort()) for (const [name, tree] of Object.entries(projectTree)) { @@ -26,56 +15,20 @@ test('vi.defineHelper hides internal stack traces', async () => { { "basic.test.ts": { "async": [ - { - "message": "expected 'async' to deeply equal 'x'", - "stacks": [ - { - "column": 8, - "file": "basic.test.ts", - "line": 26, - "method": "", - }, - ], - }, + "expected 'async' to deeply equal 'x' + at basic.test.ts:26:8", ], "soft": [ - { - "message": "expected 'soft' to deeply equal 'x'", - "stacks": [ - { - "column": 14, - "file": "basic.test.ts", - "line": 30, - "method": "", - }, - ], - }, + "expected 'soft' to deeply equal 'x' + at basic.test.ts:30:14", ], "soft async": [ - { - "message": "expected 'soft async' to deeply equal 'x'", - "stacks": [ - { - "column": 8, - "file": "basic.test.ts", - "line": 34, - "method": "", - }, - ], - }, + "expected 'soft async' to deeply equal 'x' + at basic.test.ts:34:8", ], "sync": [ - { - "message": "expected 'sync' to deeply equal 'x'", - "stacks": [ - { - "column": 10, - "file": "basic.test.ts", - "line": 22, - "method": "", - }, - ], - }, + "expected 'sync' to deeply equal 'x' + at basic.test.ts:22:10", ], }, } @@ -89,56 +42,20 @@ test('vi.defineHelper hides internal stack traces', async () => { { "basic.test.ts": { "async": [ - { - "message": "expected 'async' to deeply equal 'x'", - "stacks": [ - { - "column": 20, - "file": "basic.test.ts", - "line": 9, - "method": "", - }, - ], - }, + "expected 'async' to deeply equal 'x' + at basic.test.ts:9:20", ], "soft": [ - { - "message": "expected 'soft' to deeply equal 'x'", - "stacks": [ - { - "column": 14, - "file": "basic.test.ts", - "line": 30, - "method": "", - }, - ], - }, + "expected 'soft' to deeply equal 'x' + at basic.test.ts:30:14", ], "soft async": [ - { - "message": "expected 'soft async' to deeply equal 'x'", - "stacks": [ - { - "column": 25, - "file": "basic.test.ts", - "line": 18, - "method": "", - }, - ], - }, + "expected 'soft async' to deeply equal 'x' + at basic.test.ts:18:25", ], "sync": [ - { - "message": "expected 'sync' to deeply equal 'x'", - "stacks": [ - { - "column": 10, - "file": "basic.test.ts", - "line": 22, - "method": "", - }, - ], - }, + "expected 'sync' to deeply equal 'x' + at basic.test.ts:22:10", ], }, } @@ -149,56 +66,20 @@ test('vi.defineHelper hides internal stack traces', async () => { { "basic.test.ts": { "async": [ - { - "message": "expected 'async' to deeply equal 'x'", - "stacks": [ - { - "column": 2, - "file": "basic.test.ts", - "line": 26, - "method": "", - }, - ], - }, + "expected 'async' to deeply equal 'x' + at basic.test.ts:26:2", ], "soft": [ - { - "message": "expected 'soft' to deeply equal 'x'", - "stacks": [ - { - "column": 2, - "file": "basic.test.ts", - "line": 30, - "method": "", - }, - ], - }, + "expected 'soft' to deeply equal 'x' + at basic.test.ts:30:2", ], "soft async": [ - { - "message": "expected 'soft async' to deeply equal 'x'", - "stacks": [ - { - "column": 2, - "file": "basic.test.ts", - "line": 34, - "method": "", - }, - ], - }, + "expected 'soft async' to deeply equal 'x' + at basic.test.ts:34:2", ], "sync": [ - { - "message": "expected 'sync' to deeply equal 'x'", - "stacks": [ - { - "column": 2, - "file": "basic.test.ts", - "line": 22, - "method": "", - }, - ], - }, + "expected 'sync' to deeply equal 'x' + at basic.test.ts:22:2", ], }, } diff --git a/test/config/test/conditions-cli.test.ts b/test/config/test/conditions-cli.test.ts index 09a0fb96a189..587295f3a57a 100644 --- a/test/config/test/conditions-cli.test.ts +++ b/test/config/test/conditions-cli.test.ts @@ -104,11 +104,11 @@ test('conditions (inline indirect)', async () => { }) test('project resolve.conditions', async () => { - const { stderr, errorProjectTree } = await runVitest({ + const { stderr, errorTree } = await runVitest({ root: 'fixtures/conditions-projects', }) expect(stderr).toBe('') - expect(errorProjectTree()).toMatchInlineSnapshot(` + expect(errorTree({ project: true })).toMatchInlineSnapshot(` { "project-a": { "basic.test.js": { diff --git a/test/test-utils/index.ts b/test/test-utils/index.ts index f76a68a4da21..3997b7577294 100644 --- a/test/test-utils/index.ts +++ b/test/test-utils/index.ts @@ -241,17 +241,17 @@ export async function runVitest( get results() { return ctx?.state.getTestModules() || [] }, - errorTree() { - const tree = buildErrorTree(ctx?.state.getTestModules() || []) + errorTree(options?: { project?: boolean; stackTrace?: boolean }) { + const modules = ctx?.state.getTestModules() || [] + const tree = options?.project + ? buildErrorProjectTree(modules, options) + : buildErrorTree(modules, options) const errors = ctx?.state.getUnhandledErrors() if (errors && errors.length > 0) { tree.__unhandled_errors__ = errors.map((e: any) => e.message) } return tree }, - errorProjectTree() { - return buildErrorProjectTree(ctx?.state.getTestModules() || []) - }, testTree() { return buildTestTree(ctx?.state.getTestModules() || []) }, @@ -570,32 +570,45 @@ export class StableTestFileOrderSorter { } } -export function buildErrorTree(testModules: TestModule[]) { +export function buildErrorTree(testModules: TestModule[], options?: { stackTrace?: boolean }) { + const root = testModules[0]?.project.config.root + + function mapError(e: { message: string; stacks?: { file: string; line: number; column: number; method: string }[] }) { + if (options?.stackTrace) { + const stacks = (e.stacks || []).map((s) => { + const loc = `${relative(root, s.file)}:${s.line}:${s.column}` + return s.method ? ` at ${s.method} (${loc})` : ` at ${loc}` + }) + return [e.message, ...stacks].join('\n') + } + return e.message + } + return buildTestTree( testModules, (testCase) => { const result = testCase.result() if (result.state === 'failed') { - return result.errors.map(e => e.message) + return result.errors.map(e => mapError(e)) } return result.state }, (testSuite, suiteChildren) => { - const errors = testSuite.errors().map(error => error.message) + const errors = testSuite.errors() if (errors.length > 0) { return { ...suiteChildren, - __suite_errors__: errors, + __suite_errors__: errors.map(e => mapError(e)), } } return suiteChildren }, (testModule, moduleChildren) => { - const errors = testModule.errors().map(error => error.message) + const errors = testModule.errors() if (errors.length > 0) { return { ...moduleChildren, - __module_errors__: errors, + __module_errors__: errors.map(e => mapError(e)), } } return moduleChildren @@ -660,14 +673,14 @@ export function buildTestProjectTree(testModules: TestModule[], onTestCase?: (re return projectTree } -export function buildErrorProjectTree(testModules: TestModule[]) { +export function buildErrorProjectTree(testModules: TestModule[], options?: { stackTrace?: boolean }) { const projectTree: Record> = {} for (const testModule of testModules) { const projectName = testModule.project.name projectTree[projectName] = { ...projectTree[projectName], - ...buildErrorTree([testModule]), + ...buildErrorTree([testModule], options), } }