Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ describe('SSR hydration', () => {
)
expect(teleportContainer.innerHTML).toBe('<span>Teleported Comp1</span>')
expect(`Hydration children mismatch`).toHaveBeenWarned()
expect(`Hydration completed but contains mismatches.`).toHaveBeenErrored()

toggle.value = false
await nextTick()
Expand Down Expand Up @@ -2472,6 +2473,9 @@ describe('SSR hydration', () => {
'<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
)
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
expect(
`Hydration completed but contains mismatches.`,
).not.toHaveBeenErrored()
})

test('fragment too many children', () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export enum DOMNodeTypes {

let hasLoggedMismatchError = false
const logMismatchError = () => {
if (__TEST__ || hasLoggedMismatchError) {
if (hasLoggedMismatchError) {
return
}
// this error should show up in production
Expand Down Expand Up @@ -664,9 +664,11 @@ export function createHydrationFunctions(
if (next && isComment(next) && next.data === ']') {
return nextSibling((vnode.anchor = next))
} else {
// fragment didn't hydrate successfully, since we didn't get a end anchor
// back. This should have led to node/children mismatch warnings.
logMismatchError()
if (!isMismatchAllowed(container, MismatchTypes.CHILDREN)) {
// fragment didn't hydrate successfully, since we didn't get a end anchor
// back. This should have led to node/children mismatch warnings.
logMismatchError()
}

// since the anchor is missing, we need to create one and insert it
insert((vnode.anchor = createComment(`]`)), container, next)
Expand Down
43 changes: 43 additions & 0 deletions scripts/setup-vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare module 'vitest' {
}

interface CustomMatchers<R = unknown> {
toHaveBeenErrored(): R
toHaveBeenWarned(): R
toHaveBeenWarnedLast(): R
toHaveBeenWarnedTimes(n: number): R
Expand All @@ -14,6 +15,27 @@ interface CustomMatchers<R = unknown> {
vi.stubGlobal('MathMLElement', class MathMLElement {})

expect.extend({
toHaveBeenErrored(received: string) {
const passed = error.mock.calls.some(args => args[0].includes(received))
if (passed) {
asserted.add(received)
return {
pass: true,
message: () => `expected "${received}" not to have been errored.`,
}
} else {
const msgs = error.mock.calls.map(args => args[0]).join('\n - ')
return {
pass: false,
message: () =>
`expected "${received}" to have been errored` +
(msgs.length
? `.\n\nActual messages:\n\n - ${msgs}`
: ` but no error was recorded.`),
}
}
},

toHaveBeenWarned(received: string) {
const passed = warn.mock.calls.some(args => args[0].includes(received))
if (passed) {
Expand Down Expand Up @@ -79,16 +101,21 @@ expect.extend({
})

let warn: MockInstance
let error: MockInstance
const asserted: Set<string> = new Set()

beforeEach(() => {
asserted.clear()
warn = vi.spyOn(console, 'warn')
warn.mockImplementation(() => {})

error = vi.spyOn(console, 'error')
error.mockImplementation(() => {})
})

afterEach(() => {
const assertedArray = Array.from(asserted)

const nonAssertedWarnings = warn.mock.calls
.map(args => args[0])
.filter(received => {
Expand All @@ -104,4 +131,20 @@ afterEach(() => {
)}`,
)
}

const nonAssertedErrors = error.mock.calls
.map(args => args[0])
.filter(received => {
return !assertedArray.some(assertedMsg => {
return received.includes(assertedMsg)
})
})
error.mockRestore()
if (nonAssertedErrors.length) {
throw new Error(
`test case threw unexpected errors:\n - ${nonAssertedErrors.join(
'\n - ',
)}`,
)
}
})