Skip to content

Commit

Permalink
test: add test for fs-serve
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 13, 2021
1 parent 95dbcf4 commit fbe1829
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
26 changes: 26 additions & 0 deletions packages/playground/fs-serve/__tests__/fs-serve.spec.ts
@@ -0,0 +1,26 @@
import { isBuild } from '../../testUtils'

const json = require('../safe.json')
const stringified = JSON.stringify(json)

if (!isBuild) {
test('default import', async () => {
expect(await page.textContent('.full')).toBe(stringified)
})

test('named import', async () => {
expect(await page.textContent('.named')).toBe(json.msg)
})

test('safe fetch', async () => {
expect(await page.textContent('.safe-fetch')).toBe(stringified)
})

test('unsafe fetch', async () => {
expect(await page.textContent('.unsafe-fetch')).toBe('')
})
} else {
test('dummy test to make jest happy', async () => {
// Your test suite must contain at least one test.
})
}
5 changes: 5 additions & 0 deletions packages/playground/fs-serve/entry.js
@@ -0,0 +1,5 @@
import { msg } from './nested/foo'

export const fullmsg = msg + 'bar'

document.querySelector('.nested-entry').textContent = fullmsg
1 change: 1 addition & 0 deletions packages/playground/fs-serve/nested/foo.js
@@ -0,0 +1 @@
export const msg = 'foo'
11 changes: 11 additions & 0 deletions packages/playground/fs-serve/package.json
@@ -0,0 +1,11 @@
{
"name": "test-fs-serve",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"serve": "vite preview"
}
}
39 changes: 39 additions & 0 deletions packages/playground/fs-serve/root/index.html
@@ -0,0 +1,39 @@
<h2>Normal Import</h2>
<pre class="full"></pre>
<pre class="named"></pre>

<h2>Safe Fetch</h2>
<pre class="safe-fetch"></pre>

<h2>Unsafe Fetch</h2>
<pre class="unsafe-fetch"></pre>

<h2>Nested Entry</h2>
<pre class="nested-entry"></pre>

<script type="module">
import '../entry'
import json, { msg } from '../safe.json'

text('.full', JSON.stringify(json))
text('.named', msg)

// imported before, should be treated as safe
fetch('/@fs' + ROOT + '/safe.json')
.then((r) => r.json())
.then((data) => {
text('.safe-fetch', JSON.stringify(data))
})

// not imported before, outside of root, treated as unsafe
fetch('/@fs' + ROOT + '/unsafe.json')
.catch((e) => console.error(e))
.then((r) => r.json())
.then((data) => {
text('.unsafe-fetch', JSON.stringify(data))
})

function text(sel, text) {
document.querySelector(sel).textContent = text
}
</script>
19 changes: 19 additions & 0 deletions packages/playground/fs-serve/root/vite.config.js
@@ -0,0 +1,19 @@
const path = require('path')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
server: {
fsServe: {
root: __dirname,
strict: true
},
hmr: {
overlay: false
}
},
define: {
ROOT: JSON.stringify(path.dirname(__dirname).replace(/\\/g, '/'))
}
}
3 changes: 3 additions & 0 deletions packages/playground/fs-serve/safe.json
@@ -0,0 +1,3 @@
{
"msg": "safe"
}
3 changes: 3 additions & 0 deletions packages/playground/fs-serve/unsafe.json
@@ -0,0 +1,3 @@
{
"msg": "unsafe"
}
13 changes: 9 additions & 4 deletions scripts/jestPerTestSetup.ts
Expand Up @@ -24,6 +24,7 @@ declare global {

let server: ViteDevServer | http.Server
let tempDir: string
let rootDir: string
let err: Error

const logs = ((global as any).browserLogs = [])
Expand Down Expand Up @@ -60,16 +61,20 @@ beforeAll(async () => {
}
})

// when `root` dir is present, use it as vite's root
let testCustomRoot = resolve(tempDir, 'root')
rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : tempDir

const testCustomServe = resolve(dirname(testPath), 'serve.js')
if (fs.existsSync(testCustomServe)) {
// test has custom server configuration.
const { serve } = require(testCustomServe)
server = await serve(tempDir, isBuildTest)
server = await serve(rootDir, isBuildTest)
return
}

const options: UserConfig = {
root: tempDir,
root: rootDir,
logLevel: 'silent',
server: {
watch: {
Expand Down Expand Up @@ -128,7 +133,7 @@ afterAll(async () => {

function startStaticServer(): Promise<string> {
// check if the test project has base config
const configFile = resolve(tempDir, 'vite.config.js')
const configFile = resolve(rootDir, 'vite.config.js')
let config: UserConfig
try {
config = require(configFile)
Expand All @@ -142,7 +147,7 @@ function startStaticServer(): Promise<string> {
}

// start static file server
const serve = sirv(resolve(tempDir, 'dist'))
const serve = sirv(resolve(rootDir, 'dist'))
const httpServer = (server = http.createServer((req, res) => {
if (req.url === '/ping') {
res.statusCode = 200
Expand Down

0 comments on commit fbe1829

Please sign in to comment.