Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
6 changes: 4 additions & 2 deletions test/browser/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof playwright>[0]) => playwright(process.env.BROWSER_WS_ENDPOINT
playwright: (options?: Parameters<typeof playwright>[0]) => playwright(wsEndpoint
? {
...options,
connectOptions: {
wsEndpoint: process.env.BROWSER_WS_ENDPOINT,
wsEndpoint,
exposeNetwork: '<loopback>',
},
}
Expand Down
171 changes: 26 additions & 145 deletions test/browser/specs/assertion-helper.test.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand All @@ -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",
],
},
}
Expand All @@ -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",
],
},
}
Expand All @@ -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",
],
},
}
Expand Down
4 changes: 2 additions & 2 deletions test/config/test/conditions-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
39 changes: 26 additions & 13 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() || [])
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<string, Record<string, any>> = {}

for (const testModule of testModules) {
const projectName = testModule.project.name
projectTree[projectName] = {
...projectTree[projectName],
...buildErrorTree([testModule]),
...buildErrorTree([testModule], options),
}
}

Expand Down
Loading