Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create handler test file and test promise cases (#346)
* 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
Showing
6 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { default: (async () => () => 'test')() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = (async () => () => 'test')() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new SyntaxError('async') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |