From cff88dc2186680e5df65377ca662218a8e23e3ad Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Fri, 19 Feb 2016 05:36:13 +0000 Subject: [PATCH] assert: Added few helper assertions --- lib/assert.js | 63 ++++++++++++++++++++ test/assert.test.js | 138 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 lib/assert.js create mode 100644 test/assert.test.js diff --git a/lib/assert.js b/lib/assert.js new file mode 100644 index 0000000..47f4a32 --- /dev/null +++ b/lib/assert.js @@ -0,0 +1,63 @@ +const assert = require('assert'); +const util = require('util'); +const fail = assert.fail; + +function truncate(s, n) { + return s.length < n ? s : s.slice(0, n) + '...'; +} + +function stringifyObject(data) { + if (typeof(data) !== 'string') { + data = util.inspect( + data, + { depth: 1 } + ) + .replace(/\n /g, ''); + } + return truncate(data, 64); +} + +assert.notOk = (value, message) => { + if (value) { + fail(value, false, message, '==', assert.notOk) + } +} + +assert.match = (test, expect, message) => { + let result = test.match(expect); + if (result) return; + + fail(test, expect, message, 'match', assert.match); +} + +assert.isFulfilled = (promise, message) => { + return Promise.resolve(true) + .then(() => promise) + .catch((err) => { + if (!message) { + message = `promise failed with ${err.message || stringifyObject(err)}`; + } + fail(err, null, message, '', assert.isFulfilled); + }); +} + +assert.isRejected = (promise, message) => { + let hasFailed = false; + + return Promise.resolve(true) + .then(() => promise) + .catch((data) => { + hasFailed = true; + return data; + }) + .then((data) => { + if (hasFailed) return data; + if (!message) { + message = `promise was fulfilled with ${stringifyObject(data)}`; + } + + fail(data, null, message, '', assert.isRejected); + }); +} + +module.exports = assert; diff --git a/test/assert.test.js b/test/assert.test.js new file mode 100644 index 0000000..4d9f357 --- /dev/null +++ b/test/assert.test.js @@ -0,0 +1,138 @@ +const assert = require('assert'); +const util = require('util'); +const assertExtended = require('../lib/assert'); + +describe('assert', () => { + const testLongObject = { + a: 1, b:2, c:3, d:4, + e: {herp: 51, derp: 23}, + f: 'asdfgagwegawegawegawegawe', + g: '32ghaiwugb23 238023' + }; + + it('assertExtended should be identical to extended', () => { + assert.strictEqual(assertExtended, assert); + }); + + describe('#notOk()', () => { + it('should exist', () => { + assert.ok(assert.notOk); + }); + + it('should throw for true values', () => { + assert.throws(() => { + assert.notOk(true); + }, assert.AssertionError); + }); + + it('should pass for false values', () => { + assert.notOk(false); + }); + }); + + describe('#isFulfilled()', () => { + it('should exist', () => { + assert.ok(assert.isFulfilled); + }); + + it('should throw for rejected promises', () => { + return assert.isFulfilled(Promise.reject({})) + .catch((err) => { + assert.ok(err.message.match(/promise fail/)); + }); + }); + + it('should properly parse rejected object response', () => { + let assertMessage = util.inspect(testLongObject, {depth: 1}).replace(/\n /g, ''); + assertMessage = assertMessage.slice(0, 64) + '...'; + + return assert.isFulfilled(Promise.reject(testLongObject)) + .catch((err) => + assert.notStrictEqual(err.message.indexOf(assertMessage), -1) + ); + }); + + it('should include error message if error', () => { + const assertMessage = 'something something dark side'; + return assert.isFulfilled(Promise.reject(new Error(assertMessage))) + .catch((err) => { + assert.ok(err.message.match(new RegExp('with ' + assertMessage))); + }); + }); + + it('should pass for resolved promises', () => { + return assert.isFulfilled(Promise.resolve()); + }); + + it('should support custom message', () => { + const assertMessage = 'something something dark side'; + return assert.isFulfilled(Promise.reject({}), assertMessage) + .catch((err) => assert.ok(err.message.match(assertMessage))); + }) + + it('should return result for the resolved promise', () => { + const assertResult = {a: 1} + + return assert.isFulfilled(Promise.resolve(assertResult)) + .then((data) => assert.strictEqual(data, assertResult)); + }); + }); + + describe('#isRejected()', () => { + it('should exist', () => { + assert.ok(assert.isRejected); + }); + + it('should throw for resolved promises', () => { + let hasFailed = false; + + return assert.isRejected(Promise.resolve({})) + .catch((err) => { + hasFailed = true; + assert.ok(err.message.match(/fulfilled with/)); + }) + .then(() => { + assert.strictEqual(hasFailed, true); + }); + }); + + it('should properly stringify objects', () => { + let assertMessage = util.inspect(testLongObject, {depth: 1}).replace(/\n /g, ''); + assertMessage = assertMessage.slice(0, 64) + '...'; + + return assert.isRejected(Promise.resolve(testLongObject)) + .catch((err) => + assert.notStrictEqual(err.message.indexOf(assertMessage), -1) + ); + }); + + it('should support custom message', () => { + const assertMessage = 'something something dark side'; + return assert.isRejected(Promise.resolve({}), assertMessage) + .catch((err) => assert.ok(err.message.match(assertMessage))); + }) + + it('should return result for the unresolved promise', () => { + const assertResult = {a: 1} + + return assert.isRejected(Promise.reject(assertResult)) + .then((data) => assert.strictEqual(data, assertResult)); + }); + }); + + describe('#match()', () => { + it('should exist', () => { + assert.ok(assert.match); + }); + + it('should throw if no match', () => { + assert.throws(() => { + assert.match('a', /b/); + }, assert.AssertionError); + }); + + it('should pass if matches', () => { + assert.match('a', /a/); + }); + }); +});