Skip to content

Commit 80a43d5

Browse files
authored
fix(browser): inline pretty-format and replace picocolors with tinyrainbow (#6077)
1 parent 7f0cc24 commit 80a43d5

File tree

108 files changed

+2218
-415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2218
-415
lines changed

docs/config/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,7 @@ Path to a diff config that will be used to generate diff interface. Useful if yo
21812181
:::code-group
21822182
```ts [vitest.diff.ts]
21832183
import type { DiffOptions } from 'vitest'
2184-
import c from 'picocolors'
2184+
import c from 'tinyrainbow'
21852185

21862186
export default {
21872187
aIndicator: c.bold('--'),

packages/browser/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"types": "./context.d.ts",
2929
"default": "./context.js"
3030
},
31+
"./client": {
32+
"default": "./dist/client.js"
33+
},
3134
"./matchers": {
3235
"types": "./matchers.d.ts"
3336
},

packages/browser/rollup.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ export default () =>
5757
}),
5858
],
5959
},
60+
{
61+
input: './src/client/client.ts',
62+
output: {
63+
file: 'dist/client.js',
64+
format: 'esm',
65+
},
66+
plugins: [
67+
resolve({
68+
preferBuiltins: true,
69+
}),
70+
esbuild({
71+
target: 'node18',
72+
}),
73+
],
74+
},
6075
{
6176
input: './src/client/tester/state.ts',
6277
output: {

packages/browser/src/client/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getBrowserState } from './utils'
66

77
const PAGE_TYPE = getBrowserState().type
88

9-
export const PORT = import.meta.hot ? '51204' : location.port
9+
export const PORT = location.port
1010
export const HOST = [location.hostname, PORT].filter(Boolean).join(':')
1111
export const SESSION_ID
1212
= PAGE_TYPE === 'orchestrator'
@@ -136,4 +136,4 @@ function createClient() {
136136

137137
export const client = createClient()
138138

139-
export { channel, waitForChannel } from './channel'
139+
export * from './channel'

packages/browser/src/client/orchestrator.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
height: 100%;
2424
}
2525
</style>
26-
<script>{__VITEST_INJECTOR__}</script>
26+
{__VITEST_INJECTOR__}
27+
{__VITEST_ERROR_CATCHER__}
2728
{__VITEST_SCRIPTS__}
2829
</head>
2930
<body>

packages/browser/src/client/orchestrator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { ResolvedConfig } from 'vitest'
2+
import { channel, client } from '@vitest/browser/client'
23
import { generateHash } from '@vitest/runner/utils'
4+
import { type GlobalChannelIncomingEvent, type IframeChannelEvent, type IframeChannelIncomingEvent, globalChannel } from '@vitest/browser/client'
35
import { relative } from 'pathe'
4-
import { channel, client } from './client'
56
import { getBrowserState, getConfig } from './utils'
67
import { getUiAPI } from './ui'
7-
import { type GlobalChannelIncomingEvent, type IframeChannelEvent, type IframeChannelIncomingEvent, globalChannel } from './channel'
88
import { createModuleMocker } from './tester/msw'
99

1010
const url = new URL(location.href)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { channel, client } from '/@id/@vitest/browser/client'
2+
3+
function on(event, listener) {
4+
window.addEventListener(event, listener)
5+
return () => window.removeEventListener(event, listener)
6+
}
7+
8+
function serializeError(unhandledError) {
9+
if (typeof unhandledError !== 'object' || !unhandledError) {
10+
return {
11+
message: String(unhandledError),
12+
}
13+
}
14+
15+
return {
16+
name: unhandledError.name,
17+
message: unhandledError.message,
18+
stack: String(unhandledError.stack),
19+
}
20+
}
21+
22+
function catchWindowErrors(cb) {
23+
let userErrorListenerCount = 0
24+
function throwUnhandlerError(e) {
25+
if (userErrorListenerCount === 0 && e.error != null) {
26+
cb(e)
27+
}
28+
else {
29+
console.error(e.error)
30+
}
31+
}
32+
const addEventListener = window.addEventListener.bind(window)
33+
const removeEventListener = window.removeEventListener.bind(window)
34+
window.addEventListener('error', throwUnhandlerError)
35+
window.addEventListener = function (...args) {
36+
if (args[0] === 'error') {
37+
userErrorListenerCount++
38+
}
39+
return addEventListener.apply(this, args)
40+
}
41+
window.removeEventListener = function (...args) {
42+
if (args[0] === 'error' && userErrorListenerCount) {
43+
userErrorListenerCount--
44+
}
45+
return removeEventListener.apply(this, args)
46+
}
47+
return function clearErrorHandlers() {
48+
window.removeEventListener('error', throwUnhandlerError)
49+
}
50+
}
51+
52+
function registerUnexpectedErrors() {
53+
catchWindowErrors(event =>
54+
reportUnexpectedError('Error', event.error),
55+
)
56+
on('unhandledrejection', event =>
57+
reportUnexpectedError('Unhandled Rejection', event.reason))
58+
}
59+
60+
async function reportUnexpectedError(
61+
type,
62+
error,
63+
) {
64+
const processedError = serializeError(error)
65+
await client.rpc.onUnhandledError(processedError, type)
66+
const state = __vitest_browser_runner__
67+
68+
if (state.type === 'orchestrator') {
69+
return
70+
}
71+
72+
if (!state.runTests || !__vitest_worker__.current) {
73+
channel.postMessage({
74+
type: 'done',
75+
filenames: state.files,
76+
id: state.iframeId,
77+
})
78+
}
79+
}
80+
81+
registerUnexpectedErrors()

packages/browser/src/client/tester/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Task, WorkerGlobalState } from 'vitest'
2+
import type { BrowserRPC } from '@vitest/browser/client'
23
import type { BrowserPage, UserEvent, UserEventClickOptions, UserEventTabOptions, UserEventTypeOptions } from '../../../context'
34
import type { BrowserRunnerState } from '../utils'
4-
import type { BrowserRPC } from '../client'
55

66
// this file should not import anything directly, only types
77

packages/browser/src/client/tester/mocker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getType } from '@vitest/utils'
22
import { extname, join } from 'pathe'
3+
import type { IframeChannelOutgoingEvent } from '@vitest/browser/client'
4+
import { channel, waitForChannel } from '@vitest/browser/client'
35
import { getBrowserState, importId } from '../utils'
4-
import type { IframeChannelOutgoingEvent } from '../channel'
5-
import { channel, waitForChannel } from '../client'
66
import { rpc } from './rpc'
77

88
const now = Date.now

packages/browser/src/client/tester/msw.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { channel } from '@vitest/browser/client'
12
import type {
23
IframeChannelEvent,
34
IframeMockEvent,
45
IframeMockingDoneEvent,
56
IframeUnmockEvent,
6-
} from '../channel'
7-
import { channel } from '../channel'
7+
} from '@vitest/browser/client'
88

99
export function createModuleMocker() {
1010
const mocks: Map<string, string | null | undefined> = new Map()

0 commit comments

Comments
 (0)