From 888b46e7a175b1c755cdc0b5062d0eabf71142ed Mon Sep 17 00:00:00 2001 From: Federico Miras Date: Tue, 27 Mar 2018 22:31:45 -0300 Subject: [PATCH] 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 --- package.json | 3 ++ test/fixtures/babel-promise-export.js | 1 + test/fixtures/native-promise-export.js | 1 + test/fixtures/regular-object.js | 1 + test/fixtures/syntax-error.js | 1 + test/handler.js | 62 ++++++++++++++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 test/fixtures/babel-promise-export.js create mode 100644 test/fixtures/native-promise-export.js create mode 100644 test/fixtures/regular-object.js create mode 100644 test/fixtures/syntax-error.js create mode 100644 test/handler.js diff --git a/package.json b/package.json index 0a38a61f..1c9ffaee 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ ], "extends": "prettier", "rules": { + "ava/use-test": 0, "unicorn/no-process-exit": 0 } }, @@ -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" diff --git a/test/fixtures/babel-promise-export.js b/test/fixtures/babel-promise-export.js new file mode 100644 index 00000000..63e77e7e --- /dev/null +++ b/test/fixtures/babel-promise-export.js @@ -0,0 +1 @@ +module.exports = { default: (async () => () => 'test')() } diff --git a/test/fixtures/native-promise-export.js b/test/fixtures/native-promise-export.js new file mode 100644 index 00000000..e7842450 --- /dev/null +++ b/test/fixtures/native-promise-export.js @@ -0,0 +1 @@ +module.exports = (async () => () => 'test')() diff --git a/test/fixtures/regular-object.js b/test/fixtures/regular-object.js new file mode 100644 index 00000000..4ba52ba2 --- /dev/null +++ b/test/fixtures/regular-object.js @@ -0,0 +1 @@ +module.exports = {} diff --git a/test/fixtures/syntax-error.js b/test/fixtures/syntax-error.js new file mode 100644 index 00000000..5df03b7a --- /dev/null +++ b/test/fixtures/syntax-error.js @@ -0,0 +1 @@ +throw new SyntaxError('async') diff --git a/test/handler.js b/test/handler.js new file mode 100644 index 00000000..673f765e --- /dev/null +++ b/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) +})