Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 21, 2020
1 parent 91d76bf commit e718bd5
Show file tree
Hide file tree
Showing 9 changed files with 3,281 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
TODOs.md
playground
*.log
test/temp
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dev-server": "tsc -w --p src/server",
"build": "tsc -p src/client && tsc -p src/server",
"lint": "prettier --write --parser typescript \"src/**/*.ts\"",
"test": "yarn build && jest",
"prepublishOnly": "tsc"
},
"gitHooks": {
Expand All @@ -29,6 +30,9 @@
"git add"
]
},
"jest": {
"watchPathIgnorePatterns": ["<rootDir>/test/temp"]
},
"dependencies": {
"@babel/parser": "^7.9.4",
"@vue/compiler-sfc": "^3.0.0-beta.2",
Expand All @@ -43,9 +47,12 @@
"@types/node": "^13.13.1",
"@types/serve-handler": "^6.1.0",
"@types/ws": "^7.2.4",
"execa": "^4.0.0",
"jest": "^25.4.0",
"lint-staged": "^10.1.6",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.4",
"puppeteer": "^3.0.0",
"typescript": "^3.8.3",
"yorkie": "^2.0.0"
}
Expand Down
9 changes: 6 additions & 3 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs } from 'fs'
import path from 'path'
import http from 'http'
import http, { Server } from 'http'
import url from 'url'
import WebSocket from 'ws'
import serve from 'serve-handler'
Expand All @@ -12,9 +12,12 @@ import { rewrite } from './moduleRewriter'

export interface ServerConfig {
port?: number
cwd?: string
}

export async function createServer({ port = 3000 }: ServerConfig = {}) {
export async function createServer({ port = 3000 }: ServerConfig = {}): Promise<
Server
> {
const hmrClientCode = await fs.readFile(
path.resolve(__dirname, '../client/client.js')
)
Expand Down Expand Up @@ -83,7 +86,7 @@ export async function createServer({ port = 3000 }: ServerConfig = {}) {

server.on('listening', () => {
console.log(`Running at http://localhost:${port}`)
resolve()
resolve(server)
})

server.listen(port)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/Child.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div class="child">This is child</div>
</template>
17 changes: 17 additions & 0 deletions test/fixtures/Comp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<button @click="count++">{{ count }}</button>
<Child/>
</template>

<script>
import Child from './Child.vue'
export default {
components: { Child },
setup() {
return {
count: 0
}
}
}
</script>
2 changes: 2 additions & 0 deletions test/fixtures/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div id="app"></div>
<script type="module" src="/main.js"></script>
4 changes: 4 additions & 0 deletions test/fixtures/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createApp } from 'vue'
import Comp from './Comp.vue'

createApp(Comp).mount('#app')
78 changes: 78 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const fs = require('fs').promises
const path = require('path')
const execa = require('execa')
const puppeteer = require('puppeteer')

const timeout = (n) => new Promise((r) => setTimeout(r, n))

const fixtureDir = path.join(__dirname, 'fixtures')
const tempDir = path.join(__dirname, 'temp')
let server
let browser

jest.setTimeout(100000)

beforeAll(async () => {
await fs.rmdir(tempDir, { recursive: true })
await fs.mkdir(tempDir)
for (const file of await fs.readdir(fixtureDir)) {
await fs.copyFile(
path.join(__dirname, 'fixtures', file),
path.join(tempDir, file)
)
}
})

afterAll(async () => {
await browser.close()
await fs.rmdir(tempDir, { recursive: true })
server.kill('SIGTERM', {
forceKillAfterTimeout: 2000
})
})

test('test', async () => {
server = execa(path.resolve(__dirname, '../bin/vds.js'), {
cwd: tempDir
})
await new Promise((resolve) => {
server.stdout.on('data', (data) => {
if (data.toString().match('Running')) {
resolve()
}
})
})

browser = await puppeteer.launch()

const page = await browser.newPage()
await page.goto('http://localhost:3000')

// test nested components rendering
const button = await page.$('button')
expect(await button.evaluate((b) => b.textContent)).toBe('0')
const child = await page.$('.child')
expect(await child.evaluate((e) => e.textContent)).toBe('This is child')

// test interaction
await button.click()
expect(await button.evaluate((b) => b.textContent)).toBe('1')

// test HMR
const compPath = path.join(tempDir, 'Comp.vue')
const content = await fs.readFile(compPath, 'utf-8')
await fs.writeFile(
compPath,
content.replace('{{ count }}', 'count is {{ count }}')
)
// poll until it updates
const maxTries = 10
for (let tries = 0; tries < maxTries; tries++) {
const text = await button.evaluate((b) => b.textContent)
if (text === 'count is 1' || tries === maxTries - 1) {
expect(text).toBe('count is 1')
} else {
await timeout(200)
}
}
})
Loading

0 comments on commit e718bd5

Please sign in to comment.