Skip to content

Commit

Permalink
test: update test code
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuangzhang committed Nov 1, 2021
1 parent b2657ab commit fe60643
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
9 changes: 9 additions & 0 deletions packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Expand Up @@ -4,6 +4,15 @@ import fetch from 'node-fetch'

const url = `http://localhost:${port}`

test('/require', async () => {
await page.goto(url + '/require')
expect(await page.textContent('h1')).toMatch('foo')

// raw http request
const envHtml = await (await fetch(url + '/require')).text()
expect(envHtml).toMatch('foo')
})

test('/about', async () => {
await page.goto(url + '/about')
expect(await page.textContent('h1')).toMatch('About')
Expand Down
9 changes: 0 additions & 9 deletions packages/playground/ssr-vue/src/entry-server.js
@@ -1,15 +1,6 @@
import { createApp } from './main'
import { renderToString } from 'vue/server-renderer'
import path from 'path'
import assert from 'assert'

// test require relative path
const { foo } = require('../config')
assert.strictEqual(foo, 'foo')
// test require bare module
const serverRenderer = require('vue/server-renderer')
// test require and import result point at the same object
assert.strictEqual(serverRenderer.renderToString, renderToString)

export async function render(url, manifest, rootDir) {
const { app, router } = createApp()
Expand Down
28 changes: 28 additions & 0 deletions packages/playground/ssr-vue/src/pages/Require.vue
@@ -0,0 +1,28 @@
<template>
<h1>{{foo}}</h1>
</template>

<script>
export default {
async setup() {
if (typeof window === 'undefined') {
// code execute in server side
const { resolve } = require('path')
const cwd = process.env.JEST_WORKER_ID !== undefined ? resolve(process.cwd(), './packages/playground/ssr-vue') : process.cwd()
const { foo } = require(resolve(cwd, './config'))
return {
foo: foo
}
}
return {
foo: 'foo'
}
}
}
</script>

<style scoped>
h1 {
color: red;
}
</style>
3 changes: 1 addition & 2 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -122,9 +122,8 @@ async function instantiateModule(
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
}
const ssrRequire = (dep: string) => {
const { dirname } = path
return require(require.resolve(dep, {
paths: [dirname(mod.file!)]
paths: [path.dirname(mod.file!)]
}))
}

Expand Down

0 comments on commit fe60643

Please sign in to comment.