Skip to content

Commit

Permalink
Create handler test file and test promise cases (#346)
Browse files Browse the repository at this point in the history
* Create handler test file and test promise cases

* Change XO rule of ava importing as test and add rewire and sinon as dependencies

* Complete 100% of coverage reports

* Add fixtures for handler tests

* Remove package-lock to resolve git conflicts
  • Loading branch information
fmiras authored and leo committed Mar 28, 2018
1 parent 51735c4 commit 888b46e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -18,6 +18,7 @@
],
"extends": "prettier",
"rules": {
"ava/use-test": 0,
"unicorn/no-process-exit": 0
}
},
Expand Down Expand Up @@ -51,6 +52,8 @@
"request": "2.83.0",
"request-promise": "4.2.2",
"resumer": "0.0.0",
"rewire": "3.0.2",
"sinon": "4.4.3",
"test-listen": "1.0.2",
"then-sleep": "1.0.1",
"xo": "0.18.2"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/babel-promise-export.js
@@ -0,0 +1 @@
module.exports = { default: (async () => () => 'test')() }
1 change: 1 addition & 0 deletions test/fixtures/native-promise-export.js
@@ -0,0 +1 @@
module.exports = (async () => () => 'test')()
1 change: 1 addition & 0 deletions test/fixtures/regular-object.js
@@ -0,0 +1 @@
module.exports = {}
1 change: 1 addition & 0 deletions test/fixtures/syntax-error.js
@@ -0,0 +1 @@
throw new SyntaxError('async')
62 changes: 62 additions & 0 deletions test/handler.js
@@ -0,0 +1,62 @@
// Native
const path = require('path')

// Packages
const { serial: test } = require('ava')
const sinon = require('sinon')
const rewire = require('rewire')

const handle = rewire('../lib/handler')

test.beforeEach(() => {
sinon.stub(process, 'exit')
process.exit.callsFake(() => {
// Throw error to finish the execution of the code
throw new Error()
})
})

test.afterEach(() => {
process.exit.restore()
try {
String.prototype.split.restore()
} catch (err) {}
})

test('handle a PromiseInstance', async t => {
const file = path.resolve('test/fixtures/native-promise-export')
const result = await handle(file)
t.is(typeof result, 'function')
})

test('handle an object that holds a PromiseInstance', async t => {
const file = path.resolve('test/fixtures/babel-promise-export')
const result = await handle(file)
t.is(typeof result, 'function')
})

test('process.exit when handling an invalid object', async t => {
const file = path.resolve('test/fixtures/regular-object')
const promise = handle(file)
await t.throws(promise)
t.is(process.exit.getCall(0).args[0], 1)
})

test('process.exit when handling and inexisting file', async t => {
const file = path.resolve('foo/bar')
const promise = handle(file)
await t.throws(promise)
t.is(process.exit.getCall(0).args[0], 1)
})

test('log and process.exit when node version is below 8', async t => {
// Stub process.versions.node.split()
sinon.stub(String.prototype, 'split').callsFake(() => '7')
const logErrorSpy = sinon.spy()
handle.__set__('logError', logErrorSpy)
const file = path.resolve('test/fixtures/syntax-error')
const promise = handle(file)
await t.throws(promise)
t.is(logErrorSpy.callCount, 2)
t.is(process.exit.getCall(0).args[0], 1)
})

0 comments on commit 888b46e

Please sign in to comment.