eltro/test/assert.test.mjs

155 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2020-03-31 17:27:36 +00:00
import util from 'util'
import assert from 'assert'
import assertExtended from '../lib/assert.mjs'
import t from '../lib/eltro.mjs'
2020-03-31 17:27:36 +00:00
const testLongObject = {
a: 1, b:2, c:3, d:4,
e: {herp: 51, derp: 23},
f: 'asdfgagwegawegawegawegawe',
g: '32ghaiwugb23 238023'
}
t.describe('#notOk()', function() {
t.test('should exist', function() {
2020-03-31 17:27:36 +00:00
assertExtended.ok(assertExtended.notOk)
})
t.test('should throw for true values', function() {
2020-03-31 17:27:36 +00:00
assertExtended.throws(function() {
assertExtended.notOk(true)
}, assertExtended.AssertionError)
})
t.test('should pass for false values', function() {
2020-03-31 17:27:36 +00:00
assertExtended.notOk(false)
assertExtended.notOk(null)
assertExtended.notOk(0)
})
})
t.describe('#isFulfilled()', function() {
t.test('should exist', function() {
2020-03-31 17:27:36 +00:00
assertExtended.ok(assertExtended.isFulfilled)
})
t.test('should throw for rejected promises', function() {
2020-03-31 17:27:36 +00:00
return assertExtended.isFulfilled(Promise.reject({}))
.catch((err) => {
assertExtended.ok(err.message.match(/promise fail/))
})
})
t.test('should properly parse rejected object response', function() {
2020-03-31 17:27:36 +00:00
let assertMessage = util.inspect(testLongObject, {depth: 1}).replace(/\n /g, '')
assertMessage = assertMessage.slice(0, 64) + '...'
return assertExtended.isFulfilled(Promise.reject(testLongObject))
.catch((err) =>
assertExtended.notStrictEqual(err.message.indexOf(assertMessage), -1)
)
})
t.test('should include error message if error', function() {
2020-03-31 17:27:36 +00:00
const assertMessage = 'something something dark side'
return assertExtended.isFulfilled(Promise.reject(new Error(assertMessage)))
.catch((err) => {
assertExtended.ok(err.message.match(new RegExp('with ' + assertMessage)))
})
})
t.test('should pass for resolved promises', function() {
2020-03-31 17:27:36 +00:00
return assertExtended.isFulfilled(Promise.resolve())
})
t.test('should support custom message', function() {
2020-03-31 17:27:36 +00:00
const assertMessage = 'something something dark side'
return assertExtended.isFulfilled(Promise.reject({}), assertMessage)
.catch((err) => {
assertExtended.ok(err.message.match(assertMessage))
})
})
t.test('should return result for the resolved promise', function() {
2020-03-31 17:27:36 +00:00
const assertResult = {a: 1}
return assertExtended.isFulfilled(Promise.resolve(assertResult))
.then((data) => assertExtended.strictEqual(data, assertResult))
})
})
t.describe('#isRejected()', function() {
t.test('should exist', function() {
2020-03-31 17:27:36 +00:00
assertExtended.ok(assertExtended.isRejected)
})
t.test('should throw for resolved promises', function() {
2020-03-31 17:27:36 +00:00
let hasFailed = false
return assertExtended.isRejected(Promise.resolve({}))
.catch((err) => {
hasFailed = true
assertExtended.ok(err.message.match(/fulfilled with/))
})
.then(function() {
assertExtended.strictEqual(hasFailed, true)
})
})
t.test('should properly stringify objects', function() {
2020-03-31 17:27:36 +00:00
let assertMessage = util.inspect(testLongObject, {depth: 1}).replace(/\n /g, '')
assertMessage = assertMessage.slice(0, 64) + '...'
return assertExtended.isRejected(Promise.resolve(testLongObject))
.catch((err) =>
assertExtended.notStrictEqual(err.message.indexOf(assertMessage), -1)
)
})
t.test('should support custom message', function() {
2020-03-31 17:27:36 +00:00
const assertMessage = 'something something dark side'
return assertExtended.isRejected(Promise.resolve({}), assertMessage)
.catch((err) => assertExtended.ok(err.message.match(assertMessage)))
})
t.test('should return result for the unresolved promise', function() {
2020-03-31 17:27:36 +00:00
const assertResult = {a: 1}
return assertExtended.isRejected(Promise.reject(assertResult))
.then((data) => assertExtended.strictEqual(data, assertResult))
})
})
t.describe('#match()', function() {
t.test('should exist', function() {
2020-03-31 17:27:36 +00:00
assertExtended.ok(assertExtended.match);
});
t.test('should throw if no match', function() {
2020-03-31 17:27:36 +00:00
assertExtended.throws(function() {
assertExtended.match('a', /b/);
}, assertExtended.AssertionError);
});
t.test('should pass if matches', function() {
2020-03-31 17:27:36 +00:00
assertExtended.match('a', /a/);
});
})
t.describe('#notMatch()', function() {
t.test('should exist', function() {
2020-03-31 17:27:36 +00:00
assertExtended.ok(assertExtended.notMatch);
});
t.test('should throw if match', function() {
2020-03-31 17:27:36 +00:00
assertExtended.throws(function() {
assertExtended.notMatch('a', /a/);
}, assertExtended.AssertionError);
});
t.test('should pass if not matches', function() {
2020-03-31 17:27:36 +00:00
assertExtended.notMatch('a', /b/);
});
})