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 4cbf24c
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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('')
})
}
5 changes: 5 additions & 0 deletions packages/playground/fs-serve/entry.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = 'foo'
11 changes: 11 additions & 0 deletions packages/playground/fs-serve/package.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>
17 changes: 17 additions & 0 deletions packages/playground/fs-serve/root/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { dirname } from 'path'
import { defineConfig } from 'vite'

export default defineConfig({
server: {
fsServe: {
root: __dirname,
strict: true
},
hmr: {
overlay: false
}
},
define: {
ROOT: JSON.stringify(dirname(__dirname).replace(/\\/g, '/'))
}
})
3 changes: 3 additions & 0 deletions packages/playground/fs-serve/safe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"msg": "safe"
}
3 changes: 3 additions & 0 deletions packages/playground/fs-serve/unsafe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"msg": "unsafe"
}
6 changes: 5 additions & 1 deletion scripts/jestPerTestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ beforeAll(async () => {
return
}

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

const options: UserConfig = {
root: tempDir,
root: testCustomRoot,
logLevel: 'silent',
server: {
watch: {
Expand Down

0 comments on commit 4cbf24c

Please sign in to comment.