Skip to content

Commit

Permalink
wip(vitest-migration): ssr tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 26, 2023
1 parent 681775a commit f0d78e8
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 98 deletions.
4 changes: 0 additions & 4 deletions packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import {
compileStyle,
compileStyleAsync,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
ComponentInternalOptions,
Component,
ConcreteComponent,
InternalRenderFunction,
LifecycleHooks
InternalRenderFunction
} from './component'
import {
isFunction,
Expand Down Expand Up @@ -72,6 +71,7 @@ import {
softAssertCompatEnabled
} from './compat/compatConfig'
import { OptionMergeFunction } from './apiCreateApp'
import { LifecycleHooks } from './enums'

/**
* Interface for declaring custom options.
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
getCurrentInstance,
SetupContext,
ComponentInternalInstance,
LifecycleHooks,
currentInstance,
getComponentName,
ComponentOptions
Expand Down Expand Up @@ -44,6 +43,7 @@ import { ComponentRenderContext } from '../componentPublicInstance'
import { devtoolsComponentAdded } from '../devtools'
import { isAsyncWrapper } from '../apiAsyncComponent'
import { isSuspense } from './Suspense'
import { LifecycleHooks } from '../enums'

type MatchPattern = string | RegExp | (string | RegExp)[]

Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { vi } from 'vitest'
import {
createApp,
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrAttrFallthrough.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp } from 'vue'
import { renderToString } from '../src/renderToString'

Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrCompilerOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp } from 'vue'
import { renderToString } from '../src/renderToString'

Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrDirectives.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { renderToString } from '../src/renderToString'
import {
createApp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp, createVNode } from 'vue'
import { renderToString } from '../src/renderToString'

Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrInterpolate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { ssrInterpolate } from '../src/helpers/ssrInterpolate'
import { escapeHtml } from '@vue/shared'

Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import {
ssrRenderAttrs,
ssrRenderClass,
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrRenderList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { ssrRenderList } from '../src/helpers/ssrRenderList'

describe('ssr: renderList', () => {
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrScopeId.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp, h, mergeProps, withCtx } from 'vue'
import { renderToString } from '../src/renderToString'
import { ssrRenderComponent, ssrRenderAttrs, ssrRenderSlot } from '../src'
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrSlot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp } from 'vue'
import { renderToString } from '../src/renderToString'

Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrSuspense.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { vi } from 'vitest'
import { createApp, h, Suspense } from 'vue'
import { renderToString } from '../src/renderToString'
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrTeleport.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp, h, Teleport } from 'vue'
import { renderToString } from '../src/renderToString'
import { renderToSimpleStream } from '../src/renderToStream'
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/ssrVModelHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import {
ssrRenderDynamicModel,
ssrGetDynamicModelProps
Expand Down
4 changes: 0 additions & 4 deletions packages/server-renderer/__tests__/webStream.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment node
*/

import { createApp, h, defineAsyncComponent } from 'vue'
import { ReadableStream, TransformStream } from 'stream/web'
import { pipeToWebWritable, renderToWebStream } from '../src'
Expand Down
11 changes: 10 additions & 1 deletion packages/server-renderer/src/helpers/ssrCompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { extend, generateCodeFrame, isFunction, NO } from '@vue/shared'
import { CompilerError, CompilerOptions } from '@vue/compiler-core'
import { PushFn } from '../render'

import * as Vue from 'vue'
import * as helpers from '../internal'

type SSRRenderFunction = (
context: any,
push: PushFn,
Expand All @@ -16,6 +19,7 @@ export function ssrCompile(
template: string,
instance: ComponentInternalInstance
): SSRRenderFunction {
// TODO: this branch should now work in ESM builds, enable it in a minor
if (!__NODE_JS__) {
throw new Error(
`On-the-fly template compilation is not supported in the ESM build of ` +
Expand Down Expand Up @@ -76,5 +80,10 @@ export function ssrCompile(
}

const { code } = compile(template, finalCompilerOptions)
return (compileCache[cacheKey] = Function('require', code)(require))
const requireMap = {
vue: Vue,
'vue/server-renderer': helpers
}
const fakeRequire = (id: 'vue' | 'vue/server-renderer') => requireMap[id]
return (compileCache[cacheKey] = Function('require', code)(fakeRequire))
}
25 changes: 1 addition & 24 deletions packages/server-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,4 @@ export {
} from './renderToStream'

// internal runtime helpers
export { renderVNode as ssrRenderVNode } from './render'
export { ssrRenderComponent } from './helpers/ssrRenderComponent'
export { ssrRenderSlot, ssrRenderSlotInner } from './helpers/ssrRenderSlot'
export { ssrRenderTeleport } from './helpers/ssrRenderTeleport'
export {
ssrRenderClass,
ssrRenderStyle,
ssrRenderAttrs,
ssrRenderAttr,
ssrRenderDynamicAttr
} from './helpers/ssrRenderAttrs'
export { ssrInterpolate } from './helpers/ssrInterpolate'
export { ssrRenderList } from './helpers/ssrRenderList'
export { ssrRenderSuspense } from './helpers/ssrRenderSuspense'
export { ssrGetDirectiveProps } from './helpers/ssrGetDirectiveProps'
export { includeBooleanAttr as ssrIncludeBooleanAttr } from '@vue/shared'

// v-model helpers
export {
ssrLooseEqual,
ssrLooseContain,
ssrRenderDynamicModel,
ssrGetDynamicModelProps
} from './helpers/ssrVModelHelpers'
export * from './internal'
25 changes: 25 additions & 0 deletions packages/server-renderer/src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// internal runtime helpers
export { renderVNode as ssrRenderVNode } from './render'
export { ssrRenderComponent } from './helpers/ssrRenderComponent'
export { ssrRenderSlot, ssrRenderSlotInner } from './helpers/ssrRenderSlot'
export { ssrRenderTeleport } from './helpers/ssrRenderTeleport'
export {
ssrRenderClass,
ssrRenderStyle,
ssrRenderAttrs,
ssrRenderAttr,
ssrRenderDynamicAttr
} from './helpers/ssrRenderAttrs'
export { ssrInterpolate } from './helpers/ssrInterpolate'
export { ssrRenderList } from './helpers/ssrRenderList'
export { ssrRenderSuspense } from './helpers/ssrRenderSuspense'
export { ssrGetDirectiveProps } from './helpers/ssrGetDirectiveProps'
export { includeBooleanAttr as ssrIncludeBooleanAttr } from '@vue/shared'

// v-model helpers
export {
ssrLooseEqual,
ssrLooseContain,
ssrRenderDynamicModel,
ssrGetDynamicModelProps
} from './helpers/ssrVModelHelpers'
24 changes: 14 additions & 10 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import { fileURLToPath } from 'node:url'
import { readdirSync } from 'node:fs'

const resolve = p =>
path.resolve(fileURLToPath(import.meta.url), `../packages/${p}/src`)
path.resolve(fileURLToPath(import.meta.url), `../packages/${p}/src/index.ts`)
const dirs = readdirSync(new URL('./packages', import.meta.url))
const alias = {}

const alias = {
vue: resolve('vue'),
'vue/compiler-sfc': resolve('compiler-sfc'),
'vue/server-renderer': resolve('server-renderer'),
'@vue/compat': resolve('vue-compat')
}

for (const dir of dirs) {
alias[`@vue/${dir}`] = resolve(dir)
const key = `@vue/${dir}`
if (dir !== 'vue' && !(key in alias)) {
alias[key] = resolve(dir)
}
}

export default defineConfig({
Expand All @@ -28,13 +38,7 @@ export default defineConfig({
__COMPAT__: true
},
resolve: {
alias: {
...alias,
vue: resolve('vue'),
'vue/compiler-sfc': resolve('compiler-sfc'),
'vue/server-renderer': resolve('server-renderer'),
'@vue/compat': resolve('vue-compat')
}
alias
},
test: {
globals: true,
Expand Down

0 comments on commit f0d78e8

Please sign in to comment.