Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions playground/typed-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ declare module 'vue-router/auto-routes' {
'/partial-[name]': RouteRecordInfo<'/partial-[name]', '/partial-:name', { name: ParamValue<true> }, { name: ParamValue<false> }>,
'/custom-path': RouteRecordInfo<'/custom-path', '/surprise-:id(\d+)', Record<never, never>, Record<never, never>>,
'/test-[a-id]': RouteRecordInfo<'/test-[a-id]', '/test-:a-id', { aId: ParamValue<true> }, { aId: ParamValue<false> }>,
'/test-broken-syntax': RouteRecordInfo<'/test-broken-syntax', '/test-broken-syntax', Record<never, never>, Record<never, never>>,
'/todos/': RouteRecordInfo<'/todos/', '/todos', Record<never, never>, Record<never, never>>,
'/todos/+layout': RouteRecordInfo<'/todos/+layout', '/todos/+layout', Record<never, never>, Record<never, never>>,
'/users/': RouteRecordInfo<'/users/', '/users', Record<never, never>, Record<never, never>>,
Expand Down Expand Up @@ -287,6 +288,10 @@ declare module 'vue-router/auto-routes' {
routes: '/test-[a-id]'
views: never
}
'src/pages/test-broken-syntax.vue': {
routes: '/test-broken-syntax'
views: never
}
'src/pages/todos/index.vue': {
routes: '/todos/'
views: never
Expand Down
100 changes: 92 additions & 8 deletions src/core/definePage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TransformResult } from 'vite'
import { expect, describe, it } from 'vitest'
import { definePageTransform, extractDefinePageNameAndPath } from './definePage'
import { mockWarn } from '../../tests/vitest-mock-warn'

const sampleCode = `
<script setup>
Expand All @@ -18,6 +19,7 @@
`

describe('definePage', () => {
mockWarn()
it('removes definePage', async () => {
const result = (await definePageTransform({
code: sampleCode,
Expand Down Expand Up @@ -153,7 +155,7 @@
expect(result?.code).toMatchSnapshot()
})

it('throws if definePage uses a variable from the setup', async () => {
it('handles definePage using a variable from setup gracefully', async () => {
const code = `
<script setup>
const a = 1
Expand All @@ -162,13 +164,13 @@
})
</script>
`
// the function syntax works with sync and async errors
await expect(async () => {
await definePageTransform({
code,
id: 'src/pages/basic.vue&definePage&vue',
})
}).rejects.toThrowError()
const result = await definePageTransform({
code,
id: 'src/pages/basic.vue&definePage&vue',
})

// Should return empty object instead of throwing
expect(result).toBe('export default {}')
})

it('extracts name and path', async () => {
Expand Down Expand Up @@ -254,4 +256,86 @@
path: '/custom',
})
})

describe('error handling', () => {
const codeWithSyntaxError = `
<script setup>
definePage({
name: 'test',, // syntax error: extra comma
path: '/test'
})
</script>

<template>
<div>hello</div>
</template>
`

it('handles syntax errors gracefully when extracting definePage', async () => {
const result = await definePageTransform({
code: codeWithSyntaxError,
id: 'src/pages/broken.vue?definePage&vue',
})

// Should return empty object instead of crashing
expect(result).toBe('export default {}')
expect('Failed to process definePage:').toHaveBeenWarned()

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / copilot

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47

Check failure on line 282 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / copilot

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when extracting definePage

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:282:47
})

it('handles syntax errors gracefully when removing definePage from source', async () => {
const codeForRemoval = `
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This piece of code is duplicated, no need to have it twice

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the duplicated codeForRemoval variable and now using the shared codeWithSyntaxError variable to avoid duplication. (d8052e0)

<script setup>
const a = 1
definePage({
name: 'test',, // syntax error: extra comma
path: '/test'
})
const b = 1

Check failure on line 293 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / copilot

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:293:47
</script>

<template>
<div>hello</div>
</template>
`

const result = await definePageTransform({
code: codeForRemoval,
id: 'src/pages/broken.vue',
})

Check failure on line 304 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / copilot

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:304:52

// Should return undefined (no transform) instead of crashing
expect(result).toBeUndefined()
expect('Failed to process definePage:').toHaveBeenWarned()

Check failure on line 308 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:308:47

Check failure on line 308 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:308:47

Check failure on line 308 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:308:47

Check failure on line 308 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:308:47

Check failure on line 308 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:308:47

Check failure on line 308 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles syntax errors gracefully when removing definePage from source

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:308:47
})

it('handles extractDefinePageNameAndPath with syntax errors gracefully', async () => {
const result = await extractDefinePageNameAndPath(
codeWithSyntaxError,
'src/pages/broken.vue'
)

// Should return null/undefined instead of crashing
expect(result).toBeUndefined()
expect('Failed to extract definePage info:').toHaveBeenWarned()

Check failure on line 319 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:319:52

Check failure on line 319 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:319:52

Check failure on line 319 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:319:52

Check failure on line 319 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:319:52

Check failure on line 319 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:319:52

Check failure on line 319 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles extractDefinePageNameAndPath with syntax errors gracefully

Error: expected "Failed to extract definePage info:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:319:52
})

it('handles unclosed brackets in definePage gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a syntax error, it's a redundant test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant "handles unclosed brackets in definePage gracefully" test as it was just another syntax error variation already covered by existing tests. (d8052e0)

const codeWithUnclosedBracket = `
<script setup>
definePage({
name: 'test',
path: '/test'
// missing closing bracket
</script>
`

const result = await definePageTransform({
code: codeWithUnclosedBracket,
id: 'src/pages/unclosed.vue?definePage&vue',
})

expect(result).toBe('export default {}')
expect('Failed to process definePage:').toHaveBeenWarned()

Check failure on line 338 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles unclosed brackets in definePage gracefully

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:338:47

Check failure on line 338 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles unclosed brackets in definePage gracefully

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:338:47

Check failure on line 338 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

src/core/definePage.spec.ts > definePage > error handling > handles unclosed brackets in definePage gracefully

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:338:47

Check failure on line 338 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (20.x, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles unclosed brackets in definePage gracefully

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:338:47

Check failure on line 338 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

src/core/definePage.spec.ts > definePage > error handling > handles unclosed brackets in definePage gracefully

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:338:47

Check failure on line 338 in src/core/definePage.spec.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

src/core/definePage.spec.ts > definePage > error handling > handles unclosed brackets in definePage gracefully

Error: expected "Failed to process definePage:" to have been warned. Actual messages: - ❯ src/core/definePage.spec.ts:338:47
})
})
})
Loading
Loading