Files
hiring-test-one/test/cli.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-05-23 22:08:37 +01:00
const Tom = require('test-runner').Tom
2017-07-07 23:52:48 +01:00
const a = require('assert')
const WsCli = require('../lib/cli-app')
2019-05-23 22:08:37 +01:00
const fetch = require('node-fetch')
2017-07-07 23:52:48 +01:00
2019-05-23 22:08:37 +01:00
const tom = module.exports = new Tom('cli')
2017-07-07 23:52:48 +01:00
2019-05-23 22:08:37 +01:00
tom.test('cli.run', async function () {
2017-07-07 23:52:48 +01:00
const port = 7500 + this.index
const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--port', `${port}` ]
const cli = new WsCli({ logError: function () {} })
const server = cli.start()
2017-07-07 23:52:48 +01:00
process.argv = origArgv
2019-06-02 23:15:15 +01:00
const response = await fetch(`http://127.0.0.1:${port}/package.json`)
2017-07-07 23:52:48 +01:00
server.close()
2019-05-23 22:08:37 +01:00
a.strictEqual(response.status, 200)
2017-07-07 23:52:48 +01:00
})
2019-05-23 22:08:37 +01:00
tom.test('cli.run: bad option', async function () {
2017-07-07 23:52:48 +01:00
const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--should-fail' ]
2018-01-17 22:42:46 +00:00
const exitCode = process.exitCode
const cli = new WsCli({ logError: function () {} })
const server = cli.start()
2018-01-17 22:42:46 +00:00
if (!exitCode) process.exitCode = 0
2017-07-07 23:52:48 +01:00
process.argv = origArgv
a.strictEqual(server, undefined)
})
2019-05-23 22:08:37 +01:00
tom.test('cli.run: --help', async function () {
2017-07-07 23:52:48 +01:00
const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--help' ]
const cli = new WsCli({ log: function () {} })
cli.start()
2017-07-07 23:52:48 +01:00
process.argv = origArgv
})
2019-05-23 22:08:37 +01:00
tom.test('cli.run: --version', async function () {
2017-07-07 23:52:48 +01:00
const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--version' ]
let logMsg = ''
const cli = new WsCli({ log: function (msg) { logMsg = msg } })
cli.start()
const pkg = require('../package.json')
a.strictEqual(logMsg.trim(), pkg.version)
2017-07-07 23:52:48 +01:00
process.argv = origArgv
})
tom.test('cli.run: default-stack', async function () {
2017-07-07 23:52:48 +01:00
const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--default-stack' ]
let logMsg = ''
const cli = new WsCli({ log: function (msg) { logMsg = msg } })
cli.start()
2019-06-02 23:15:15 +01:00
a.ok(/lws-static/.test(logMsg))
2017-07-07 23:52:48 +01:00
process.argv = origArgv
})