test: make throw tests validate was is thrown

When the second argument to nodeunit's assert.throws() is a string it
is used as the failure message, not for exception message matching.

To assert the contents of the exception's message a RegExp must be
used.
This commit is contained in:
Ryan Graham 2014-08-08 18:25:19 -07:00
parent c3b8dd26a7
commit 6b44120d53

View file

@ -19,11 +19,11 @@ var test = tap4nodeunit.test;
test('ensure Logger creation options', function (t) { test('ensure Logger creation options', function (t) {
t.throws(function () { new Logger(); }, t.throws(function () { new Logger(); },
'options (object) is required', /options \(object\) is required/,
'no options should throw'); 'no options should throw');
t.throws(function () { new Logger({}); }, t.throws(function () { new Logger({}); },
'options.name (string) is required', /options\.name \(string\) is required/,
'no options.name should throw'); 'no options.name should throw');
t.doesNotThrow(function () { new Logger({name: 'foo'}); }, t.doesNotThrow(function () { new Logger({name: 'foo'}); },
@ -31,22 +31,23 @@ test('ensure Logger creation options', function (t) {
var options = {name: 'foo', stream: process.stdout, streams: []}; var options = {name: 'foo', stream: process.stdout, streams: []};
t.throws(function () { new Logger(options); }, t.throws(function () { new Logger(options); },
/cannot mix "streams" and "stream" options/,
'cannot use "stream" and "streams"'); 'cannot use "stream" and "streams"');
// https://github.com/trentm/node-bunyan/issues/3 // https://github.com/trentm/node-bunyan/issues/3
options = {name: 'foo', streams: {}}; options = {name: 'foo', streams: {}};
t.throws(function () { new Logger(options); }, t.throws(function () { new Logger(options); },
'invalid options.streams: must be an array', /invalid options.streams: must be an array/,
'"streams" must be an array'); '"streams" must be an array');
options = {name: 'foo', serializers: 'a string'}; options = {name: 'foo', serializers: 'a string'};
t.throws(function () { new Logger(options); }, t.throws(function () { new Logger(options); },
'invalid options.serializers: must be an object', /invalid options.serializers: must be an object/,
'"serializers" cannot be a string'); '"serializers" cannot be a string');
options = {name: 'foo', serializers: [1, 2, 3]}; options = {name: 'foo', serializers: [1, 2, 3]};
t.throws(function () { new Logger(options); }, t.throws(function () { new Logger(options); },
'invalid options.serializers: must be an object', /invalid options.serializers: must be an object/,
'"serializers" cannot be an array'); '"serializers" cannot be an array');
t.end(); t.end();
@ -63,11 +64,11 @@ test('ensure Logger constructor is safe without new', function (t) {
test('ensure Logger creation options (createLogger)', function (t) { test('ensure Logger creation options (createLogger)', function (t) {
t.throws(function () { bunyan.createLogger(); }, t.throws(function () { bunyan.createLogger(); },
'options (object) is required', /options \(object\) is required/,
'no options should throw'); 'no options should throw');
t.throws(function () { bunyan.createLogger({}); }, t.throws(function () { bunyan.createLogger({}); },
'options.name (string) is required', /options\.name \(string\) is required/,
'no options.name should throw'); 'no options.name should throw');
t.doesNotThrow(function () { bunyan.createLogger({name: 'foo'}); }, t.doesNotThrow(function () { bunyan.createLogger({name: 'foo'}); },
@ -75,22 +76,23 @@ test('ensure Logger creation options (createLogger)', function (t) {
var options = {name: 'foo', stream: process.stdout, streams: []}; var options = {name: 'foo', stream: process.stdout, streams: []};
t.throws(function () { bunyan.createLogger(options); }, t.throws(function () { bunyan.createLogger(options); },
/cannot mix "streams" and "stream" options/,
'cannot use "stream" and "streams"'); 'cannot use "stream" and "streams"');
// https://github.com/trentm/node-bunyan/issues/3 // https://github.com/trentm/node-bunyan/issues/3
options = {name: 'foo', streams: {}}; options = {name: 'foo', streams: {}};
t.throws(function () { bunyan.createLogger(options); }, t.throws(function () { bunyan.createLogger(options); },
'invalid options.streams: must be an array', /invalid options.streams: must be an array/,
'"streams" must be an array'); '"streams" must be an array');
options = {name: 'foo', serializers: 'a string'}; options = {name: 'foo', serializers: 'a string'};
t.throws(function () { bunyan.createLogger(options); }, t.throws(function () { bunyan.createLogger(options); },
'invalid options.serializers: must be an object', /invalid options.serializers: must be an object/,
'"serializers" cannot be a string'); '"serializers" cannot be a string');
options = {name: 'foo', serializers: [1, 2, 3]}; options = {name: 'foo', serializers: [1, 2, 3]};
t.throws(function () { bunyan.createLogger(options); }, t.throws(function () { bunyan.createLogger(options); },
'invalid options.serializers: must be an object', /invalid options.serializers: must be an object/,
'"serializers" cannot be an array'); '"serializers" cannot be an array');
t.end(); t.end();
@ -107,27 +109,28 @@ test('ensure Logger child() options', function (t) {
'empty options should be fine too'); 'empty options should be fine too');
t.throws(function () { log.child({name: 'foo'}); }, t.throws(function () { log.child({name: 'foo'}); },
'invalid options.name: child cannot set logger name', /invalid options.name: child cannot set logger name/,
'child cannot change name'); 'child cannot change name');
var options = {stream: process.stdout, streams: []}; var options = {stream: process.stdout, streams: []};
t.throws(function () { log.child(options); }, t.throws(function () { log.child(options); },
/cannot mix "streams" and "stream" options/,
'cannot use "stream" and "streams"'); 'cannot use "stream" and "streams"');
// https://github.com/trentm/node-bunyan/issues/3 // https://github.com/trentm/node-bunyan/issues/3
options = {streams: {}}; options = {streams: {}};
t.throws(function () { log.child(options); }, t.throws(function () { log.child(options); },
'invalid options.streams: must be an array', /invalid options.streams: must be an array/,
'"streams" must be an array'); '"streams" must be an array');
options = {serializers: 'a string'}; options = {serializers: 'a string'};
t.throws(function () { log.child(options); }, t.throws(function () { log.child(options); },
'invalid options.serializers: must be an object', /invalid options.serializers: must be an object/,
'"serializers" cannot be a string'); '"serializers" cannot be a string');
options = {serializers: [1, 2, 3]}; options = {serializers: [1, 2, 3]};
t.throws(function () { log.child(options); }, t.throws(function () { log.child(options); },
'invalid options.serializers: must be an object', /invalid options.serializers: must be an object/,
'"serializers" cannot be an array'); '"serializers" cannot be an array');
t.end(); t.end();