Repository: spion/blue-tape Branch: master Commit: 166a86de731e Files: 8 Total size: 6.3 KB Directory structure: gitextract_nw7nc_0v/ ├── .github/ │ └── ISSUE_TEMPLATE/ │ └── project-not-maintained.md ├── .gitignore ├── .npmignore ├── README.md ├── bin/ │ └── blue-tape.js ├── blue-tape.js ├── package.json └── test/ └── index.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/ISSUE_TEMPLATE/project-not-maintained.md ================================================ --- name: Project not maintained about: This project is no longer maintained title: '' labels: '' assignees: '' --- The best option is to switch to a library that supports promises - either https://github.com/jprichardson/tape-promise or https://www.node-tap.org/ - or perhaps its time to revisit substack/tape/issues/262 and add promise support to tape. ================================================ FILE: .gitignore ================================================ node_modules ================================================ FILE: .npmignore ================================================ test .gitignore ================================================ FILE: README.md ================================================ #### This project is no longer maintained. The best option is to switch to a library that supports promises - either https://github.com/jprichardson/tape-promise or https://github.com/tapjs/node-tap - or perhaps its time to revisit https://github.com/substack/tape/issues/262 and add promise support to tape. -------------- # blue-tape Tape with promise support. ### Usage Same as [tape](https://github.com/substack/tape), except if you return a promise from a test, it will be checked for errors. If there are no errors, the test will end. Otherwise the test will fail. This means there is no need to use `t.plan()` or `t.end()`. Also provides `t.shouldFail(promise P, optional class|regex expected, optional message)` (as well as the alias `shouldReject`) which returns a new promise that resolves successfully if `P` rejects. If you provide the optional class, or regex then it additionally ensures that `err` is an instance of that class or that the message matches the regular expression. The behaviour is identical to tape's `throws` assertion. ### Examples Assuming `delay()` returns a promise: ```js const test = require('blue-tape'); test("simple delay", function(t) { return delay(1); }); test("should fail", function(t) { return delay(1).then(function() { throw new Error("Failed!"); }); }); ``` Assuming `failDelay()` returns a promise that rejects with a DerpError: ```js test("promise fails but test succeeds", function(t) { return t.shouldFail(failDelay(), DerpError); }); ``` ### License MIT ================================================ FILE: bin/blue-tape.js ================================================ #!/usr/bin/env node require('tape/bin/tape') ================================================ FILE: blue-tape.js ================================================ var Test = require('tape/lib/test') function checkPromise (p) { return p && p.then && typeof p.then === 'function' } Test.prototype.run = function () { if (this._skip) { return this.end() } this.emit('prerun') try { var p = this._cb && this._cb(this) var isPromise = checkPromise(p) var self = this if (isPromise) { p.then(function () { self.end() }, function (err) { err ? self.error(err) : self.fail(err) self.end() }) } } catch (err) { if (err) { this.error(err) } else { this.fail(err) } this.end() return } this.emit('run') } function noop() {} Test.prototype.shouldFail = Test.prototype.shouldReject = function (promise, expected, message, extra) { var self = this return promise.then(function () { self.throws(noop, expected, message, extra) }, function (err) { function f() {throw err} self.throws(f, expected, message, extra) }) } module.exports = require('tape') ================================================ FILE: package.json ================================================ { "name": "blue-tape", "version": "1.0.0", "description": "Tape test runner with promise support", "main": "blue-tape.js", "bin": { "blue-tape": "./bin/blue-tape.js" }, "scripts": { "test": "standard && node test/index.js" }, "keywords": [ "tape", "bluebird", "promises" ], "author": "spion", "license": "MIT", "dependencies": { "tape": ">=2.0.0 <5.0.0" }, "directories": { "test": "test" }, "repository": { "type": "git", "url": "git://github.com/spion/blue-tape.git" }, "bugs": { "url": "https://github.com/spion/blue-tape/issues" }, "devDependencies": { "bl": "^4.0.3", "bluebird": "^2.1.2", "standard": "*" } } ================================================ FILE: test/index.js ================================================ var tape = require('../blue-tape') var bl = require('bl') var P = require('bluebird') P.longStackTraces() function test (name, test, checkErrors) { tape.test(name, function (t) { var htest = tape.createHarness() htest.createStream().pipe(bl(function (_, data) { checkErrors && checkErrors(data.toString().split('\n'), t) })) htest(function (t) { return test(t, htest) }) }) } function verifyAsserts (counts) { return function (lines, t) { t.equal(count(lines, /^ok/), counts.ok, 'should have ' + counts.ok + ' ok asserts') t.equal(count(lines, /^not ok/), counts.fail, 'should have ' + counts.fail + ' failed asserts') t.end() } } function count (lines, regex) { var c = 0 for (var k = 0; k < lines.length; ++k) { if (regex.test(lines[k])) { ++c } } return c } test('non-promise test', function (t) { t.ok(true) t.end() }, verifyAsserts({ok: 1, fail: 0})) test('simple delay', function (t) { return P.delay(1) }, verifyAsserts({ok: 0, fail: 0})) test('should not affect plan', function (t) { t.plan(2) t.ok(true) t.ok(true) return P.delay(1) }, verifyAsserts({ok: 2, fail: 0})) test('nested tests with promises', function (t) { t.test('delay1', function (t) { return P.delay(1) }) t.test('delay2', function () { return P.delay(1) }) }, verifyAsserts({ok: 0, fail: 0})) test('should error', function (t) { return P.delay(1).then(function () { throw new Error('Failed!') }) }, verifyAsserts({ok: 0, fail: 1})) test('should fail', function (t) { return P.delay(1).then(function () { return P.reject() }) }, verifyAsserts({ok: 0, fail: 1})) test('run test with only', function (t, htest) { var count = 0 htest('first', function (t) { t.equal(++count, 1) t.end() }) htest.only('second', function (t) { t.equal(++count, 1) t.end() }) t.end() }, verifyAsserts({ok: 1, fail: 0})) test('test that expects promise to fail', function (t) { return t.shouldFail(P.delay(1).then(function () { return P.reject(new Error("Test")) })) }, verifyAsserts({ok: 1, fail: 0})) test('test that expects promise to fail, but it succeeds', function (t) { return t.shouldFail(P.delay(1).then(function () { return P.resolve() })) }, verifyAsserts({ok: 0, fail: 1})) test('test that expects specific exception', function (t) { return t.shouldFail(P.delay(1).then(function () { var f = 5 f.toFixed(100) // RangeError }), RangeError) }, verifyAsserts({ok: 1, fail: 0})) test('test that expects wrong exception', function (t) { return t.shouldFail(P.delay(1).then(function () { var f = 5 f.toFixed(100) // RangeError }), SyntaxError) }, verifyAsserts({ok: 0, fail: 1}))