提交 12deab2f 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#40 fix typescript unit tests with ava

上级 09099fc9
......@@ -16,19 +16,20 @@
},
"scripts": {
"ts-node": "ts-node",
"build": "tsc",
"build": "tsc && cp package.json dist/ && cp src/puppet-web/*.js dist/src/puppet-web/",
"doctor": "ts-node bin/doctor",
"clean": "rm -fr dist/*",
"lint": "npm run eslint && npm run tslint",
"eslint": "eslint \"{bin,example,src,test}/**/*.js\"",
"tslint": "tslint \"{bin,example,src,test}/**/*.ts\" && tsc --noEmit",
"sloc": "sloc . --details --format cli-table --keys total,source,comment --exclude \"node_modules|doc\" && sloc . --exclude \"node_modules|doc\"",
"pretest": "npm run lint",
"pretest": "npm run lint && npm run clean && npm run build",
"test": "npm run test:phantomjs && npm run test:chrome",
"posttest": "npm run sloc",
"test:phantomjs": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs node_modules/.bin/ava --timeout=10m \"{src,test}/**/*.spec.js\"",
"test:chrome": "cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome node_modules/.bin/ava --timeout=10m \"{src,test}/**/*.spec.js\"",
"posttest": "npm run clean && npm run sloc",
"test:phantomjs": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=10m \"dist/{src,test}/**/*.spec.js\"",
"test:chrome": "cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome ava --timeout=10m \"dist/{src,test}/**/*.spec.js\"",
"testdev": "cross-env LC_ALL=C WECHATY_LOG=silly ava --ext ts --serial --verbose --fail-fast --timeout=3m",
"testdist": "ava --verbose --fail-fast --timeout=3m",
"ava": "cross-env LC_ALL=C WECHATY_LOG=verbose ts-node node_modules/.bin/ava \"{src,test}/**/*.spec.js\"",
"start": "ts-node bin/client",
"dev": "ts-node dev.ts",
......@@ -85,6 +86,7 @@
},
"dependencies": {
"arrify": "^1.0.1",
"ava": "^0.16.0",
"body-parser": "^1.15.2",
"brolog": "^0.3.10",
"chromedriver": "^2.23.1",
......
......@@ -130,7 +130,7 @@ export class Server extends EventEmitter {
this.emit('connection', client)
client.on('disconnect', e => {
log.silly('PuppetWebServer', 'initEventsFromClient() on(discohnnect) socket.io disconnect: %s', e)
log.silly('PuppetWebServer', 'initEventsFromClient() on(disconnect) socket.io disconnect: %s', e)
// 1. Browser reload / 2. Lost connection(Bad network)
this.socketClient = null
this.emit('disconnect', e)
......
......@@ -110,7 +110,7 @@ export class Wechaty extends EventEmitter implements Sayable {
* This throws if any accessibility checks fail, and does nothing otherwise.
*/
// fs.accessSync(dotGitPath, fs.F_OK)
fs.statSync(dotGitPath) // .isDirectory()
fs.statSync(dotGitPath).isDirectory()
const ss = require('child_process')
.spawnSync(gitLogCmd, gitLogArgs, { cwd: __dirname })
......
......@@ -16,7 +16,8 @@ if (Config.isDocker) {
// const n = execSync('ps a | grep Xvfb | grep -v grep | wc -l').toString().replace(/\n/, '', 'g')
// t.is(parseInt(n), 1, 'should has Xvfb started')
t.notThrows(() => {
fs.accessSync(Config.CMD_CHROMIUM, fs['X_OK'])
// fs.accessSync(Config.CMD_CHROMIUM, fs['X_OK'])
fs.statSync(Config.CMD_CHROMIUM).isFile()
}, 'should exist xvfb-chrome exectable')
})
......
......@@ -10,8 +10,6 @@ import {
, PuppetWeb
} from '../../src/puppet-web/'
// import util from 'util'
// const PORT = process.env.WECHATY_PORT || 58788
const PROFILE = 'unit-test-session.wechaty.json'
......@@ -21,19 +19,11 @@ test('Puppet Web Event smoking test', async t => {
let pw = new PuppetWeb({profile: PROFILE})
t.truthy(pw, 'should instantiated a PuppetWeb')
// co(function* () {
await pw.init()
t.pass('should be inited')
await Event.onBrowserDead.call(pw, 'event unit test')
t.pass('should finish onBrowserDead event process')
await pw.quit()
// })
// .catch(e => t.fail(e)) // Reject
// .then(r => { // Finally
// pw.quit()
// .then(_ => t.end())
// })
// .catch(e => t.fail(e)) // Exception
await pw.init()
t.pass('should be inited')
await Event.onBrowserDead.call(pw, 'event unit test')
t.pass('should finish onBrowserDead event process')
await pw.quit()
})
import { test } from 'ava'
import {
PuppetWeb
Config
, PuppetWeb
, Message
, log
} from '../../'
......@@ -12,13 +13,13 @@ import {
// import { spy } from 'sinon'
test('Puppet Web Self Message Identification', t => {
test.skip('@deprecated Puppet Web Self Message Identification', t => {
const p = new PuppetWeb()
t.truthy(p, 'should instantiated a PuppetWeb')
const EXPECTED_USER_ID = 'zixia'
const m = new Message()
m.set('from', EXPECTED_USER_ID)
m.from(EXPECTED_USER_ID)
p.userId = EXPECTED_USER_ID
t.truthy(p.self(m), 'should identified self for message which from is self')
})
......@@ -32,6 +33,8 @@ test.serial('PuppetWeb login/logout events', async t => {
let pw = new PuppetWeb()
t.truthy(pw, 'should instantiated a PuppetWeb')
Config.puppetInstance(pw)
await pw.init()
t.pass('should be inited')
t.is(pw.logined() , false , 'should be not logined')
......@@ -57,6 +60,8 @@ test.serial('PuppetWeb server/browser communication', async t => {
let pw = new PuppetWeb()
t.truthy(pw, 'should instantiated a PuppetWeb')
Config.puppetInstance(pw)
const EXPECTED_DING_DATA = 'dingdong'
await pw.init()
......
......@@ -6,7 +6,8 @@ import * as sinon from 'sinon'
import {
// PuppetWeb
// , Message
log
UtilLib
, log
} from '../../'
import {
......@@ -14,10 +15,9 @@ import {
Server
} from '../../src/puppet-web/'
const PORT = 48788
test('PuppetWebServer basic tests', async t => {
const s = new Server(PORT)
const port = await UtilLib.getPort(18788)
const s = new Server(port)
t.is(typeof s, 'object', 'PuppetWebServer instance created')
let httpsServer: https.Server
......@@ -62,12 +62,14 @@ test('PuppetWebServer basic tests', async t => {
})
test('PuppetWebServer smoke testing', async t => {
const server = new Server(PORT)
const port = await UtilLib.getPort(18788)
const server = new Server(port)
t.truthy(server, 'new server instance')
// co(function* () {
const retInit = await server.init()
t.truthy(retInit, 'server:' + PORT + ' inited')
t.truthy(retInit, 'server:' + port + ' inited')
const retHttps = await dingHttps()
t.is(retHttps , 'dong', 'ding https got dong')
......@@ -88,7 +90,7 @@ test('PuppetWebServer smoke testing', async t => {
//////////////////////////////////////////
function dingHttps() {
const options = require('url').parse(`https://localhost:${PORT}/ding`)
const options = require('url').parse(`https://localhost:${port}/ding`)
options.rejectUnauthorized = false // permit self-signed CA
return new Promise((resolve, reject) => {
......
......@@ -120,17 +120,16 @@ test('Room smoking test', async t => {
})
test('Room static method', async t => {
const room = await Room.find({
topic: 'xxx'
})
t.truthy(room === null, 'should return null before login')
try {
await Room.find({ topic: 'xxx' })
t.fail('should throw but not')
} catch (e) {
t.pass('should throw before login or not found')
}
const roomList = await Room.findAll({
topic: 'yyy'
})
t.is(roomList.length, 0, 'should return empty array before login')
// t.end()
})
......@@ -20,7 +20,6 @@ import {
* there will have race conditions for the conflict of `getBrowserPids()`
*/
test.serial('WebDriver process create & quit test', async t => {
// co(function* () {
const b = new PuppetWebBrowser()
t.truthy(b, 'should instanciate a browser')
......@@ -43,12 +42,6 @@ test.serial('WebDriver process create & quit test', async t => {
t.is(pids.length, 0, 'no driver process after quit')
}
return
// })
// .catch(e => t.fail(e))
// .then(_ => t.end())
// return
})
test.serial('WebDriver smoke testing', async t => {
......@@ -61,48 +54,40 @@ test.serial('WebDriver smoke testing', async t => {
let driver // for help function `execute`
// co(function* () {
const m = (await wb.getBrowserPids()).length
t.is(m, 0, 'should has no browser process before get()')
const m = (await wb.getBrowserPids()).length
t.is(m, 0, 'should has no browser process before get()')
driver = await wb.initDriver()
t.truthy(driver, 'should init driver success')
driver = await wb.initDriver()
t.truthy(driver, 'should init driver success')
const injectio = bridge.getInjectio()
t.truthy(injectio.length > 10, 'should got injectio script')
const injectio = bridge.getInjectio()
t.truthy(injectio.length > 10, 'should got injectio script')
// XXX: if get rid of this dummy,
// driver.get() will fail due to cant start phantomjs process
// 20160828 fixed in new version of selenium webdriver
// await Promise.resolve()
// XXX: if get rid of this dummy,
// driver.get() will fail due to cant start phantomjs process
// 20160828 fixed in new version of selenium webdriver
// await Promise.resolve()
await driver.get('https://wx.qq.com/')
t.pass('should open wx.qq.com')
await driver.get('https://wx.qq.com/')
t.pass('should open wx.qq.com')
const n = (await wb.getBrowserPids()).length
// console.log(n)
// await new Promise((resolve) => {
// setTimeout(() => {
// resolve()
// }, 3000)
// })
t.truthy(n > 0, 'should exist browser process after get()')
const n = (await wb.getBrowserPids()).length
// console.log(n)
// await new Promise((resolve) => {
// setTimeout(() => {
// resolve()
// }, 3000)
// })
t.truthy(n > 0, 'should exist browser process after get()')
const retAdd = await driverExecute('return 1+1')
t.is(retAdd, 2, 'should return 2 for execute 1+1 in browser')
const retAdd = await driverExecute('return 1+1')
t.is(retAdd, 2, 'should return 2 for execute 1+1 in browser')
const retInject = await driverExecute(injectio, 8788)
t.truthy(retInject, 'should return a object contains status of inject operation')
t.is(retInject.code, 200, 'should got code 200 for a success wechaty inject')
const retInject = await driverExecute(injectio, 8788)
t.truthy(retInject, 'should return a object contains status of inject operation')
t.is(retInject.code, 200, 'should got code 200 for a success wechaty inject')
await wb.quit()
// })
// .catch(e => t.fail('promise rejected. e:' + e)) // Rejected
// .then(_ => { // Finally
// wb.quit()
// .then(_ => t.end())
// })
// .catch(e => t.fail(e)) // Exception
await wb.quit()
return
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册