Skip to content

Commit

Permalink
test: add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 28, 2022
1 parent da4db97 commit f6c33bd
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 9 deletions.
1 change: 1 addition & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.d.ts
121 changes: 121 additions & 0 deletions e2e/__snapshots__/routes.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Vitest Snapshot v1

exports[`generates the routes 1`] = `
"export const routes = [
{
path: \\"/:path(.*)\\",
name: \\"/[...path]\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/[...path].vue'),
/* no children */
},
{
path: \\"/about\\",
name: \\"/about\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/about.vue'),
/* no children */
},
{
path: \\"/\\",
name: \\"/\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/index.vue'),
/* no children */
},
{
path: \\"/users.new\\",
name: \\"/users.new\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/users.new.vue'),
/* no children */
},
{
path: \\"/users\\",
name: \\"/users\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/users.vue'),
children: [
{
path: \\":id\\",
name: \\"/users/[id]\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/users/[id].vue'),
/* no children */
}
],
},
{
path: \\"/articles\\",
/* no name */
/* no component */
children: [
{
path: \\":id\\",
name: \\"/articles/[id]\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/articles/[id].vue'),
/* no children */
},
{
path: \\":slugs+\\",
name: \\"/articles/[slugs]+\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/articles/[slugs]+.vue'),
/* no children */
}
],
},
{
path: \\"/optional\\",
/* no name */
/* no component */
children: [
{
path: \\":doc?\\",
name: \\"/optional/[[doc]]\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/optional/[[doc]].vue'),
/* no children */
},
{
path: \\":docs*\\",
name: \\"/optional/[[docs]]+\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/optional/[[docs]]+.vue'),
/* no children */
}
],
},
{
path: \\"/nested\\",
/* no name */
/* no component */
children: [
{
path: \\"folder\\",
/* no name */
/* no component */
children: [
{
path: \\"\\",
name: \\"/nested/folder/\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/nested/folder/index.vue'),
/* no children */
},
{
path: \\"should\\",
/* no name */
/* no component */
children: [
{
path: \\"work\\",
/* no name */
/* no component */
children: [
{
path: \\"\\",
name: \\"/nested/folder/should/work/\\",
component: () => import('/Users/posva/unplugin-vue-router/e2e/fixtures/filenames/routes/nested/folder/should/work/index.vue'),
/* no children */
}
],
}
],
}
],
}
],
}
]"
`;
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions e2e/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { join } from 'path'
import { expect, it } from 'vitest'
import { createRoutesContext } from '../src/core/context'
import { DEFAULT_OPTIONS } from '../src/options'
import { fileURLToPath, URL } from 'url'

const __dirname = fileURLToPath(new URL('./', import.meta.url))

/**
* This is a simple full test to check that all filenames are valid in different environment (windows, mac, linux).
*/

it('generates the routes', async () => {
const context = createRoutesContext({
...DEFAULT_OPTIONS,
// dts: join(__dirname, './__types.d.ts'),
dts: false,
logs: false,
routesFolder: join(__dirname, './fixtures/filenames/routes'),
})

await context.scanPages()
expect(context.generateRoutes()).toMatchSnapshot()
})
8 changes: 7 additions & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import Vue from '@vitejs/plugin-vue'

export default defineConfig({
clearScreen: false,
plugins: [Vue({}), VueRouter(), Inspect()],
plugins: [
Vue({}),
VueRouter({
logs: true,
}),
Inspect(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
Expand Down
16 changes: 11 additions & 5 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export function createRoutesContext(options: Required<Options>) {

const routeTree = createPrefixTree(options)

function log(...args: any[]) {
if (options.logs) {
console.log(...args)
}
}

const resolvedRoutesFolder = resolve(root, options.routesFolder)
const serverWatcher = chokidar.watch(resolvedRoutesFolder, {
ignoreInitial: true,
Expand Down Expand Up @@ -54,7 +60,7 @@ export function createRoutesContext(options: Required<Options>) {
}

function addPage(path: string) {
console.log('added', path)
log('added', path)
routeTree.insert(
stripRouteFolder(path),
// './' + path
Expand All @@ -63,15 +69,15 @@ export function createRoutesContext(options: Required<Options>) {
}

function removePage(path: string) {
console.log('remove', path)
log('remove', path)
routeTree.remove(stripRouteFolder(path))
}

function setupWatcher() {
serverWatcher
.on('change', (path) => {
// TODO: parse defineRoute macro?
console.log('change', path)
log('change', path)
writeConfigFiles()
})
.on('add', (path) => {
Expand Down Expand Up @@ -186,8 +192,8 @@ export function createRouter(options) {

let lastDTS: string | undefined
async function _writeConfigFiles() {
console.log('writing')
logTree(routeTree)
log('writing')
logTree(routeTree, log)
if (dts) {
const content = generateDTS()
if (lastDTS !== content) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export type LiteralStringUnion<LiteralType, BaseType extends string = string> =
| LiteralType
| (BaseType & Record<never, never>)

export function logTree(tree: TreeLeaf) {
console.log(printTree(tree))
export function logTree(tree: TreeLeaf, log: (str: string) => any) {
log(printTree(tree))
}

const MAX_LEVEL = 1000
Expand Down
8 changes: 7 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ export interface Options {
/**
* Should generate d.ts files. Defaults to `true` if `typescript` is installed.
*/
dts?: boolean
dts?: boolean | string

/**
* Allows inspection by vite-plugin-inspect by not adding the leading `\0` to the id of virtual modules.
* @internal
*/
_inspect?: boolean

/**
* Activates debug logs.
*/
logs?: boolean
}

export const DEFAULT_OPTIONS: Required<Options> = {
Expand All @@ -45,5 +50,6 @@ export const DEFAULT_OPTIONS: Required<Options> = {
importMode: 'async',
root: process.cwd(),
dts: isPackageExists('typescript'),
logs: false,
_inspect: false,
}

0 comments on commit f6c33bd

Please sign in to comment.