From 6b44120d5380d74da4f72309f3fa24503f97be48 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Fri, 8 Aug 2014 18:25:19 -0700 Subject: [PATCH] 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. --- test/ctor.test.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/test/ctor.test.js b/test/ctor.test.js index 2435497..57920e6 100644 --- a/test/ctor.test.js +++ b/test/ctor.test.js @@ -19,11 +19,11 @@ var test = tap4nodeunit.test; test('ensure Logger creation options', function (t) { t.throws(function () { new Logger(); }, - 'options (object) is required', + /options \(object\) is required/, 'no options should throw'); t.throws(function () { new Logger({}); }, - 'options.name (string) is required', + /options\.name \(string\) is required/, 'no options.name should throw'); 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: []}; t.throws(function () { new Logger(options); }, + /cannot mix "streams" and "stream" options/, 'cannot use "stream" and "streams"'); // https://github.com/trentm/node-bunyan/issues/3 options = {name: 'foo', streams: {}}; 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'); options = {name: 'foo', serializers: 'a string'}; 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'); options = {name: 'foo', serializers: [1, 2, 3]}; 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'); t.end(); @@ -63,11 +64,11 @@ test('ensure Logger constructor is safe without new', function (t) { test('ensure Logger creation options (createLogger)', function (t) { t.throws(function () { bunyan.createLogger(); }, - 'options (object) is required', + /options \(object\) is required/, 'no options should throw'); t.throws(function () { bunyan.createLogger({}); }, - 'options.name (string) is required', + /options\.name \(string\) is required/, 'no options.name should throw'); 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: []}; t.throws(function () { bunyan.createLogger(options); }, + /cannot mix "streams" and "stream" options/, 'cannot use "stream" and "streams"'); // https://github.com/trentm/node-bunyan/issues/3 options = {name: 'foo', streams: {}}; 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'); options = {name: 'foo', serializers: 'a string'}; 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'); options = {name: 'foo', serializers: [1, 2, 3]}; 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'); t.end(); @@ -107,27 +109,28 @@ test('ensure Logger child() options', function (t) { 'empty options should be fine too'); 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'); var options = {stream: process.stdout, streams: []}; t.throws(function () { log.child(options); }, + /cannot mix "streams" and "stream" options/, 'cannot use "stream" and "streams"'); // https://github.com/trentm/node-bunyan/issues/3 options = {streams: {}}; 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'); options = {serializers: 'a string'}; 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'); options = {serializers: [1, 2, 3]}; 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'); t.end();