Skip to content

Commit

Permalink
[Tests] add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 6, 2021
1 parent 07693ae commit 5162b64
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
coverage/
13 changes: 13 additions & 0 deletions .nycrc
@@ -0,0 +1,13 @@
{
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"lines": 86,
"statements": 85.93,
"functions": 82.43,
"branches": 76.06,
"exclude": [
"coverage",
"test"
]
}
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -16,14 +16,16 @@
"aud": "^1.1.1",
"auto-changelog": "^2.0.0",
"eslint": "^6.8.0",
"safe-publish-latest": "^1.1.4"
"nyc": "^10.3.2",
"safe-publish-latest": "^1.1.4",
"tape": "^5.1.1"
},
"scripts": {
"prepublish": "safe-publish-latest",
"lint": "eslint .",
"postlint": "es-shim-api --bound",
"pretest": "npm run lint",
"tests-only": "exit 1",
"tests-only": "nyc tape 'test/**/*.js'",
"test": "npm run tests-only",
"posttest": "aud --production",
"version": "auto-changelog && git add CHANGELOG.md",
Expand Down
21 changes: 21 additions & 0 deletions test/implementation.js
@@ -0,0 +1,21 @@
'use strict';

var test = require('tape');

var runTests = require('./tests');

test('implementation', function (t) {
/* eslint global-require: 1 */
if (typeof Promise === 'function') {
var promisify = require('../implementation');
runTests(promisify, t);
} else {
t['throws'](
function () { require('../implementation'); },
TypeError,
'throws when no Promise available'
);
}

t.end();
});
21 changes: 21 additions & 0 deletions test/index.js
@@ -0,0 +1,21 @@
'use strict';

var test = require('tape');

var runTests = require('./tests');

test('as a function', function (t) {
/* eslint global-require: 1 */
if (typeof Promise === 'function') {
var promisify = require('../');
runTests(promisify, t);
} else {
t['throws'](
function () { require('../'); },
TypeError,
'throws when no Promise available'
);
}

t.end();
});
14 changes: 14 additions & 0 deletions test/shimmed.js
@@ -0,0 +1,14 @@
'use strict';

var test = require('tape');
var util = require('util');

var runTests = require('./tests');

test('shimmed', { skip: typeof Promise !== 'function' }, function (t) {
require('../auto'); // eslint-disable-line global-require

runTests(util.promisify, t);

t.end();
});
44 changes: 44 additions & 0 deletions test/tests.js
@@ -0,0 +1,44 @@
'use strict';

module.exports = function runTests(promisify, t) {
t.equal(typeof promisify, 'function', 'promisify is a function');

t['throws'](
function () { promisify(null); },
TypeError,
'throws on non-functions'
);

var yes = function () {
var cb = arguments[arguments.length - 1];
cb(null, Array.prototype.slice.call(arguments, 0, -1));
};
var no = function () {
var cb = arguments[arguments.length - 1];
cb(Array.prototype.slice.call(arguments, 0, -1));
};

var pYes = promisify(yes);
var pNo = promisify(no);

t.equal(typeof pYes, 'function', 'pYes is a function');
t.equal(typeof pNo, 'function', 'pNo is a function');

t.test('pYes is properly promisified', { skip: typeof Promise !== 'function' }, function (st) {
st.plan(1);

var p = pYes(1, 2, 3);
return p.then(function (result) {
st.deepEqual(result, [1, 2, 3], 'fulfillment: arguments are preserved');
});
});

t.test('pNo is properly promisified', { skip: typeof Promise !== 'function' }, function (st) {
st.plan(1);

var p = pNo(1, 2, 3);
return p.then(null, function (args) {
st.deepEqual(args, [1, 2, 3], 'rejection: arguments are preserved');
});
});
};

0 comments on commit 5162b64

Please sign in to comment.