Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Aug 24, 2016
1 parent 0441ca8 commit 4ac78d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var NativePromise = require('native-promise')
* const fs = require('fs')
* const getPromise = require('native-or-another')
* const NativeOrBluebird = getPromise()
* // => `bluebird` promise, but only if installed,
* // throws otherwise
*
* const NativeOrPromise = getPromise(require('promise'))
* const NativeOrPinkie = getPromise(require('pinkie'))
* const NativeOrQ = getPromise(require('q'))
Expand All @@ -42,7 +45,10 @@ var NativePromise = require('native-promise')
* @api public
*/
module.exports = function nativeOrAnother (Promize) {
if (NativePromise) return NativePromise
if (NativePromise) {
NativePromise.___nativePromise = true
return NativePromise
}
if (typeof Promize === 'function') {
Promize.___customPromise = true
return Promize
Expand All @@ -55,10 +61,8 @@ module.exports = function nativeOrAnother (Promize) {
console.error('Please install `bluebird` yourself (as devDependency is enough)')
console.error('or give Promise implementation.')
console.error('See the https://github.com/tunnckoCore/native-or-another for more info.')
process.exit(1)
throw err
}

Promize.___bluebirdPromise = true
return Promize
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
},
"devDependencies": {
"assertit": "^0.1.0",
"bluebird": "^3.4.2",
"cross-spawn": "^4.0.0",
"pinkie": "^2.0.1",
"semver": "^5.0.3"
"semver": "^5.0.3",
"spawn-sync": "^1.0.15"
},
"files": [
"index.js"
Expand Down
28 changes: 25 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,53 @@
var test = require('assertit')
var semver = require('semver')
var getPromise = require('./index')
var spawnSync = require('cross-spawn').sync

test('should throw err if no `Promise` given and if no `bluebird` installed', function (done) {
function fixture () {
return getPromise()
}

if (semver.lt(process.version, '0.11.13')) {
test.throws(fixture, Error)
test.throws(fixture, /Cannot find module 'bluebird'/)
return done()
}

test.strictEqual(fixture().___nativePromise, true)
done()
})

test('should use native Promise if available, Bluebird if installed', function (done) {
var res = spawnSync('npm', ['install', '-D', 'bluebird'])
/* istanbul ignore next */
if (res.error) return done(err)

var Promize = getPromise()
var promise = new Promize(function (resolve, reject) {
resolve(123)
})

promise.then(function (res) {
return promise.then(function (res) {
test.strictEqual(res, 123)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(Promize.___bluebirdPromise, true)
}
done()
var proc = spawnSync('npm', ['uninstall', '-D', 'bluebird'])
done(proc.error)
}, done)
})

test('should use given custom promise module', function (done) {
var Promize = getPromise(require('pinkie'))
var promise = Promize.resolve(456)

promise.then(function (res) {
return promise.then(function (res) {
test.strictEqual(res, 456)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(Promize.___customPromise, true)
}
done()
}, done)
})

0 comments on commit 4ac78d1

Please sign in to comment.