Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Add try...catch block to catch error
Browse files Browse the repository at this point in the history
  • Loading branch information
thangngoc89 committed Sep 16, 2016
1 parent f4b13e4 commit 41b25c3
Showing 1 changed file with 67 additions and 54 deletions.
121 changes: 67 additions & 54 deletions __tests__/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,70 @@ const execOpts = { cwd: target }
const phenomic = "node ./node_modules/.bin/phenomic"
const timing = process.env.CI ? 10000 : 5000

describe("Integration: CLI", () => {
beforeEach(() => {
process.env.BABEL_ENV = "development"
beforeEach(() => {
process.env.BABEL_ENV = "development"
})

it("should throw if a CLI flag is NOT recognized", () => {
return new Promise((resolve, reject) => {
const child = exec(
`${ phenomic } start --open=false --lol`, execOpts,
(err) => {
if (err && !err.killed) {
clearTimeout(timeout)
expect(err.message.indexOf("Unknown argument") > -1).toBeTruthy()
resolve()
}
}
)

const timeout = setTimeout(() => {
child.kill()
reject("Test didn't finish before timeout")
}, timing)
})
})

it("should throw if a CLI flag is NOT recognized", () => {
return new Promise((resolve, reject) => {
const child = exec(
`${ phenomic } start --open=false --lol`, execOpts,
(err) => {
if (err && !err.killed) {
clearTimeout(timeout)
expect(err.message.indexOf("Unknown argument") > -1).toBeTruthy()
resolve()
}
it("should NOT throw if a CLI flag is recognized", () => {
return new Promise((resolve, reject) => {
const child = exec(
`${ phenomic } start --open=false --devPort=4000`, execOpts,

// should die quickly...
(err) => {
if (err && !err.killed) {
clearTimeout(timeout)
reject(err)
}
)
}
)

const timeout = setTimeout(() => {
// ...or be ok quickly
// we assume it's ok and kill the process
// we don't need the actual build
const timeout = setTimeout(() => {
try {
child.kill()
reject("Test didn't finish before timeout")
}, timing)
})
}
catch (err) {
console.error(err)
}
resolve()
}, timing)
})
})

it("should NOT throw if port is used", () => {
return new Promise((resolve, reject) => {
const app = require("express")()
const server = app.listen(3333, (err) => {
if (err) {
reject(err)
}

it("should NOT throw if a CLI flag is recognized", () => {
return new Promise((resolve, reject) => {
const child = exec(
`${ phenomic } start --open=false --devPort=4000`, execOpts,
`${ phenomic } start --open=false --devPort=3333`, execOpts,

// should die quickly...
(err) => {
if (err && !err.killed) {
clearTimeout(timeout)
Expand All @@ -47,41 +80,21 @@ describe("Integration: CLI", () => {
}
)

// ...or be ok quickly
// we assume it's ok and kill the process
// we don't need the actual build
const timeout = setTimeout(() => {
child.kill()
resolve()
}, timing)
})
})

it("should NOT throw if port is used", () => {
return new Promise((resolve, reject) => {
const app = require("express")()
const server = app.listen(3333, (err) => {
if (err) {
reject(err)
}

const child = exec(
`${ phenomic } start --open=false --devPort=3333`, execOpts,

(err) => {
if (err && !err.killed) {
clearTimeout(timeout)
reject(err)
}
}
)

const timeout = setTimeout(() => {
try {
child.kill()
}
catch (err) {
console.error(err)
}
try {
server.close()
resolve()
}, timing * 2)
})
}
catch (err) {
console.error(err)
}
resolve()
}, timing * 2)
})
})
})

0 comments on commit 41b25c3

Please sign in to comment.