Skip to content

Commit

Permalink
feat: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jul 19, 2021
1 parent 5cddff9 commit 82618a0
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/playground/lib/__tests__/lib.spec.ts
@@ -1,4 +1,6 @@
import { isBuild } from 'testUtils'
import { isBuild, findAssetFile, testDir } from 'testUtils'
import path from 'path'
import fs from 'fs'

if (isBuild) {
test('es', async () => {
Expand All @@ -8,6 +10,15 @@ if (isBuild) {
test('umd', async () => {
expect(await page.textContent('.umd')).toBe('It works')
})

test('Library mode does not include `preload`', async () => {
expect(await page.textContent('.dynamic-import-message')).toBe('hello vite')
const code = fs.readFileSync(
path.join(testDir, 'dist/lib/dynamic-import-message.js'),
'utf-8'
)
expect(code).not.toMatch('__vitePreload')
})
} else {
test('dev', async () => {
expect(await page.textContent('.demo')).toBe('It works')
Expand Down
90 changes: 90 additions & 0 deletions packages/playground/lib/__tests__/serve.js
@@ -0,0 +1,90 @@
// @ts-check
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace
// the default e2e test serve behavior

const path = require('path')
const http = require('http')
const sirv = require('sirv')

const port = (exports.port = 9527)

/**
* @param {string} root
* @param {boolean} isBuildTest
*/
exports.serve = async function serve(root, isBuildTest) {
// build first

if (!isBuildTest) {
const { createServer } = require('vite')
process.env.VITE_INLINE = 'inline-serve'
let viteServer = await (
await createServer({
root: root,
logLevel: 'silent',
server: {
watch: {
usePolling: true,
interval: 100
},
host: true,
fs: {
strict: !isBuildTest
}
},
build: {
target: 'esnext'
}
})
).listen()
// use resolved port/base from server
const base = viteServer.config.base === '/' ? '' : viteServer.config.base
const url =
(global.viteTestUrl = `http://localhost:${viteServer.config.server.port}${base}`)
await page.goto(url)

return viteServer
} else {
const { build } = require('vite')
await build({
root,
logLevel: 'silent',
configFile: path.resolve(__dirname, '../vite.config.js')
})

await build({
root,
logLevel: 'silent',
configFile: path.resolve(__dirname, '../vite.dyimport.config.js')
})

// start static file server
const serve = sirv(path.resolve(root, 'dist'))
const httpServer = http.createServer((req, res) => {
if (req.url === '/ping') {
res.statusCode = 200
res.end('pong')
} else {
serve(req, res)
}
})

return new Promise((resolve, reject) => {
try {
const server = httpServer.listen(port, async () => {
await page.goto(`http://localhost:${port}`)
resolve({
// for test teardown
async close() {
await new Promise((resolve) => {
server.close(resolve)
})
}
})
})
} catch (e) {
reject(e)
}
})
}
}
7 changes: 7 additions & 0 deletions packages/playground/lib/index.dist.html
@@ -1,13 +1,20 @@
<!-- the production demo page, copied into dist/ -->
<div class="es"></div>
<div class="umd"></div>
<div class="dynamic-import-message"></div>

<script type="module">
import myLib from './my-lib-custom-filename.es.js'

myLib('.es')
</script>

<script type="module">
import message from './lib/dynamic-import-message.js'

message('.dynamic-import-message')
</script>

<script src="./my-lib-custom-filename.umd.js"></script>
<script>
MyLib('.umd')
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/lib/src/main2.js
@@ -0,0 +1,4 @@
export default async function message(sel) {
const message = await import('./message.js')
document.querySelector(sel).textContent = message.default
}
1 change: 1 addition & 0 deletions packages/playground/lib/src/message.js
@@ -0,0 +1 @@
export default 'hello vite'
18 changes: 18 additions & 0 deletions packages/playground/lib/vite.dyimport.config.js
@@ -0,0 +1,18 @@
const fs = require('fs')
const path = require('path')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
build: {
minify: false,
lib: {
entry: path.resolve(__dirname, 'src/main2.js'),
formats: ['es'],
name: 'message',
fileName: () => 'dynamic-import-message.js'
},
outDir: 'dist/lib'
}
}

0 comments on commit 82618a0

Please sign in to comment.