diff --git a/lib/mocha.js b/lib/mocha.js index 21a3269528..667300e36e 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -345,11 +345,12 @@ Mocha.prototype.randomize = function randomize(seed) { /** * Generate a random seed. - * @returns {number} Unsigned 32-bit integer + * @returns {string} Hexadecimal string */ Mocha.seed = function seed() { - return Random.integer(0, utils.MAX_SAFE_INTEGER)(Random.engines.mt19937() - .autoSeed()); + var engine = Random.engines.mt19937() + .autoSeed(); + return utils.toHex(Random.integer(0, utils.MAX_SAFE_INTEGER)(engine)); }; /** diff --git a/lib/utils.js b/lib/utils.js index f406dd5b0c..edb03a35ad 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -816,6 +816,15 @@ exports.isPromise = function isPromise(value) { return typeof value === 'object' && typeof value.then === 'function'; }; +/** + * Given integer `int`, try to convert it to a hex string. + * @param {number} int Integer + * @returns {string} Hexadecimal representation thereof + */ +exports.toHex = function toHex(int) { + return '0x' + int.toString(16).toUpperCase(); +}; + /** * Returns `true` if `value` is a "safe" 32-bit unsigned int. * @param {*} [value] Value to test diff --git a/test/acceptance/random.js b/test/acceptance/random.js index bde963bc3c..4d22f76731 100644 --- a/test/acceptance/random.js +++ b/test/acceptance/random.js @@ -3,20 +3,18 @@ var mocha = require('../..'); // yuk var Mocha = mocha.Mocha || mocha; +var utils = require('../../lib/utils'); describe('random option', function() { describe('seed', function() { - it('should return a number', function() { + it('should return a hex string', function() { expect(Mocha.seed()) .to - .be - .a('number'); + .match(/^0x[0-9A-F]+/); }); - it('should return a positive number', function() { - expect(Mocha.seed() > 0) - .to - .be(true); + it('should be a valid seed', function() { + expect(utils.toSafeUnsignedInteger(Mocha.seed())).to.be.a('number'); }); it('should return a finite number', function() { diff --git a/test/utils.spec.js b/test/utils.spec.js index 6459b4a778..d37fab0714 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -228,6 +228,12 @@ describe('utils', function() { }); }); + describe('.toHex', function() { + it('should convert an integer to hex', function() { + utils.toHex(0xDEADBEEF).should.equal('0xDEADBEEF'); + }); + }); + describe('.isPromise', function() { it('should return true if the value is Promise-ish', function() { utils.isPromise({then: function() {}}).should.be.true;